As a little Perl 6 exercise I translated the Perl 5 Markov chain / dissociated-press script from _The Practice of Programming_:
http://cm.bell-labs.com/cm/cs/tpop/markov.pl Here's my Perl 6 attempt, with support for different prefix lengths. my $n = 2; # prefix length my $max = 10000; # max output words my %state; my $o = 0; my @w = [;] "\n" xx $n # initial sate does { method adv(@w: $i){ @w <== $i; @w.shift; } }; for <>.slurp.split { # read each word of input %state{@@w}.push: $_; @w.adv: $_; # advance chain } %state{@@w}.push: "\n"; # add tail @w »=» "\n"; # reset initial state while ($_ = %state{@@w}.pick).say { last if /\n/ or $o++ >= $max; @w.adv: $_; # advance chain } Since pugs doesn't seem to have full multislice support yet, can anyone comment on whether I have the syntax and semantics right here? In particular: - Does shift do what I'm expecting it to do with a multidim array? - Am I using the @ and @@ sigils correctly? I think I could just use @@ everywhere (right?), but I tried to use it only where needed. Do I need an @@ on the method invocant or anywhere else? - Does @w »=» "\n" leave the multidimensionality intact? - As I understand it, the way I have it written the anonymous mixin is applied to the value in @w and not to the @w container. If I did something like @w = 1,2,3 that would clobber the mixin, right? Does the hyperop-assignment avoid that? - Is there some concise syntax for attaching the mixin to the container, like my @w is (Array does {...}) = ...; Is this the correct non-golf way to do it? Role R {...} class A is Array { does R } my @w is A = ...; - Can I call methods on <> or do I have to say $*ARGS? - Can for <>.slurp.split {...} be trusted to not use a huge amount of memory for large inputs? Any commentary or suggestions for improvement are welcomed. -ryan (rhr on #perl6)