Lähettäjä: gabriele renzi <rff_rff@remove-yahoo.it>
Aihe: Re: Convert a Hash into an Arraylinus sellberg ha scritto:
> georgesawyer wrote:
>
>>> did I mention that I love #inject?
>>
>> (Wistfully) Even though 'inject' is the name in Smalltalk, I feel it were
>> better named, 'consolidate'.
>
>
> I don't have anything against #inject, but I could see the case for an
> alias #accumulate, much like SICP's accumulate which afaik is the same
> thing.
>
> (I wouldn't mind #reduce as well, though that would be slightly incorrect)#fold would be better imho (even if slightly incorrect)
Well, since #foldr and #foldl are not offered, it may be better to
call it #accum. On the other hand...
# ...if memory serves...
module Enumerable
def foldl(accum, sym)
self.each do |item| accum = accum.send(sym, item) end
end
def foldr(accum, sym)
self.reverse.each do |item| accum = item.send(sym, accum) end
end
end
[1,2,3,4,5].foldl 0, :-
=> -15 # (((((0 - 1) - 2) - 3) - 4) - 5)
[1,2,3,4,5].foldr 0, :-
=> 3 # (1 - (2 - (3 - (4 - (5 - 0)))))
[true, true, false, true].foldl true, :&
=> false # ((((true & true) & true) & false) & true)
E