S
Simon Strandgaard
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!
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!