develooper Front page | perl.perl5.porters | Postings from June 2021

Re: RFC: Multiple-alias syntax for for

Thread Previous | Thread Next
From:
Dave Mitchell
Date:
June 22, 2021 15:03
Subject:
Re: RFC: Multiple-alias syntax for for
Message ID:
YNH7y6j/FRWVm9Da@iabyn.com
On Tue, Jun 08, 2021 at 11:20:57AM +0000, Nicholas Clark wrote:
> About two months ago Rik had mentioned to me the idea of implementing this
> (currently illegal) syntax to iterate over hashes:
> 
>     for my ($key, $value) (%hash) { ... }
> 
> which isn't actually hash specific - it generalises to N-at-a-time over any
> list.

Just throwing this out as a possible further/later extension of this idea:
allow for multiple lists, separated by semicolons, to be iterated over in
parallel, to provide a more general N-from-M iteration mechanism.

This could already be done by using a List::Util::zip() type function,
but this way would be built-in so more efficient, and doesn't involve
creating a temporary list.

So given:

    @a = (1,2,3,4,5); @b = (11,22,33,44,55); @c = (111,222,333,444,555);

consider

    for my ($x, $y, $z) (@a; @b; @c) { print "($x,$y,$z) " }
    # outputs (1,11,111) (2,22,222) ... (5,55,555)

and

    for my $x (@a; @b; @c) { print "$x " }
    # outputs 1 11 111 2 22 222  ...  5 55 555

and

    for my ($x, $y) (@a; @b; @c) { print "($x,$y) " }
    # outputs (1,11) (111,2) (22,222) ... (5,55) (555,<undef>)

and

    # just to make clear that it's 3 lists, not 3 arrays;
    for my $x ((7,@a); (8,@b); (9,@c)) { print "$x " }
    # outputs 7 8 9 1 11 111 2 22 222  ...  5 55 555

Obviously it would do suitable injecting of undef final values for differing
list lengths etc.

This doesn't clash with the existing

    for (init; cond; iterate) { ...}

syntax, since that doesn't have an iterator variable.

Anyway, I'm not particularly championing this suggestion; just mentioning
in passing as I process through my backlog of several month's unread
p5p/github emails.


-- 
This is a great day for France!
    -- Nixon at Charles De Gaulle's funeral

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About