Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

'for' clarification, summary...

5 views
Skip to first unread message

Buddha Buck

unread,
Nov 5, 2002, 11:34:40 AM11/5/02
to perl6-l...@perl.org
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);


Damian Conway

unread,
Nov 5, 2002, 6:09:19 PM11/5/02
to perl6 >> "perl6-language@perl.org"
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

0 new messages