From: Brian Candler Date: 2008-11-03T03:37:52+09:00 Subject: Re: ruby1.9: lazy versions of Enumerator#select and friends? David A. Black wrote: > It did have something like this, but not any more. I'm not sure they > were identical to what you've written, but the basic idea of an > enumerator that bundled itself with a block did exist in 1.9 for a > while. I'd be interested to learn exactly what the dropped feature was. Perhaps we're talking at cross purposes, but I'm not sure I'm suggesting "an enumerator that bundled itself with a block". Rather, I'm suggesting that certain methods in Enumerable which used to return an array, instead return an Enumerator, so that they can be chained horizontally. By this I mean: a.meth1 { ... }.meth2 { ... }.meth3 { ... } currently runs "vertically" (meth1 enumerates all of 'a' and creates an array; meth2 enumerates this array and creates another array; then meth3 enumerates this array and creates a final array) By running "horizontally" I mean that the first element of 'a' is processed all the way across; then the second element of 'a'; and so on. No intermediate arrays are created. > I'd strongly go for #1. #2 would require not only massive code > overhaul, but change of habits, and lots of to_a is unsightly (and you > wouldn't necessarily want to have to hard-code the class of what you > want back, anyway [i.e., the 'a' in 'to_a']). I don't understand the last sentence - a whole bunch of Enumerable methods like #map, #select etc are already hard-coded to return an Array, so you currently have no choice. With what I propose you can use any method you like to build the result. If you don't want an array, then don't use to_a. You could use 'inject' or 'each' or 'each_with_object' to gather the resulting elements in whatever way you like: e.g. a.meth1 { ... }.meth2 { ...}.each { |x,y| h[x] = y } Or even, as the original example I posted shows, you can just use the results as they arrive and then discard them: a.meth1 { ... }.meth2 { ...}.each { |x| puts x } Now that Enumerators exist, the fact that Enumerable methods are hard-coded always to create an Array seems very limiting. There are so many other things you might want to create instead. -- Posted via http://www.ruby-forum.com/.