[ann] iterator 0.1

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"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,264
Latest member
FletcherDa

Latest Threads

Top