From: Simon Strandgaard Date: 2003-11-25T04:27:13+09:00 Subject: Re: [ann] iterator 0.1 On Tue, 25 Nov 2003 02:39:34 +0900, T. Onoma wrote: > On Monday 24 November 2003 05:42 pm, Simon Strandgaard wrote: >> > If Ryby's native iterators (yield+each) ain't flexible enough, >> > you may want to try using this collection of bidirectional >> > iterator classes. Building custom iterator classes are simple. >> > This is a simple implementation of GoF iterator-pattern. >> >> BTW: who has tried this package? what do you think? > > 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/viewcvs/cgi/viewcvs.cgi/projects/regexp_engine/source/?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"