Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Prolegemona To A Future Scripting Language: Game Of Life In Perl 6

by jaldhar (Vicar)
on May 01, 2003 at 05:43 UTC ( #254569=note: print w/replies, xml ) Need Help??

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

Thanks to all that replied. This was very enlightening for me and I hope it was for others too. Here is the script again with suggestions and corrections various monks made and some comments.

#!/usr/bin/perl use warnings; use strict;

theDamian calls this indirect object syntax. what's the direct syntax then?

my $life = new life : 20;

theDamian points out this is a more idiomatic way of doing an infinite loop.

loop { $life.display() }

In perl 6, a class isn't just a package but a new type of block scope indicated by the keyword class. It can have methods indicated by method and attributes indicated by has.

class life {

Perl 6 will still have the regular data types but you will have more say over what range of values they can hold or how they should be represented at the machine level. For instance my grid only holds 0 and 1 values. I originally had Int but several monks point out a Bit is potentially more space-efficient.

has Int $.count; has Int $.dimension; has Array of Bit @.grid;

There's no need to create a constructor now it is automatically done for you. Instead you should do initialization in a special method. I was led by the Linux Magazine article to believe it should be called CREATE bu theDamian says it ought to be called BUILD.

You will note functions can have proper prototypes now.

method BUILD(Int $dimension) { $.count = 0; $.max = $dimension - 1;

Here we are creating an array of bits. We are saying that it is two-d, size $dimension x $dimension, and the default value of each element is 0.

my Array of Bit @.grid is dim($dimension,$dimension) is default(0);

For those who are not up on their game of life theory, what's happening here is an initial grouping of on cells is being created in the center of the grid. The shape is called an R-pentomino and it becomes static after 100 turns.

@grid[$dimension / 2 - 1][$dimension / 2] = 1; @grid[$dimension / 2 - 1][$dimension / 2 + 1] = 1; @grid[$dimension / 2][$dimension / 2] = 1; @grid[$dimension / 2][$dimension / 2 - 1] = 1; @grid[$dimension / 2 + 1][$dimension / 2] = 1;

Because the grid is a multidimensional array it cannot be directly declared as an attribute. That's why we made a local variable called @grid. Now that we know its' size we can assign it to the attibute.

@.grid = @grid; }

theDamian suggested adding this routine as a utility. Why is it declared as sub and not method? I'll take a guess and say so it can be inherited by subclasses but not used outside the clase sort of like c++ protected functions. Or it's a typo :-)

sub iterate (&block) {

This is what foreach loops will look like. No need to put a my on the $x and $y, it's implied.

Also note parentheses are optional on fors, ifs etc.

for 0..$.max -> $x { for 0..$.max -> $y { block($x,$y); } } }

We can declare methods private to our class. In perl5 you could always get to the internals of an object.

method calculate() is private { my @newgrid; iterate {

$^x and $^y are placeholders. They are an implicit way of saying my($x, $y) = @_; You should use them as you need them and they are assigned alphabetically (in UTF-8) to the parameters of your function.

my $live = sum(@.grid[$^x-1..$^x+1][$^y-1..$^y+1]); @newgrid[$^x][$^y] = $live==2 && @.grid[$^x][$^y] || $live==3; } @.grid = @newgrid; } method display { iterate { print $.grid[$^x][$^y] ?? '+' :: '.'; print "\n" if $^x == $.max; } print "\n";

We can interpolate any arbitrary code or variable into a string with $( ... )

print "Turn $(++$.count), press enter to continue or ctl-c to quit +;

File handles are now scalars and STDIN is called IN.


jkahn wondered why I have calculate here and not in the main loop. That's how I had it originally and how I would do it in a real program but I confess I broke one of my own rules for this exercise and made calculate a private method just to show off that feature.

.calculate(); } }


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://254569]
[atcroft]: beech: No, it was an attempt at a physics demonstration.

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2017-05-27 05:46 GMT
Find Nodes?
    Voting Booth?