[ann] iterator 0.1

S

Simon Strandgaard

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
 
N

Nikolai Weibull

* Simon Strandgaard said:
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 ;-)
anyway, may be interesting to see how useful this is. this may be
something to integrate into (or other way around perhaps? ;-) RDSL
http://rubyforge.org/projects/rdsl/
nikolai
 
T

ts

N> my first impression is that you must really, really love iterators ;-)
N> anyway, may be interesting to see how useful this is. this may be
N> something to integrate into (or other way around perhaps? ;-) 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
 
M

Mark Wilson

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

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

[snip]

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

Regards,

Mark
 
S

Simon Strandgaard

N> my first impression is that you must really, really love iterators ;-)
N> anyway, may be interesting to see how useful this is. this may be
N> something to integrate into (or other way around perhaps? ;-) 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 ?
 
S

Simon Strandgaard

my first impression is that you must really, really love iterators ;-)
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/viewcv...=aeditor&content-type=text/vnd.viewcvs-markup

See unittest of robust-Array/iterator here:
http://rubyforge.org/cgi-bin/viewcv...=aeditor&content-type=text/vnd.viewcvs-markup
 
R

Robert Klemme

ts said:
N> my first impression is that you must really, really love iterators ;-)
N> anyway, may be interesting to see how useful this is. this may be
N> something to integrate into (or other way around perhaps? ;-) 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
 
T

ts

R> Seems like I have to upgrade:

R> 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
 
T

ts

S> also, what happened to 'pigeon>' ?

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



Guy Decoux
 
D

Daniel Kelley

Robert> How old is that one?

Released in 1959 on the album "More of Tom Lehrer".

d.k.
 
S

Simon Strandgaard

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.


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?
 
T

T. Onoma

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?

-t0
 

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

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top