What have you found for these years?

2011-02-14

ruby coroutine with fiber or enumerator

Fiber uses "yield" and "resume", while
Enumerator uses "yield" and "next".
You can "rewind" on Enumerator, but not on Fiber.

I think both Fiber and Enumerator should respond to call,
the former aliases resume to call, and the latter aliases next to call.

As you can see, they can be somehow identical in terms of interface.
full code: godfat/sandbox/ruby/coroutine.rb
FactFiber = lambda{
  Fiber.new{
          Fiber.yield(r  = x  = 1)
    loop{ Fiber.yield(r *= x += 1) }
  }
}

FactEnume = lambda{
  Enumerator.new{ |fiber|
          fiber.yield(r  = x  = 1)
    loop{ fiber.yield(r *= x += 1) }
  }
}
References:
* [Ruby 1.9-Feature#4326][Open] Fiber should respond to call() and []
* 1849. 01-29 是 parent 還是 child?

0 retries:

Post a Comment

All texts are licensed under CC Attribution 3.0