--0015175cb9d272e59e04a387fc4f
Content-Type: text/plain; charset=UTF-8

Hi,

Most of the `Enumerable` methods can be used for fiber based generators if
`Fiber` is made enumerable.

class Fiber
  include Enumerable
  def each
    loop { yield self.resume }
  end
end

def fib_gen
  Fiber.new {
    a, b  , 1
    while true
      Fiber.yield a
      a, b  , a + b
    end
  }
end

# Find the fibonacci number greater than 1000
fib_gen.find {|x| x > 1000 }
# take first 10 fibonacci numbers
fib_gen.take 10
# take_while numbers are smaller than 1000
fib_gen.take_while {|x| x < 1000 }

If it could be the default behavior, it would be very useful. Some methods
won't be applicable, particularly for non-terminating generators in their
default incarnation viz. drop_while. It can either be re-written to advance
the generator using Fiber#resume or it can be left upto the user to handle
infinite sequences correctly.

If I understand correctly, writing generators was one of the purposes of
fibers. Why can't fibers be made enumerable by default?

--0015175cb9d272e59e04a387fc4f--