[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: iterator class not working

Ross Bamford

3/24/2006 2:10:00 PM

Just a small fix:

On Fri, 2006-03-24 at 22:49 +0900, Ross Bamford wrote:
> def initialize(enum)
> @yield = lambda do
> enum.each do |item|
> - @yield = callcc { |cc|
+ callcc { |cc|
> @next.call cc, item
> }
> end
> raise "Exhausted"
> end
> end

Doesn't really hurt (as currently written) but it's unnecessary and
could cause confusion.

--
Ross Bamford - rosco@roscopeco.REMOVE.co.uk



11 Answers

Pete

3/24/2006 2:26:00 PM

0

Thanks for all you help.

Finally, using two nested continuations works well...

Here's my working code ... I am very curious about
optimization and alternatives from you guts. Afterall
I found this issue a very exciting one!!

class Iterator

def initialize(obj, method = :each)
@obj = obj
@method = method
end

def next
raise "done" if @done
@iteration.call if @iteration
@iteration, @current = callcc do |loop|
@obj.send(@method) do |item|
callcc do |state|
loop.call state, item
end
end
nil
end
@current
end

end

names = %w{peter paul mary gordy john jane elwood}

it = Iterator.new(names)
while item = it.next
puts item
end

it = Iterator.new("Teststring", :each_byte)
while item = it.next
puts item.chr
end


peter
paul
mary
gordy
john
jane
elwood
T
e
s
t
s
t
r
i
n
g


> --- Ursprüngliche Nachricht ---
> Von: Ross Bamford <rossrt@roscopeco.co.uk>
> An: ruby-talk@ruby-lang.org (ruby-talk ML)
> Betreff: Re: iterator class not working
> Datum: Fri, 24 Mar 2006 23:10:05 +0900
>
> Just a small fix:
>
> On Fri, 2006-03-24 at 22:49 +0900, Ross Bamford wrote:
> > def initialize(enum)
> > @yield = lambda do
> > enum.each do |item|
> > - @yield = callcc { |cc|
> + callcc { |cc|
> > @next.call cc, item
> > }
> > end
> > raise "Exhausted"
> > end
> > end
>
> Doesn't really hurt (as currently written) but it's unnecessary and
> could cause confusion.
>
> --
> Ross Bamford - rosco@roscopeco.REMOVE.co.uk
>
>


Pete

3/24/2006 2:28:00 PM

0

really UGLY typo!!

don't take it personal :-)

> optimization and alternatives from you gu_t_s. Afterall

guys!!!!!


> --- Ursprüngliche Nachricht ---
> Von: "Peter Ertl" <pertl@gmx.org>
> An: ruby-talk@ruby-lang.org (ruby-talk ML)
> Betreff: Re: iterator class not working
> Datum: Fri, 24 Mar 2006 23:25:59 +0900
>
> Thanks for all you help.
>
> Finally, using two nested continuations works well...
>
> Here's my working code ... I am very curious about
> optimization and alternatives from you guts. Afterall
> I found this issue a very exciting one!!
>
> class Iterator
>
> def initialize(obj, method = :each)
> @obj = obj
> @method = method
> end
>
> def next
> raise "done" if @done
> @iteration.call if @iteration
> @iteration, @current = callcc do |loop|
> @obj.send(@method) do |item|
> callcc do |state|
> loop.call state, item
> end
> end
> nil
> end
> @current
> end
>
> end
>
> names = %w{peter paul mary gordy john jane elwood}
>
> it = Iterator.new(names)
> while item = it.next
> puts item
> end
>
> it = Iterator.new("Teststring", :each_byte)
> while item = it.next
> puts item.chr
> end
>
>
> peter
> paul
> mary
> gordy
> john
> jane
> elwood
> T
> e
> s
> t
> s
> t
> r
> i
> n
> g
>
>
> > --- Ursprüngliche Nachricht ---
> > Von: Ross Bamford <rossrt@roscopeco.co.uk>
> > An: ruby-talk@ruby-lang.org (ruby-talk ML)
> > Betreff: Re: iterator class not working
> > Datum: Fri, 24 Mar 2006 23:10:05 +0900
> >
> > Just a small fix:
> >
> > On Fri, 2006-03-24 at 22:49 +0900, Ross Bamford wrote:
> > > def initialize(enum)
> > > @yield = lambda do
> > > enum.each do |item|
> > > - @yield = callcc { |cc|
> > + callcc { |cc|
> > > @next.call cc, item
> > > }
> > > end
> > > raise "Exhausted"
> > > end
> > > end
> >
> > Doesn't really hurt (as currently written) but it's unnecessary and
> > could cause confusion.
> >
> > --
> > Ross Bamford - rosco@roscopeco.REMOVE.co.uk
> >
> >
>


Pete

3/24/2006 2:29:00 PM

0

aargh, again...

remove that line, please:

> raise "done" if @done

> --- Ursprüngliche Nachricht ---
> Von: "Peter Ertl" <pertl@gmx.org>
> An: ruby-talk@ruby-lang.org (ruby-talk ML)
> Betreff: Re: iterator class not working
> Datum: Fri, 24 Mar 2006 23:25:59 +0900
>
> Thanks for all you help.
>
> Finally, using two nested continuations works well...
>
> Here's my working code ... I am very curious about
> optimization and alternatives from you guts. Afterall
> I found this issue a very exciting one!!
>
> class Iterator
>
> def initialize(obj, method = :each)
> @obj = obj
> @method = method
> end
>
> def next
> raise "done" if @done
> @iteration.call if @iteration
> @iteration, @current = callcc do |loop|
> @obj.send(@method) do |item|
> callcc do |state|
> loop.call state, item
> end
> end
> nil
> end
> @current
> end
>
> end
>
> names = %w{peter paul mary gordy john jane elwood}
>
> it = Iterator.new(names)
> while item = it.next
> puts item
> end
>
> it = Iterator.new("Teststring", :each_byte)
> while item = it.next
> puts item.chr
> end
>
>
> peter
> paul
> mary
> gordy
> john
> jane
> elwood
> T
> e
> s
> t
> s
> t
> r
> i
> n
> g
>
>
> > --- Ursprüngliche Nachricht ---
> > Von: Ross Bamford <rossrt@roscopeco.co.uk>
> > An: ruby-talk@ruby-lang.org (ruby-talk ML)
> > Betreff: Re: iterator class not working
> > Datum: Fri, 24 Mar 2006 23:10:05 +0900
> >
> > Just a small fix:
> >
> > On Fri, 2006-03-24 at 22:49 +0900, Ross Bamford wrote:
> > > def initialize(enum)
> > > @yield = lambda do
> > > enum.each do |item|
> > > - @yield = callcc { |cc|
> > + callcc { |cc|
> > > @next.call cc, item
> > > }
> > > end
> > > raise "Exhausted"
> > > end
> > > end
> >
> > Doesn't really hurt (as currently written) but it's unnecessary and
> > could cause confusion.
> >
> > --
> > Ross Bamford - rosco@roscopeco.REMOVE.co.uk
> >
> >
>


Robert Dober

3/24/2006 2:43:00 PM

0

On 3/24/06, Ross Bamford <rossrt@roscopeco.co.uk> wrote:
>
> Just a small fix:
>
> On Fri, 2006-03-24 at 22:49 +0900, Ross Bamford wrote:
> > def initialize(enum)
> > @yield = lambda do
> > enum.each do |item|
> > - @yield = callcc { |cc|
> + callcc { |cc|
> > @next.call cc, item
> > }
> > end
> > raise "Exhausted"
> > end
> > end
>
> Doesn't really hurt (as currently written) but it's unnecessary and
> could cause confusion.
>
> --
> Ross Bamford - rosco@roscopeco.REMOVE.co.uk


Well I got it!
Was I missing something or is my solution really better?

---------------------------------- 8< --------------------------
class Iterator
class Exhausted < Exception; end
def initialize( enum )
@enum = enum
@next = nil
end

def next
return @next.call if @next
@enum.each do
|item|
callcc{ |cc|
@next = cc
return item
}
end # do
raise Exhausted, "No more items :("
end # def next
end # class Iterator

i = Iterator.new( %w{ Ringo John Paul George } )
loop do; puts i.next; end





--
Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui
concerne l'univers, je n'en ai pas acquis la certitude absolue.

- Albert Einstein

Pete

3/24/2006 2:53:00 PM

0

Indeed, you topped my solution my needing one callcc less...

I allowed myself to change it a little so it will return
nil once the enumeration is done and does not need exceptions:

class Iterator
def initialize( enum )
@enum = enum
@next = nil
end

def next
@next.call if @next
@enum.each do |item|
callcc do |cc|
@next = cc
return item
end
end
nil
end
end

i = Iterator.new( %w{ Ringo John Paul George } )

while item = i.next
puts item
end


> --- Ursprüngliche Nachricht ---
> Von: "Robert Dober" <robert.dober@gmail.com>
> An: ruby-talk@ruby-lang.org (ruby-talk ML)
> Betreff: Re: iterator class not working
> Datum: Fri, 24 Mar 2006 23:43:15 +0900
>
> On 3/24/06, Ross Bamford <rossrt@roscopeco.co.uk> wrote:
> >
> > Just a small fix:
> >
> > On Fri, 2006-03-24 at 22:49 +0900, Ross Bamford wrote:
> > > def initialize(enum)
> > > @yield = lambda do
> > > enum.each do |item|
> > > - @yield = callcc { |cc|
> > + callcc { |cc|
> > > @next.call cc, item
> > > }
> > > end
> > > raise "Exhausted"
> > > end
> > > end
> >
> > Doesn't really hurt (as currently written) but it's unnecessary and
> > could cause confusion.
> >
> > --
> > Ross Bamford - rosco@roscopeco.REMOVE.co.uk
>
>
> Well I got it!
> Was I missing something or is my solution really better?
>
> ---------------------------------- 8< --------------------------
> class Iterator
> class Exhausted < Exception; end
> def initialize( enum )
> @enum = enum
> @next = nil
> end
>
> def next
> return @next.call if @next
> @enum.each do
> |item|
> callcc{ |cc|
> @next = cc
> return item
> }
> end # do
> raise Exhausted, "No more items :("
> end # def next
> end # class Iterator
>
> i = Iterator.new( %w{ Ringo John Paul George } )
> loop do; puts i.next; end
>
>
>
>
>
> --
> Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui
> concerne l'univers, je n'en ai pas acquis la certitude absolue.
>
> - Albert Einstein
>


Robert Dober

3/24/2006 3:24:00 PM

0

On 3/24/06, Ross Bamford <rossrt@roscopeco.co.uk> wrote:
>
> On Fri, 2006-03-24 at 23:43 +0900, Robert Dober wrote:
> >
> > Well I got it!
> > Was I missing something or is my solution really better?
>
> Well, if you're going to cheat and use return ... :)
>
> Seriously though, I like that one. Shows how it's important to remember
> the 'normal' stuff when you're thinking in this high level stuff :)
>
> And yes, I guess it is better:
>
> user system total real
> Iterator1 4.730000 0.040000 4.770000 ( 4.877037)
> Iterator2 2.840000 0.050000 2.890000 ( 2.960747)
> Iterator3 2.800000 0.130000 2.930000 ( 2.998121)
> Iterator4 2.040000 0.020000 2.060000 ( 2.131763)
>
> (1 = my updated, easy to follow version of Peter's original, 2 = same
> version with one callcc and one catch, 3 = Peter's second
> implementation, 4 = your callcc/return implementation)
>
> As you can see, though, they're all pretty slow when it comes right down
> to it (this was only over (0...50000).to_a) - some of the
> FasterGenerator quiz entries I mentioned achieved quite amazing speeds
> over that many iterations and more - you should check them out.
>
> --
> Ross Bamford - rosco@roscopeco.REMOVE.co.uk
>
>
> I have no merit than ;), no it is true that I have not understood a bit of
your continuation stuff, when I realized what callcc really does, I realized
that I was wrong about the coroutines.
Ty for the acknowledgement but I learnt it from your code!
Cheers


--
Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui
concerne l'univers, je n'en ai pas acquis la certitude absolue.

- Albert Einstein

Pit Capitain

3/24/2006 4:14:00 PM

0

Peter Ertl schrieb:
> class Iterator
> ...
> end
>
> i = Iterator.new( %w{ Ringo John Paul George } )
>
> while item = i.next
> puts item
> end

Peter, test your code with

puts i.next
puts i.next

I don't think you can get away with only one continuation.

Regards,
Pit


Nate Smith

3/24/2006 4:22:00 PM

0

Hello,

What is the benefit in ruby of allowing the following syntax:

in irb:

irb(main):005:0> 1 + + + + 3
=> 4
irb(main):006:0> 1 + - + 3
=> -2
irb(main):007:0> 1 - - - - 3
=> 4
irb(main):008:0> 1 - + - 3
=> 4
irb(main):010:0> 1 - +-3
=> 4

Is this a feature or a gotcha?

Nate




Ara.T.Howard

3/24/2006 4:33:00 PM

0

Pete

3/24/2006 4:34:00 PM

0

whoa, amazing + surprising!!!

now, I have to check that code again ... :-)

> --- Ursprüngliche Nachricht ---
> Von: Pit Capitain <pit@capitain.de>
> An: ruby-talk@ruby-lang.org (ruby-talk ML)
> Betreff: Re: iterator class not working
> Datum: Sat, 25 Mar 2006 01:14:09 +0900
>
> Peter Ertl schrieb:
> > class Iterator
> > ...
> > end
> >
> > i = Iterator.new( %w{ Ringo John Paul George } )
> >
> > while item = i.next
> > puts item
> > end
>
> Peter, test your code with
>
> puts i.next
> puts i.next
>
> I don't think you can get away with only one continuation.
>
> Regards,
> Pit
>