[ann] iterator 0.1

homepage:
http://raa.ruby-lang.org/list.rhtml?name=iterator

download:
http://rubyforge.org/download.php/213/iterator-0.1.tar.gz

About

···

=====

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.

Status

The overall design are stable. I don’t expect any big changes.

Collection, iterates over an Array or similar containers.
Reverse, decorator which reverses the iterator.
Range, iterate in the range between two iterators.
Concat, concat multiple iterators into one.
Continuation, turn #each_word/#each_byte into an iterator.

License

Ruby’s license.

Example

We can virtually concatene two arrays, so that they appear
as they were a single array.

def test_concat_sort
	i1 = [5, 3, 1].create_iterator
	i2 = [2, 4, 6].create_iterator
	iterator = Iterator::Concat.new(i1, i2)
	assert_equal((1..6).to_a, iterator.sort)
end

The iterator base class looks like this.
Please comment on this interface. Is it OK?
Does it need anything?

class Iterator
# close iterator after usage.
def close

# set position at first element
def first

# goto next element
def next

# true if we have reached the end
def is_done?

# set position at last element
def last

# goto previous element
def prev

# get the value of current element
def current

# set the value of current element
def current=(value)

# iterate over all elements (forward)
def each

# iterate over all elements (backward)
def reverse_each

# create a reverse iterator
def reverse

# typical enum methods also
include Enumerable

end

Authors last words

We are curios to hear what your first-impression are, What do
you want from ‘iterator.rb’? What are your requirements?
Please contribute with extensions or suggestions. In case you
observe bugs or other misbehavier, then please drop us a mail.

Messages must be submitted via this web-forum (prefered way of contact):
http://rubyforge.org/forum/forum.php?forum_id=46


Simon Strandgaard

