From: Robert Klemme Date: 2005-09-16T00:51:35+09:00 Subject: Re: Math: sum and faculty Martin DeMello wrote: > Robert Klemme wrote: >> >> To sum up, #sum seems too specialized. #inject is there and does >> the job quite nicely - and easily. > > Actually, something in between would be quite nice in terms of code > aesthetics. I'm thinking of the specialised but very common case of a > binary accumulator and a unary function, thus (calling it 'accumulate' > for want of a better name): > > sum = enum.accumulate(0, :+) {|x| f(x)} The difference isn't really that big, is it? sum = enum.inject(0) {|s,x| s + f(x)} > collect = enum.accumulate([], :<<) {|x| f(x)} I prefer collect = enum.map {|x| f(x)} collect = enum.collect {|x| f(x)} > and if you leave off the block, it simply applies f(x) = x. It avoids > cluttering the block with the accumulator the way inject does. Personally I don't find the "s +" to be cluttering the block. At least not enough to justify adding this to Enumerable. My 0.02EUR... Kind regards robert