Selasa, 29 Juni 2010

Reduce in Perl

Perl has grep/map/sort since probably forever (actually, sort() starts appearing since Perl 2.0). But even now, reduce is still not a builtin in Perl 5 (though available via List::Util), so doing reduce is probably not something that comes as naturally to Perl programmers. Meanwhile Ruby, JavaScript, and even PHP have their reduce operation builtin.

But then, reduce is "not really that useful" (you can just replace it with a simple for loop). So much that Python 3.0 now removes the function from the global namespace and reduces it (pun intended) to a mere member of functools. I guess reduce is really handy only if you are in a heavily functional language that lacks procedural basics.

This can be thought of as a testament to the level of language design skill that Larry has.

The rather funny thing is, in Perl 6, in addition to the reduce() List method there is also the reduce ([]) metaoperator as well.

3 komentar:

  1. I think reduce really is that useful and leaving it out was merely an oversight. That's probably true of everything in List::Util and some things in List::MoreUtils (e.g., zip). Viewed this way, it's not that funny that Perl 6 has these things; Larry is correcting these oversights.

    I guess that Python is going in the other direction suggests that you're right that languages don't need these operators, but I would rather use a language that has them.

    BalasHapus
  2. I agree with oylen, I miss reduce in core perl and am glad that it's a nice operator in perl 6.

    BalasHapus
  3. Language::Functional provides foldl and foldr. For some reason, I can remember what these means but not reduce.

    -- mst

    BalasHapus