Em Qui, 2009-05-28 às 00:26 -0500, John M. Dlugosz escreveu: > Mark J. Reed markjreed-at-gmail.com |Perl 6| wrote: > > Perhaps Perl 6 should not aspire to the expressiveness of APL. :) As > > nice as it is that you can write Conway's Life in a one-liner(*), I > > think that a little verbosity now and then is a good thing for > > legibility.... > > (*) life ←{↑1 ω⌵.^3 4=+/,‾1 0 1◦.ϕ⊂ω} > So how would that translate to Perl 6? Both as an exact translation, > and how to do it better in a Perl way?
I didn't try to translate it, but rather I simply tried to implement it in a way rakudo already runs.... So... here's my shot! <CODE> class Game { # XXX: rakudo doesn't know how to initialize arrays properly yet. has $.seed; has $.width; has $.height; multi method generation(Int $generation where 0) { return @($.seed); } multi method generation(Int $generation) { my @previous = self.generation($generation - 1); my @new; # XXX: rakudo doesn't autovivify arrays properly yet. @new.push: [] for ^$.width; for ^$.width X ^$.height -> $x, $y { # XXX: there's an extra set of parens for rakudo my $value = [+] map { @previous[$^i][$^j] }, ((grep { 0 <= $_ < $.width }, ($x-1)..($x+1)) X (grep { 0 <= $_ < $.height }, ($y-1)..($y+1))); if @previous[$x][$y] && 2 <= ($value - 1) <= 3 { @new[$x][$y] = 1; } elsif !...@previous[$x][$y] && $value == 3 { @new[$x][$y] = 1; } else { @new[$x][$y] = 0; } } return @new; } } my $game = Game.new( :width(15), :height(15), :seed([ [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 ] ])); my @g = $game.generation(4); say .perl for @g; </CODE> I guess my mindset is too much "imperative" for me to think in a more elegant solution... daniel