Martin DeMello <martindemello / yahoo.com> wrote:
> Simon Strandgaard <neoneye / adslhome.dk> wrote:
> [snip example 2]
> This suggests "higher order" iterators composed of parallel and serial
> combinations of other iterators.
> 
> ic = Iterator::Parallel(ia, ib) # or even ic = ia | ib
> while ic.has_next?
>   a, b = ic.current
>   result << "#{a} -> #{b}"
> end
> ic.close
> 

Yes, I have a MultiIterator in the sample directory.
Though I never thought of naming it Parallel.. I will consider rename.
Maybe I should refine it (owner should close children) and make a full-blown 
iterator out of it ?

#
# example 4. parallel iterators
#
require 'iterator'
class MultiIterator < Iterator::Base
  def initialize(*iterators)
    @iterators = iterators
    first
  end
  def first; @iterators.each{|i| i.first} end
  def next; @iterators.each{|i| i.next} end
  def has_next?
    @iterators.each{|i| return true if i.has_next? }
    false
  end
  def current; @iterators end
end
a = %w(a b c d e).create_iterator
b = (0..4).to_a.create_iterator
i = MultiIterator.new(a, b)
while i.has_next?
  ia, ib = i.current
  p [ia.current, ib.current]
  i.next
end 
i.close
a.close
b.close


There is also Iterator::Concat

#
# example 5. concat 3 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! 


Thanks Martin for the Parallel name... I will add it to my todo list.

-- 
Simon Strandgaard