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

Re: Look-ahead arguments in for loops

Thread Previous | Thread Next
From:
Miroslav Silovic
Date:
October 3, 2005 07:38
Subject:
Re: Look-ahead arguments in for loops
Message ID:
4341184C.4050707@puremagic.com
lrpalmer@gmail.com wrote:

>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.
>
>  
>

To pick some nits, reduce and fold are different concepts. By 
definition, reduce doesn't take the initial value, while fold does.

So reduce using fold is something like

    @items || die "horribly";
    foldl &fn, @items[0], @items[1..]

... while fold using reduce is:

    reduce &fn, ($initial, @items)

I think both are useful, depending on the circumstances.


    Miro





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