Re:My Objection to Array#sum
by nostrademons on Thursday April 09, 2009 @09:05PM
(#555490)
That's what Haskell does. Your universal joiner is called "fold", sometimes pronounced "reduce" (Scheme/Python) or "inject" (Smalltalk/Ruby). From the Haskell prelude:
sum = foldl (+) 0
product = foldl (*) 1
and = foldl (&&) True
or = foldl (||) False
any p = or . map p
all p = and . map p
concat = foldl (++) []
unlines = concat . map (++ "\n")
Google calls it "reduce" as well - this is what MapReduce is based upon.(BTW, bad search query: [haskell unlines] gives me the Haskell prelude definition as the last result on the first page, even though that's the authoritative source. It gives me the Zvon and Informatik mirrors as #1 and #2, and then lots of useless discussion. Couldn't there be a way to boost "authoritative" sources up the rankings?)
Yeah, in practice though I would rarely use reduce in python to do a sum/any/all, but now that they're in the language I like them. Before I would just spell it all out. It was just slightly too wordy to make it worth it, kind of like python lambdas in general.
It would be cool but probably perlishly unadvisable if join,sum,any,all were just implemented as