Buddha Buck wrote:
Examples:
# process @array, one element at a time
for @array -> $x { ... };
Yes.
# process @array, in pairs
for @array -> $x, $y { ... };
Yes.
# process all of @a, then all of @b, one element at a time
for @a, @b -> $x { ... };
Yes.
# process @a, then @b, in pairs (@a[0] and @a[1] first, etc)
for @a, @b -> $x, $y { ... };
Yes.
# process @a and @b in interleaved pairs (@a[n] and @b[n])
for zip(@a, @b) -> $x, $y { ... };
Yes.
# process in @a[0], @b[0], @a[1], @b[1], ... order
for zip(@a, @b) -> $x { ... };
Yes.
# triples from @a, @b and @c, alternately
for zip(@a, @b, @c, 3) -> $x, $y, $z { ... };
I've suggested:
for zip(@a, @b, @c, by=>3) -> $x, $y, $z { ... };
# triples from @a, pairs from @b, together
for weave( @a => 3, @b => 2) -> $v, $w, $x, $y, $z { ... };
I've suggested:
for zip( @a => 3, @b => 2) -> $v, $w, $x, $y, $z { ... };
It seems the "real" definition for 'for' is something like:
sub for(@a is rw, &s) {
my $numargs = &s.signature().length;
s(@a[0..$numargs-1]);
for(@a[$numargs...], &s);
}
Close. That signature really needs to be something like:
sub for(*@a is rw, &s);
And the implementation certainly won't be recursive (though it's
an elegant way to describe the algorithm ;-)
and zip(), weave(), or equivalents would be array generators.
Yes. Except, I don't believe we need C<weave>.
This would mean that things like:
sub printproducts($x, $y) { print $x*$y, "\n"; }
for zip(@multiplicands, @multipliers), &printproducts;
>
> and it would print one product for each pair of factors.
Yes. And this too:
for zip(@multiplicands, @multipliers) { print $^x*$^y, "\n"; }
Hmm, what (if anything) would this do, modulo minor syntax errors:
my $twoByTwo = -> ($x, $y) {
print "Noah led a $x and a $y onto the ark\n"; }
my $threeByThree = -> ( $x, $y, $z) {
print "Shem led a $x, a $y, and a little baby $z off of the ark\n"; }
my $arksubs = $twoByTwo | $threeByThree;
for @a, pick($arksubs);
It picks either the 2-by-2 state or the 3-by-3 state of the
$arksubs junction before the loop begins and then iterates
through @a accordingly.
BTW, that last line should probably use either:
pick($arksubs:)
or:
$arksubs.pick
Damian