in reply to Re: Perl6 lazy list of code blocks
in thread Perl6 lazy list of code blocks
Thank you, moritz, for your explanation, and in the process, for introducing me to several aspects of Perl6 I was unfamiliar with.
I am indeed seeking to build an indefinite list of closures, each one having an incremented value of $n. If this seems an odd request, then rest assured it's only because I've rendered this as an isolated, simplified problem for posting here; the more complex application uses an analogous data structure in an elemental way.
Your first suggestion...
my @blocks := (0..*).map: -> $n { -> $x, $y { $x * $y * $n } };
...seems just right, but it hangs rakudo-star-2012.04.
Your second suggestion...
sub gen($x, $y) { state $n = 0; $x*$y*$n++ } my @foo := &gen xx *;
...is, as you suggested, not what I'm looking for, because I want $n to reflect the ordinal position in the series, not the ordinal call order of the closure.
Your third suggestion...
my @foo := sub ($x, $y) { state $n = 0; $x*$y*$n++ } xx *;
...gives every element of @foo the same ordinal position in the series (0), and re-accessing @foo[0], say, increments $n for @foo[0]'s closure, whereas I want it to be static indicator of ordinal position in the series.
Given what you said about the sequence operator (...) trying to invoke my closure, I realized that what I need is the equivalent of prime (') in CL/Scheme, and to properly scope the ordinal incrementor $n.
So this is my solution...
...which when accessed in the following manner...my @foo = { state $n=-1; $n++; sub ($x,$y) { $x+$y+$n } } ... *;
...will produce the following output...for 0..2 -> $i { for 0..2 { say @foo[$i](3,5); } say; }
8 8 8 9 9 9 10 10 10
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Perl6 lazy list of code blocks
by moritz (Cardinal) on May 24, 2012 at 07:08 UTC | |
by aes (Initiate) on May 24, 2012 at 10:27 UTC | |
|
Re^3: Perl6 lazy list of code blocks
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 |