From: MonkeeSage Date: 2007-11-29T02:35:14+09:00 Subject: Re: Ruby needs continuations... On Nov 28, 9:41 am, Charles Oliver Nutter wrote: > MonkeeSage wrote: > > You actually need a generator, which can be implemented with > > continuations, but doesn't have to be (in 1.9 the generators are > > implemented with threads -- have a look at generator.rb in 1.8 and 1.9 > > to see the differences). Using generators, you can implement izip (and > > all of itertools if you'd like, though most of it already has > > equivalent ruby solutions). Here is a pretty close translation of the > > python given in the itertools docs: > > Ruby 1.9 also moves continuations out of core, but provides a form of > bounded continuations (coroutines) that's possible to implement in JRuby: > > require 'fiber' > f = Fiber.new { a = 1; while true; Fiber.yield a; a += 1; end } > 5.times { puts f.resume } > > $ jruby -J-Djruby.compat.version=ruby1_9 fiber_example.rb > 1 > 2 > 3 > 4 > 5 > > Under JRuby, it's using a native thread per live Fiber, so it's heavier > than in Ruby 1.9 which uses green threads. However they'll actually run > in parallel on JRuby, so that's a benefit. JRuby also supports a thread > pool in 1.1 that helps blunt the cost of spinning up native threads. > > - Charlie I just tried the above izip function in jruby, but it only gave me two iterations... $ jruby test.rb 1 4 2 5 ...hmm? $ jruby -v ruby 1.8.5 (2007-11-28 rev 3841) [i386-jruby1.0] Regards, Jordan