--J/dobhs11T7y2rNN
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

On Fri, Mar 11, 2005 at 11:13:54PM +0900, Luke Graham wrote:
> More or less compliant with the generator.rb api, which I like.
> Some proof that the thing works, too.
> 
> module Enumerable
> 	attr_accessor :co, :st, :res, :sto
> 	

(snip)

> end

I also wrote some lightweight generator, before I knew that there is one
in the std lib.

Now I see that it's about the same speed as your Enumerable extension,
ie., they are cca. 1.5 times faster than the official one.

This is enough reason to show it... it converts an iterator method to a
generator. Eg.:

 require 'iter2gener'

 a  0...3000).to_a
 g  2G.new a.method(:each)
 while enext; p e; end

You have some further options, eg, you can set what to do when iterator
is exhausted.

Csaba

--J/dobhs11T7y2rNN
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="iter2gener.rb"

class I2G
  def initialize(i,*a,&b)
    if i.is_a? Array 
      @iter,@final  [0],i[1]
    else
      @iter,@final  ,proc{|x|}
    end
    @args  
    @co  il
    @trans  b ? b : proc{|x| x})
  end
  def now
    @curr
  end
  def call
    callcc { |@curr|
      @co ? @co[] :
      (rv  iter.call(*@args) { |x| callcc{ |@co| now[@trans[x]] }}; now[@final[rv]])
    }
  end
  alias [] call
  alias next call
end

--J/dobhs11T7y2rNN--