class Rule {
has Int $.neighbors = 2;
has %.lookup =
( '11'=>'0', '10'=>'1', '01'=>'1', '00'=>'0' );
}
This code isn't particularly useful by itself, but if you wanted to try it, just copy and past that code into Pugs, a Perl 6 implementation, and call it like so:Note that both $.neighbors and %.lookup have not one, but two punctuation marks at the beginning of the word. The first is a sigil. The sigil designates $.neighbor to be a scalar, and %.lookup to be a hash (e.g. lookup table). The second character (both have a period) tells the class to create accessors for each. So we can do things like "$rule.neighbors()"pugs> my Rule $rule .= new();
pugs> say "Rule has $rule.neighbors() neighbors."
Rule has 2 neighbors.
class Rule {
has Int $.number is ro;
has Int $.neighbors is ro;
has Bool %.lookup is rw;
submethod BUILD (:$.number, :$.neighbors) {
for ( 0 .. (2 ** $.neighbors -1) ) -> $key {
my $format = "\%0" ~ $.neighbors ~ "b";
%.lookup{sprintf($format,$key)} =
?($.number +& (1 ~ 0 x $key) );
}
}
}Why?pugs> my Rule $rule .= new(:neighbors(2), :number(6));
Labels: cellular automata, deusexautomatis, etech
Subscribe to
Posts [Atom]