From: Brian Candler Date: 2009-11-11T00:47:26+09:00 Subject: Re: RCR enumerable extra into core Roger Pack wrote: > we currently have > list.map => Enumerator > > so should > list.map(:method) > > best return an array or an enumerator? The ultimate conclusion of this: all Enumerable methods should return other Enumerators. When you want a real array, then add .to_a to the end. > (though I don't really understand > why enumerators are cool, apparently they've become quite common lately) It's cool because evaluation takes place "left to right" instead of generating potentially huge intermediate arrays. You start getting answers sooner, you can write results out to disk or a socket without buffering, and you can deal with infinite lists, but using the same syntax. e.g. a = (1..1/0.0).select{ |i| i % 2 == 0 }.map{ |i| i + 100 }.take(10).to_a Implementing this turns out to be easier than you'd think, and is efficient. No funky Fibers or Continuations required. http://github.com/trans/facets/blob/master/lib/core/facets/denumerable.rb http://github.com/trans/facets/blob/master/lib/core/facets/enumerable/defer.rb -- Posted via http://www.ruby-forum.com/.