S
Simon Strandgaard
I haven't tried but I've been eyeing it -- wanting to see what it can do. Can
you show some good examples? Or point us to a good place to see some?
There are a, yet small, collection of samples; here:
http://rubyforge.org/cgi-bin/viewcvs/cgi/viewcvs.cgi/projects/iterator/samples/?cvsroot=aeditor
Also my regexp-engine are using it (so far only in the 'iterator' branch):
http://rubyforge.org/cgi-bin/viewcv...ource/?cvsroot=aeditor&only_with_tag=iterator
Ruby's block concept has enormous power. The following examples
can be implemented even simpler by using block.
Where the difference lies are that Ruby's has internal-iterators,
and this package provides external-iterators.
At some point Ruby's iterator primitives are not sufficiently, Then you
have to use external-iterators. In my case I am working on a regexp
engine, which I want to take a wide range of different input (ascii,
utf-8, jis, ucs-4..etc).
If you want flexibility you might consider using external-iterators.
--
Simon Strandgaard
# demonstration of how to make an implicit iterator (fibonacci)
require 'iterator'
class FibonacciIterator < Iterator::Base
def initialize
super()
first
end
def first
@value = 1
@value_next = 1
end
def next
tmp, @value = @value, @value_next
@value_next += tmp
end
def is_done?; false end # infinity
def current; @value end
end
i = FibonacciIterator.new
p Array.copy_n(i, 10) #=> [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
i.close
# demonstration of how to use #transform
require 'iterator'
input = "heLLo woRLD".split(//)
i1 = input.create_iterator
i2 = input.create_iterator_end
dest = []
result = dest.create_iterator_end
Iterator::Algorithm.transform(i1, i2, result) {|val| val.upcase}
p dest.join #=> "HELLO WORLD"