Feature #6552

Enumerator::Generator:select should return another Enumerator::Generator

Added by Eike Dierks over 3 years ago. Updated about 3 years ago.



The current implementation of Enumerator:select fails when applied to an open ended Enumerator:Generator,
resulting in an endless loop.

In the current implementation, :select seems to collect all values from the enumerator before applying the selection.
If that Enumeration is not bound, this results in an endless loop.

Instead, applying :select to a Generator should be applied to each object in turn and should itself return a Enumerator::Generator.

For example to select the first 5 even numbers starting at 123 should work as:
but this currently results in an endless loop.

The same problem applies for :map and some other operators on Enumerations

However changing :select and :map to make them work with open ended Enumerations
might also change the api contract as they are currently defined to return an array.

We might want to have a look at the SICSP on streams,

I came up with this prototype:

class Enumerator
# return a generator for all elements from enumeration where block returns true
def select &block
Enumerator.new do |y|
self.each do |obj|
if block.call(obj)

Let's wrap an open ended Range in a Generator:
e=Enumerator.new{|y| (123..Float::INFINITY).each{|n|y< [124, 126, 128, 130, 132]


Enumerator_Generator_select.rb Magnifier - sample (806 Bytes) Eike Dierks, 06/07/2012 06:47 AM


#1 Updated by Innokenty Mikhailov over 3 years ago

You might be interested in Enumerator::Lazy that effectively solves you problem.
It's in trunk already and will be available in ruby 2.0

#2 Updated by Shyouhei Urabe over 3 years ago

  • Status changed from Open to Feedback

#3 Updated by Yusuke Endoh about 3 years ago

  • Status changed from Feedback to Rejected

I agree with gregolsen. No feedback. Closing.

Yusuke Endoh mame@tsg.ne.jp

Also available in: Atom PDF