How to stop iteration in a Enumerator::Lazy method?

Dir*_*urs 6 ruby enumerator lazy-evaluation

I am trying to implement a take_until method for Ruby 2's Enumerator::Lazy class. It should work similar to take_while but instead stop iteration when the yielded block returns true. The result should include the item where the yielded block matches.

My question is how do I signal that the end of the iteration is reached? When using regular Enumerators you can raise the StopIteration error in an each method to signal the end of the iterator. But that doesn't seem to work for lazy enum's:

class Enumerator::Lazy  
  def take_until
    Lazy.new(self) do |yielder, *values|
      yielder << values
      raise StopIteration if yield *values
    end
  end
end

(1..Float::INFINITY).lazy.take_until{ |i| i == 5 }.force
Run Code Online (Sandbox Code Playgroud)

I also tried to break out of the block to no effect. The documentation for Enumerator::Lazy doesn't seem to help either.

Why using take_while is not a valid option.

The main problem with take_while is that by its nature it will attempt to evaluate one more item than you need. In my application the Enumerator doesn't yield numbers, but messages fetched over the network. Trying to evaluate a message that is not there (yet?) is a blocking action which is highly undesirable. This is illustrated by the following contrived example:

enum = Enumerator.new do |y|
  5.times do |i|
    y << i
  end
  sleep
end

enum.lazy.take_while{ |i| i < 5 }.force
Run Code Online (Sandbox Code Playgroud)

To receive the first five items from this enumerator you will need to evaluate the sixth result. This is not as lazy as it could be. In my use case this is undesirable since the process would block.

Providing a pure Ruby implementation of take for Enumerator::Lazy

The standard library includes a take method that does something similar to what I want. It doesn't use a block as a condition but a number, but it does break out of the the iteration once that number is reached instead of evaluating one more item. Following on from the example above:

enum.lazy.take(5).force
Run Code Online (Sandbox Code Playgroud)

This does not get to the 6th item and so does not block. Problem is the version in the standard library is implemented in C and I can't seem to figure out how this could be implemented in pure Ruby. A ruby implementation of that method would be an acceptable answer.

Thanks in advance!

Den*_*rdy 0

根据我的评论,我认为修改take_while是更好的选择(或者至少是有效的选择):

(1..Float::INFINITY).lazy.take_while { |i| i < 6 }.force
=> [1, 2, 3, 4, 5]
Run Code Online (Sandbox Code Playgroud)

对于不太容易重写的更复杂的条件,添加一个变量:

found = false
(1..Float::INFINITY).lazy.take_while do |i|
  if i == 5
    found = true
  else
    !found
  end
end.force
=> [1, 2, 3, 4, 5]
Run Code Online (Sandbox Code Playgroud)

您也可以take_while基于最后一个块进行定义:

class Enumerator::Lazy
  def take_until
    take_while do |*args|
      if !@found
        @found = yield(*args)
        true
      else
        false
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

请注意,它也不会不必要地调用该块:

p (1..20).lazy.take_until{|i| p i; i == 5}.force
p (1..20).lazy.take_until{|i| p i; i == 3}.force
p (1..20).lazy.take_until{|i| p i; i == 8}.force
Run Code Online (Sandbox Code Playgroud)