Here's my current understanding of what's under discussion for for-loops:
Larry wants to eliminate the ; from the RHS of the ->, so the only thing
for needs to know about the RHS is the number and types of the
arguments. This puts the specification about how to generate those
arguments on the LHS of the ->, and only there.
Examples:
# process @array, one element at a time
for @array -> $x { ... };
# process @array, in pairs
for @array -> $x, $y { ... };
# process all of @a, then all of @b, one element at a time
for @a, @b -> $x { ... };
# process @a, then @b, in pairs (@a[0] and @a[1] first, etc)
for @a, @b -> $x, $y { ... };
# process @a and @b in interleaved pairs (@a[n] and @b[n])
for zip(@a, @b) -> $x, $y { ... };
# process in @a[0], @b[0], @a[1], @b[1], ... order
for zip(@a, @b) -> $x { ... };
# triples from @a, @b and @c, alternately
for zip(@a, @b, @c, 3) -> $x, $y, $z { ... };
# triples from @a, pairs from @b, together
for weave( @a => 3, @b => 2) -> $v, $w, $x, $y, $z { ... };
What is undecided is the exact name and syntax of zip() and weave().
(Note: in my sample syntax, zip(@a, @b, @c, ...) is the same as
weave(@a=>1, @b=>1, @c=>1,...) and zip(@a, @b, @c, ..., n) is the same
as weave (@a=>n, @b=>n, @c=>n, ...).
My feeling:
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);
}
and zip(), weave(), or equivalents would be array generators.
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.
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);
- Re: 'for' clarification, summary... Buddha Buck
- Re: 'for' clarification, summary... Damian Conway