We are curios to hear what your first-impression are, What do
you want from ‘iterator.rb’? What are your requirements?
my first impression is that you must really, really love iterators :wink:
anyway, may be interesting to see how useful this is. this may be
something to integrate into (or other way around perhaps? :wink: RDSL
http://rubyforge.org/projects/rdsl/
nikolai

···


::: name: Nikolai Weibull :: aliases: pcp / lone-star / aka :::
::: born: Chicago, IL USA :: loc atm: Gothenburg, Sweden :::
::: page: www.pcppopper.org :: fun atm: gf,lps,ruby,lisp,war3 :::
main(){printf(&linux[“\021%six\012\0”],(linux)[“have”]+“fun”-97);}

iterator 0.2, just released.

I have some StandardTemplateLibrary inspired methods that operate
on iterators.

#copy
#copy_n
#copy_backward
#fill
#fill_n
#transform
#transform2

Also fixed fatal bug in Iterator::Continuation class.

BTW: who has tried this package? what do you think?

···

On Thu, 20 Nov 2003 14:52:39 +0100, Simon Strandgaard wrote:

homepage:
http://raa.ruby-lang.org/list.rhtml?name=iterator

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.


Simon Strandgaard

* Simon Strandgaard <qj5nd7l02@sneakemail.com> [Nov, 20 2003 17:40]:

We are curios to hear what your first-impression are, What do
you want from 'iterator.rb'? What are your requirements?

my first impression is that you must really, really love iterators :wink:
anyway, may be interesting to see how useful this is. this may be
something to integrate into (or other way around perhaps? :wink: RDSL

Well, the problem is that it exist something similar in 1.8.1

# == Overview

···

#
# This library provides the Generator class, which converts an
# internal iterator (i.e. an Enumerable object) to an external
# iterator. In that form, you can roll many iterators independently.

svg% grep 'def ' generator.rb
  def initialize(enum = nil, &block)
  def yield(value)
  def end?()
  def next?()
  def index()
  def pos()
  def next()
  def current()
  def rewind()
  def each
  def initialize(*enums)
  def size
  def length
  def end?(i = nil)
  def each
  def test_block1
  def test_block2
  def test_each
  def test_each
svg%

Guy Decoux

We are curios to hear what your first-impression are, What do
you want from ‘iterator.rb’? What are your requirements?
my first impression is that you must really, really love iterators :wink:

true

anyway, may be interesting to see how useful this is.
[snip]

The most interesting thing which I can think of right now,
where only iterators will do, are for my Robust-Array/iterator.

A Robust-iterator is a iterator which keeps pointing at the
same element, even though you insert/delete elements in the
same data structure. When insert/delete occur, all iterators
gets notified about the delta-change. AEditor needs it.

See implementation of robust-Array/iterator here:
http://rubyforge.org/cgi-bin/viewcvs/cgi/viewcvs.cgi/projects/experimental/iterator2/robust.rb?rev=1.2&cvsroot=aeditor&content-type=text/vnd.viewcvs-markup

See unittest of robust-Array/iterator here:
http://rubyforge.org/cgi-bin/viewcvs/cgi/viewcvs.cgi/projects/experimental/iterator2/test_robust.rb?rev=1.2&cvsroot=aeditor&content-type=text/vnd.viewcvs-markup

···

On Fri, 21 Nov 2003 03:04:48 +0900, Nikolai Weibull wrote:


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?

-t0

···

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?

A certain level of convergence is, in my opinion, a good sign.

Regards,

Mark

···

On Nov 20, 2003, at 1:11 PM, ts wrote:

We are curios to hear what your first-impression are, What do
you want from ‘iterator.rb’? What are your requirements?
my first impression is that you must really, really love iterators
:wink:
anyway, may be interesting to see how useful this is. this may be
something to integrate into (or other way around perhaps? :wink: RDSL

Well, the problem is that it exist something similar in 1.8.1

[snip]

We are curios to hear what your first-impression are, What do
you want from ‘iterator.rb’? What are your requirements?
my first impression is that you must really, really love iterators :wink:
anyway, may be interesting to see how useful this is. this may be
something to integrate into (or other way around perhaps? :wink: RDSL

You are welcome to use ‘iterator.rb’ in RDSL… However ‘iterator.rb’ are
under Ruby license and RDSL are under LGPL. I think you can make a
vendor-branch, and import ‘iterator.rb’ into your cvs-repository:
http://www.loria.fr/~molli/cvs/doc/cvs_13.html#SEC98

Well, the problem is that it exist something similar in 1.8.1

== Overview

This library provides the Generator class, which converts an

internal iterator (i.e. an Enumerable object) to an external

iterator. In that form, you can roll many iterators independently.

I wasn’t aware of ‘generator.rb’. The ‘Generator from a block’ idea
is good, I will rip it.

I started working on the iterator before Ruby-1.8.1 were released,
because I needed it for my regexp-engine in order to iterate over many
different kinds of input-streams. Also because I needed a robust-iterator.
‘generator.rb’ cannot replace ‘iterator.rb’.

I originally tried out Horst Duchene’s ‘stream’ package, but found it too
counter-intuitive. I don’t believe there are other iterator packages ?

···

On Fri, 21 Nov 2003 03:11:28 +0900, ts wrote:


Simon Strandgaard

“ts” decoux@moulon.inra.fr schrieb im Newsbeitrag
news:200311201811.hAKIBOa26104@moulon.inra.fr

We are curios to hear what your first-impression are, What do
you want from ‘iterator.rb’? What are your requirements?
my first impression is that you must really, really love iterators
:wink:
anyway, may be interesting to see how useful this is. this may be
something to integrate into (or other way around perhaps? :wink: RDSL

Well, the problem is that it exist something similar in 1.8.1

Seems like I have to upgrade:

09:23:31 [w]: irb -r generator
/usr/lib/ruby/1.8/irb/init.rb:215:in require': /cygdrive/w/lib/ruby/generator.rb:33: unterminated string meets end of file (SyntaxError) /cygdrive/w/lib/ruby/generator.rb:33: syntax error from /usr/lib/ruby/1.8/irb/init.rb:215:in load_modules’
from /usr/lib/ruby/1.8/irb/init.rb:213:in each' from /usr/lib/ruby/1.8/irb/init.rb:213:in load_modules’
from /usr/lib/ruby/1.8/irb/init.rb:21:in setup' from /usr/lib/ruby/1.8/irb.rb:54:in start’
from /usr/bin/irb:13
09:23:36 [w]: ruby -v
ruby 1.8.0 (2003-08-04) [i386-cygwin]
09:23:38 [w]: uname -a
CYGWIN_NT-5.0 bond 1.5.5(0.94/3/2) 2003-09-20 16:31 i686 unknown unknown
Cygwin
09:23:42 [w]:

robert

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.

···

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?


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”

Seems like I have to upgrade:

09:23:31 [w]: irb -r generator

yes,

nasun% ruby -v
ruby 1.8.1 (2003-10-31) [sparc-solaris2.8]
nasun%

nasun% irb -r generator
irb(main):001:0> g = Generator.new(['A', 'B', 'C', 'Z'])
=> #<Generator:0x1002f9830 @cont_next=nil, @queue=["A"], @cont_endp=nil, @index=0, @block=#<Proc:0x0000000100329b20@/opt/sparcv9/lib/ruby/1.8/generator.rb:69>, @cont_yield=#<Continuation:0x1002f94e8>>
irb(main):002:0> puts g.next while g.next?
A
B
C
Z
=> nil
irb(main):003:0> nasun%

Guy Decoux

where did ‘svg%’ go ? :wink:

···

il Fri, 21 Nov 2003 19:33:27 +0900, ts decoux@moulon.inra.fr ha scritto::

nasun% ruby -v

also, what happened to ‘pigeon>’ ?

···

On Fri, 21 Nov 2003 20:27:40 +0000, gabriele renzi wrote:

il Fri, 21 Nov 2003 19:33:27 +0900, ts decoux@moulon.inra.fr ha
scritto::

nasun% ruby -v

where did ‘svg%’ go ? :wink:


Simon Strandgaard

You have forgotten condor% [ruby-talk:25826], aestivum% [ruby-talk:47494]

Guy Decoux

···

On Fri, 21 Nov 2003 20:27:40 +0000, gabriele renzi wrote:

il Fri, 21 Nov 2003 19:33:27 +0900, ts <decoux@moulon.inra.fr> ha
scritto::

nasun% ruby -v

where did 'svg%' go ? :wink:

also, what happened to 'pigeon>' ?

poisoned in the park?

martin

···

Simon Strandgaard neoneye@adslhome.dk wrote:

On Fri, 21 Nov 2003 20:27:40 +0000, gabriele renzi wrote:

il Fri, 21 Nov 2003 19:33:27 +0900, ts decoux@moulon.inra.fr ha
scritto::

nasun% ruby -v

where did ‘svg%’ go ? :wink:

also, what happened to ‘pigeon>’ ?

mh… I didn’t even knew ruby at that time I suppose :slight_smile:

···

il Sun, 23 Nov 2003 02:12:46 +0900, ts decoux@moulon.inra.fr ha scritto::

You have forgotten condor% [ruby-talk:25826], aestivum% [ruby-talk:47494]

“Martin DeMello” martindemello@yahoo.com schrieb im Newsbeitrag
news:vubwb.137096$jy.117581@clgrps13…

···

Simon Strandgaard neoneye@adslhome.dk wrote:

On Fri, 21 Nov 2003 20:27:40 +0000, gabriele renzi wrote:

il Fri, 21 Nov 2003 19:33:27 +0900, ts decoux@moulon.inra.fr ha
scritto::

nasun% ruby -v

where did ‘svg%’ go ? :wink:

also, what happened to ‘pigeon>’ ?

poisoned in the park?

You mean, Georg Kreisler was there?

http://www.metrolyrics.com/lyrics/121810/Georg_Kreisler/Tauberl_Vergiften/
http://www.br-online.de/alpha/forum/vor0311/20031123_i.shtml

Note: you should be German speaking to enjoy this song text…

:wink:

robert

Robert Klemme wrote:

“Martin DeMello” martindemello@yahoo.com schrieb im Newsbeitrag
news:vubwb.137096$jy.117581@clgrps13…

il Fri, 21 Nov 2003 19:33:27 +0900, ts decoux@moulon.inra.fr ha
scritto::

nasun% ruby -v

where did ‘svg%’ go ? :wink:

also, what happened to ‘pigeon>’ ?

poisoned in the park?

You mean, Georg Kreisler was there?

http://www.metrolyrics.com/lyrics/121810/Georg_Kreisler/Tauberl_Vergiften/
http://www.br-online.de/alpha/forum/vor0311/20031123_i.shtml

Note: you should be German speaking to enjoy this song text…

:wink:

Fascinating… this appears to be a translation of the original
song (in English) by Tom Lehrer.

I guess people everywhere are pretty much the same… :wink:

Hal

···

Simon Strandgaard neoneye@adslhome.dk wrote:

On Fri, 21 Nov 2003 20:27:40 +0000, gabriele renzi wrote:

“Hal Fulton” hal9000@hypermetrics.com schrieb im Newsbeitrag
news:3FC1B89D.5080908@hypermetrics.com

Robert Klemme wrote:

“Martin DeMello” martindemello@yahoo.com schrieb im Newsbeitrag
news:vubwb.137096$jy.117581@clgrps13…

il Fri, 21 Nov 2003 19:33:27 +0900, ts decoux@moulon.inra.fr ha
scritto::

nasun% ruby -v

where did ‘svg%’ go ? :wink:

also, what happened to ‘pigeon>’ ?

poisoned in the park?

You mean, Georg Kreisler was there?

http://www.metrolyrics.com/lyrics/121810/Georg_Kreisler/Tauberl_Vergiften/

http://www.br-online.de/alpha/forum/vor0311/20031123_i.shtml

Note: you should be German speaking to enjoy this song text…

:wink:

Fascinating… this appears to be a translation of the original
song (in English) by Tom Lehrer.

How old is that one?

I guess people everywhere are pretty much the same… :wink:

Pigeons, too. :slight_smile:

robert
···

Simon Strandgaard neoneye@adslhome.dk wrote:

On Fri, 21 Nov 2003 20:27:40 +0000, gabriele renzi wrote:

“Hal Fulton” hal9000@hypermetrics.com schrieb im
Newsbeitrag news:3FC1B89D.5080908@hypermetrics.com

Robert Klemme wrote: > “Martin DeMello”
martindemello@yahoo.com schrieb im Newsbeitrag >
news:vubwb.137096$jy.117581@clgrps13…

il Fri, 21 Nov 2003 19:33:27 +0900, ts
decoux@moulon.inra.fr ha >>>>scritto::

nasun% ruby -v

where did ‘svg%’ go ? :wink:

also, what happened to ‘pigeon>’ ?

poisoned in the park?

You mean, Georg Kreisler was there?

http://www.metrolyrics.com/lyrics/121810/Georg_Kreisler/Tauberl_Vergiften/

http://www.br-online.de/alpha/forum/vor0311/20031123_i.shtml

Note: you should be German speaking to enjoy this song
text…

:wink:

Fascinating… this appears to be a translation of the original
song (in English) by Tom Lehrer.

> How old is that one?

Released in 1959 on the album “More of Tom Lehrer”.

d.k.

···

Simon Strandgaard neoneye@adslhome.dk wrote:

On Fri, 21 Nov 2003 20:27:40 +0000, gabriele renzi wrote: