Skip to content

Instantly share code, notes, and snippets.

@dalinaum
Created December 3, 2010 04:27
Show Gist options
  • Save dalinaum/726580 to your computer and use it in GitHub Desktop.
Save dalinaum/726580 to your computer and use it in GitHub Desktop.
perl 6 oop
class Person {
# $. means "public", "is rw" means it is writable.
has ($.position, $.succ is rw);
has $.alive is rw = True;
# Stringify
method Str() {
"Person $.position, " ~ ($.alive ?? "alive" !! "dead")
}
# Create a linked chain of people.
method createChain($n) {
return self unless $n > 0;
$.succ = Person.new(position => $.position + 1);
$.succ.createChain($n-1);
}
# Kill every nth person. Stop killing if I'm the last one.
method kill($pos is rw, $n, $remaining is rw) {
return $.succ.kill($pos, $n, $remaining) if !$.alive;
return self if $remaining == 1;
if $pos == $n {
$.alive = False;
$pos = 0;
$remaining -= 1;
}
$.succ.kill($pos+1,$n, $remaining);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment