develooper Front page | perl.perl6.language | Postings from October 2005

Re: Look-ahead arguments in for loops

Thread Previous | Thread Next
From:
Luke Palmer
Date:
October 1, 2005 13:22
Subject:
Re: Look-ahead arguments in for loops
Message ID:
7ca3f01605100113225737c9ac@mail.gmail.com
On 10/1/05, John Macdonald <john@perlwolf.com> wrote:
> I forget what the final choice was for syntax for the reduce
> operator (it was probably even a different name from reduce -
> that's the APL name), but it would be given a list and an
> operator and run as:
>
>     my $running = op.identity;
>     $running = $running op $_ for @list;
>
> So, to get a loop body that knows the previous value, you
> define an operator whose identity is the initial value of the
> list and reduce the rest of the list.

And that was never quite resolved.  The biggest itch was with
operators that have no identity, and operators whose codomain is not
the same as the domain (like <, which takes numbers but returns
bools).

Anyway, that syntax was

    $sum = [+] @items;

And the more general form was:

    $sum = reduce { $^a + $^b } @items;

Yes, it is called reduce, because "foldl" is a miserable name.

Luke

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