in reply to Perl6 lazy list of code blocks
I can explain why this error occurs. The sequence operator ... tries to invoke your closure, but since you didn't provide any elements to start with, it tries to invoke the closure without arguments, and fails.
The solution is probably to not use the sequence operator for this kind of thing.
Based on what you have written, I guess that this is what you want to achieve:
use v6; my @blocks := (0..*).map: -> $n { -> $x, $y { $x * $y * $n } }; say @blocks[2](2, 9);
It doesn't make much sense to me, but it is an infinite list of closures, each one having a higher $n than the previous one.
Update: An approach that is closer to your original is to use the list repetition operator:
sub gen($x, $y) { state $n = 0; $x*$y*$n++ } my @foo := &gen xx *;
But that probably doesn't do what you want. Since merely creating the elements of the infinite list doesn't invoke the subroutine, $n is incremented in the order that the closures are called, not in the order they appear inside the list. In the example above, $n is shared between all references to &gen, so it will always increase by one. If you write it as
my @foo := sub ($x, $y) { state $n = 0; $x*$y*$n++ } xx *;
instead, each closure gets a separate $n, so calling
say @foo[2](1, 1); say @foo[0](1, 1); say @foo[5](1, 1); say @foo[0](1, 1);
Produces
0 0 0 1
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl6 lazy list of code blocks
by aes (Initiate) on May 23, 2012 at 19:06 UTC | |
by moritz (Cardinal) on May 24, 2012 at 07:08 UTC | |
by aes (Initiate) on May 24, 2012 at 10:27 UTC | |
by aes (Initiate) on May 23, 2012 at 21:06 UTC | |
by moritz (Cardinal) on May 24, 2012 at 08:21 UTC | |
by aes (Initiate) on May 24, 2012 at 10:34 UTC |