From: Robert Klemme Date: 2013-03-26T16:40:18+09:00 Subject: Re: Choice of methods/implementation (was: Writing a game, trying to make dynamic loops) On Tue, Mar 26, 2013 at 6:11 AM, tamouse mailing lists wrote: > On Mon, Mar 25, 2013 at 5:04 PM, Robert Klemme > wrote: >> I don't think this calls for #inject / #reduce. IMHO #map is a better >> choice here since you do not need to explicitly deal with the Array >> copied into. > > Very good point. I may have started thinking of #reduce as a big > hammer that can be used everywhere. :) A while ago I went through that phase as well... :-) On Tue, Mar 26, 2013 at 6:20 AM, tamouse mailing lists wrote: > On Mon, Mar 25, 2013 at 7:03 PM, Henry Maddocks wrote: >> I like to think of 'reduce' meaning you have several things that you want to >> reduce to a single thing whereas with 'map' you are mapping one thing to >> another thing, e.g. mapping integers to players. Yeah, but in this case mapping one collection onto another one is a superior abstraction because it forces one to do less mechanics. :-) > I do probably overuse #reduce, as I find those things elegant. It is. Basically you can implement almost all methods of Enumerable with #inject. I think I did this once for fun. > But I > do sometimes forget about the #map part of the Map Reduce pattern. I'm > getting better though! I recently recoded something from this: > > names = items.reduce([]) {|m,i| m << i.name} > > to this: > > names = items.map(&:name) Thumbs up! Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/