From: "David A. Black" Date: 2004-04-06T02:43:04+09:00 Subject: Re: deciding between ruby and python Hi -- On Mon, 5 Apr 2004, Charles Comstock wrote: > David A. Black wrote: > > > for doesn't generate a result list though. What you've got here will > > return [3,2,1], and all the x+2 calculations will be discarded (like > > #each). > > Hmm I had misunderstood that about each, I encountered it once, but > promptly forgot about it. Do we do this to increase efficiency? > Otherwise I can't see a situation where you wouldn't want to return the > results of each for iteration. There's definitely an efficiency issue; #each only has to keep the current value in memory, whereas #map has to create a new array. We definitely want a way to iterate through things without creating a result array in memory (think about going through the lines of a multi-gigabyte file, for instance). Also, #map and #each tend to do different things. For example: arr.each {|e| p "Current element: #{e}"} If you map'd that, you'd end up with an array of nils (the return value of #p), which would be pretty pointless. So it's a mixture of efficiency ("low impact" iterating) and somewhat different applications. David -- David A. Black dblack@wobblini.net