http://www.perlmonks.org?node_id=253438


in reply to Prolegemona To A Future Scripting Language: Game Of Life In Perl 6

That's a great effort, especially without a compiler. Here are a few corrections and some more "idiomatic" solutions.

Comments in bold italics explain corrections on the previous line.
Comments in normal italics explain stylistic choices on the previous line.


my $life = new life: 20;
# Indirect object syntax requires colon after invocant
loop { $life.display() }
# Argumentless &loop is cleaner that &while for infinite loops
class life { has Int $.count; has Int $.max; has Array of Bit @.grid;
# Bit is potentially much smaller than Int for 1/0 values
method BUILD(Int $dim)
# BUILD is the object initializer.
# CREATE is something else entirely (see Apocalypse 12 ;-)
{ $.count = 0; $.max = $dimension-1; my Array of Bit @grid is dim($dim,$dim) is default(0);
# Setting default value to 0 reduces initialization costs
@grid[$dim / 2 - 1][$dim / 2 ] = 1; @grid[$dim / 2 - 1][$dim / 2 + 1] = 1; @grid[$dim / 2 ][$dim / 2 ] = 1; @grid[$dim / 2 ][$dim / 2 - 1] = 1; @grid[$dim / 2 + 1][$dim / 2 ] = 1; @.grid := @grid;
# Can't declare variably dimensioned array as attribute.
# Have to declare it when dimension known (i.e. in
# constructor) and then bind it to attribute
} sub iterate (&block)
# Utility sub to factor out repetitive 2D iterations
{ for 0..$.max -> $x
# Prefer &for over &loop when possible (e.g. here)
{ for 0..$.max -> $y { block($x,$y); } } } method calculate() is private { my @newgrid; iterate { my $live = sum(@.grid[$^x-1..$^x+1][$^y-1..$^y+1]); @newgrid[$^x][$^y] = $live==2 && @.grid[$^x][$^y] || $live==3; }
# Pass &iterate a block with placeholders for $x and $y
@.grid = @newgrid; } method display { iterate { print $.grid[$^x][$^y] ?? '+' :: '.'; print "\n" if $^x == $.max; } print "\n"; print "Turn $(++$.count), press enter to continue or ctl-c to quit +; <$*IN>;
# STDIN is now $*IN
.calculate(); } }
Update: Fixed display method (thanks jkahn)