From: Robert Dober Date: 2009-06-23T17:45:06+09:00 Subject: Re: map shall not return an Enumerator ( was re guru help ) On Tue, Jun 23, 2009 at 3:32 AM, David A. Black wrote: > It would buy me coll.send_to_elements( :+ , 42 ) coll.send_to_elements.join( ", ") versus coll.map{ |x| x + 42 } coll.map{ |x| x.join( ", ") } it is not so much about typing (although nobody would forbid aliasing #send_to_elements) but about readability. But maybe I am reading too much about Clojure lately ;). >> Can you do this with lazy Enumerators? >> >> fibs = cons_stream( 0 ){ cons_stream( 1 ){ add_streams(fibs, fibs.tail) } >> } > > I can't do it that compactly (though maybe someone can). Here's some > doodling with the block form of an enumerator: > > fib = Enumerator.new do |y| > # y is a "yielder" object -- lazy yielding via << >  y << 1 > >  a,b = 0,1 >  loop do >    y << a + b >    a,b = b, a + b >  end > end > > p fib.next > p fib.next > p fib.next > p fib.next > p fib.next > > # Output: > 1 > 1 > 2 > 3 > 5 > > Or maybe something using cycle. Wouldn't Kernel#cons_stream be great for this expressiveness? BTW it does not necessarily mean that Enumerators should not be lazy, they could be the foundation of the "functional" interface of streams as I have of course a class hidden behind my "functional" implementation of streams. Cheers Robert -- Toutes les grandes personnes ont d’abord été des enfants, mais peu d’entre elles s’en souviennent. All adults have been children first, but not many remember. [Antoine de Saint-Exupéry]