From: Kristof Bastiaensen Date: 2004-06-20T02:08:30+09:00 Subject: Re: [RCR] More enumerator functionality On Sat, 19 Jun 2004 19:28:28 +0900, Michael Neumann wrote: > On Fri, Jun 18, 2004 at 10:13:25PM +0900, Kristof Bastiaensen wrote: >> On Fri, 18 Jun 2004 21:28:45 +0900, Michael Neumann wrote: >> >> [...] >> >> > >> > That's a more general form, as after the "lazy", all Enumerable >> > operations do not create intermediate arrays. Of course, it's very slow >> > compared to the non-lazy methods (Generator uses continuations). >> > >> > >> That's interesting. >> If I am correct, your collect_lazy and lazy.collect behaves the same as >> enum_for with a block. Your select_lazy and lazy.select the same as >> enum_if. > > I don't know what enum_for is doing, but select_lazy is the same as > enum_if, only it's implementation is very different. > Currently enum_for (and its alias to_enum) creates an enumerable that uses a different method than each. For example: require "enumerator" str = "xyz" enum = str.enum_for(:each_byte) a = enum.map {|b| '%02x' % b } #=> ["78", "79", "7a"] My proposal is that enum_for can take a block, so it can do a custom transformation on the data: data = [2, 3, 6] powers = data.enum_for { |i| i * i } #"each" implied powers.to_a => [4, 9, 36] data << 7 powers.to_a => [4, 9, 36, 49] If I am not mistaken this is the same as your collect_lazy. Regards, Kristof