[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[ann] iterator 0.1

Simon Strandgaard

11/20/2003 1:53:00 PM

homepage:
http://raa.ruby-lang.org/list.rhtml?nam...

download:
http://rubyforge.org/download.php/213/iterator-...


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?f...


--
Simon Strandgaard
21 Answers

Nikolai Weibull

11/20/2003 6:05:00 PM

0

* 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 ;-)
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/proj...
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);}

ts

11/20/2003 6:11:00 PM

0

>>>>> "N" == Nikolai Weibull <ruby-talk@pcppopper.org> writes:

N> * 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?
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


Mark Wilson

11/20/2003 6:56:00 PM

0


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

>>>>>> "N" == Nikolai Weibull <ruby-talk@pcppopper.org> writes:
>
> N> * 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?
> 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


Simon Strandgaard

11/20/2003 7:10:00 PM

0

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

>>>>>> "N" == Nikolai Weibull <ruby-talk@pcppopper.org> writes:
>
> N> * 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?
> 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....


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

--
Simon Strandgaard

Simon Strandgaard

11/20/2003 8:27:00 PM

0

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

> * 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 ;-)

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.view...

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.view...


--
Simon Strandgaard

Robert Klemme

11/21/2003 8:24:00 AM

0


"ts" <decoux@moulon.inra.fr> schrieb im Newsbeitrag
news:200311201811.hAKIBOa26104@moulon.inra.fr...
> >>>>> "N" == Nikolai Weibull <ruby-talk@pcppopper.org> writes:
>
> N> * 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?
> 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

ts

11/21/2003 10:33:00 AM

0

>>>>> "R" == Robert Klemme <bob.news@gmx.net> writes:

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


gabriele renzi

11/21/2003 8:28:00 PM

0

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


>nasun% ruby -v

where did 'svg%' go ? ;)

Simon Strandgaard

11/22/2003 5:03:00 PM

0

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 ? ;)

also, what happened to 'pigeon>' ?

--
Simon Strandgaard


ts

11/22/2003 5:13:00 PM

0

>>>>> "S" == Simon Strandgaard <neoneye@adslhome.dk> writes:

S> 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 ? ;)

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

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



Guy Decoux