in reply to Re^2: Perl6 lazy list of code blocks
in thread Perl6 lazy list of code blocks

I found a bug. Using...

my @foo = { state $n=-1; $n++; sub ($x,$y) { $x+$y+$n } } ... *;

...in my sequential access code example from my previous post, the state variable $n seemed to maintain the ordinal position of each closure in the series, but it hid the bug of $n being instead a high-water mark for positional access, and so...

my $foo_0 = @foo[0].(3,5); my $foo_1 = @foo[1].(3,5); my $foo_2 = @foo[2].(3,5);

...produces...

10 10 10

...when what I desired was...

8 9 10

Thus, after an additional scope trick to lexically package (in $p) the persistent state value (in $n), this is my better solution...

my @foo = { state $n=-1; $n++; my $p=$n; sub ($x,$y) { $x+$y+$p } } .. +. *;

Replies are listed 'Best First'.
Re^4: Perl6 lazy list of code blocks
by moritz (Cardinal) on May 24, 2012 at 08:21 UTC
      Ah, but you're repeating the sequential access behavior that hides the bug. Try instead reversing the access order and you'll see the "high water mark" bug.
      say @foo[2].(3,5); say @foo[1].(3,5); say @foo[0].(3,5);
      ...produces...
      10 10 10