"Simon Strandgaard" <neoneye / adslhome.dk> schrieb im Newsbeitrag
news:20040506120251.43669286.neoneye / adslhome.dk...
> homepage:
> http://raa.ruby-lang.org/list.rhtml?name=iterator
>
> download:
> http://rubyforge.org/frs/?group_id=18&release_id=467
>
>
> External iterators with some STL inspiration.
>
>
> #
> # example of how to do multiway each
> #
> require 'iterator'
> data_a = %w(a b c d)
> data_b = (0..3)
> ia = Iterator::Continuation.new(data_a, :each)
> ib = Iterator::Continuation.new(data_b, :each)
> result = []
> while ia.has_next? and ib.has_next?
>   result << ia.current
>   result << ib.current
>   ia.next
>   ib.next
> end
> ia.close
> ib.close
> p result   # ["a", 0, "b", 1, "c", 2, "d", 3]
>
>
>
>
> #
> # example of how to concat iterators
> #
> require 'iterator'
> ary1 = "Hell".split(//).to_a
> ary2 = "O wO".split(//).to_a
> ary3 = "rld!".split(//).to_a
> i1 = ary1.create_iterator
> i2 = ary2.create_iterator
> i3 = ary3.create_iterator
> iterator = Iterator::Concat.new([i1, i2, i3])
> ary2.map!{|i|i.swapcase}
> puts iterator.to_a.join  #-> Hello World!

Great!  This looks good.  I'd just do some small interface changes.

Move Array#create_iterator to Enumerable and rename it to
Enumerable#iterator (we're lazy typers) if possible.  Alternatively you
could provide this wrapper:

module Enumerable
  def iterator; Iterator::Continuation.new(self, :each); end
end

Make Iterator::Concat#initialize to be able to do
iterator = Iterator::Concat.new(i1, i2, i3) # brackets removed
iterator = Iterator::Concat.new(i1, ary2, i3)

Nice to have but maybe not worth the effort:
Iterator#reset

Regards

    robert