[sf-perl] reduce-like function in Perl?

Chris Mungall cjm at fruitfly.org
Tue Apr 26 16:09:01 PDT 2005


You might want to check out Language::Functional

http://search.cpan.org/~lbrocard/Language-Functional-0.03/Functional.pm

The equivalent function is 'foldl' or 'foldr'

I don't know if there's anyway to pass around '+' as a function - you'll
have to define your own 'plus' function

you might want to check out fp::functionals too

functional programming rules!!

On Tue, 26 Apr 2005, Chris Palmer wrote:

> Say I have a list of numbers, and I want to add them all up in one go.
> Is there a reduce-like function (see Python) to do this?
>
>     # hypothetical
>     $total = reduce(+, @numbers);
>
> I can do this, but it's aesthetically displeasing (better than foreach
> at least):
>
>     $total = 0;
>     map { $total += $_ } @numbers;
>
> Also, what's the very fastest way to sum a bunch of numbers? I have read
> hunks of *Mastering Algorithms with Perl*, and it has good performance
> anecdotes, but I don't have it here with me now...
>
> Thanks!
>
> _______________________________________________
> SanFrancisco-pm mailing list
> SanFrancisco-pm at pm.org
> http://mail.pm.org/mailman/listinfo/sanfrancisco-pm
>


More information about the SanFrancisco-pm mailing list