Created
December 3, 2010 04:27
-
-
Save dalinaum/726580 to your computer and use it in GitHub Desktop.
perl 6 oop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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