From: Yuh-Ruey Chen Date: 2008-11-02T08:04:41+09:00 Subject: Re: Iterator objects and lazy evaluation On Nov 1, 1:16 pm, Brian Candler wrote: > Yuh-Ruey Chen wrote: > > 1) Are there equivalents for iteration/enumeration functions like map > > that return iterator/enumeration objects (in a Python sense)? > > You can generate Enumerator objects, and in 1.9 (and I think 1.8.7), an > Enumberable method which hasn't been given a block returns an > Enumerator. > > irb(main):001:0> a = (1..10).each > => # > irb(main):002:0> a.next > => 1 > irb(main):003:0> a.next > => 2 > irb(main):004:0> a.next > => 3 > > However I don't think you can use these for chaining. It might be nice > if you could do something like > > (1..10).to_enum.map2 { |x| x * 2 }.map2 { |x| x + 1 }.map2 { |x| puts > x } > > which wouldn't create any intermediate array. I guess you'd need each > element to call 'next' on the previous one, and you'd need something at > the end to trigger the whole process off. > -- > Posted viahttp://www.ruby-forum.com/. Yes, this is exactly what I'm looking for - the ability to chain iterators like this without creating intermediate arrays, hopefully using less memory.