On Wed, May 27, 2009 at 02:21:40PM -0400, Mark J. Reed wrote: > On Wed, May 27, 2009 at 1:59 PM, Daniel Carrera < > daniel.carr...@theingots.org> wrote: > > Wow... That's a foldl! In a functional language, that would be called a > > fold. > > In Haskell it may be called fold (well, foldl and foldr), but the concept > has has a variety of names. Two of the more common ones are "reduce" and > "inject"; I believe Perl6 chose "reduce" for consistency with the Perl5 > List::Util module. Common Lisp and Python also call it "reduce": > > (defun ! (n) > (reduce #'* (loop for i from 1 to n collecting i))) > > > def fact(n): > return reduce(lambda x,y: x*y, range(1,n+1)) > > > While Ruby calls it "inject". > > > def fact(n) > (1..n).inject { |x,y| x*y } > end > > > Perl 6 has a lot of functional features. IMO the nice thing about its > version of reduce is the way it's incorporated into the syntax as a > metaoperator.
Historically, the name reduce was used (first?) in APL, which also provided it as a meta-operator. op/ would use op to reduce the array on the right of the meta-operator. (Although, in APL, it could be an n-dimensional object, not necessarily a 2-dimensional array - the reduce would compute an (n-1)-dimensional object from it. This could be used to generate row-sums and column sums. APL was extremely terse, you could compute almost anything in a single line - Perl golfing afficionados have never really caught up, although with the addition of Unicode operators Perl 6 could now go ahead.)