From: Robert Dober Date: 2006-03-24T22:45:41+09:00 Subject: Re: iterator class not working ------=_Part_7422_14837052.1143207924293 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On 3/24/06, Peter Ertl wrote: > > Hello Robert, > > thanks for your feedback. > > Yield'ing and using Array.[] seems obvious but I want class 'Iterator' to > work for any 'Enumerable', not only 'Array'. > > The intention for using 'binding' is that I expect / hope it will keep > track > of the enumeration state inside 'Enumeration.each'. However, I don't know > if > that's possible in ruby at all. > > > > > --- Urspr=FCngliche Nachricht --- > > Von: "Robert Dober" > > An: ruby-talk@ruby-lang.org (ruby-talk ML) > > Betreff: Re: iterator class not working > > Datum: Fri, 24 Mar 2006 22:17:32 +0900 > > > > On 3/24/06, Peter Ertl wrote: > > > > > > Hi, > > > > > > in order to understand ruby better (and for fun reasons of course) I > try > > > to > > > write an iterator class. this way I want to understand 'binding' and > > > 'callcc' better. > > > > > > however, it does not work! :-( > > > > > > any help will be greatly appreciated!!! > > > > > > --------------------------------------------- > > > > > > class Iterator > > > > > > def initialize(enum) > > > @context =3D binding > > > end > > > > > > def next > > > enum =3D eval("enum", @context) > > > @context, item =3D callcc do |cont| > > > enum.each do |item| > > > cont.call binding, item > > > > > > ^ > > | > > ------------------------+ > > you are missing "state" here, each time you call next, you start to > > iterate > > over your "enum" and > > jump out of the iterator at the first iteration, that is giving you > "mary" > > all the time. > > > > > > end > > > end > > > item > > > end > > > > > > end > > > > > > names =3D %w{mary gordy john jane elwood} > > > > > > it =3D Iterator.new(names) > > > > > > 3.times do > > > puts it.next > > > end > > > > > > > mary > > > > mary > > > > mary > > > > > > Best regards > > > Peter > > > > > > > > Well I see why it does not work, but I fail to see your design behind > the > > thing > > it could be done like this > > class Iterator > > def initialize(*args); @items=3Dargs;@i=3D-1;end > > > > def next > > @i+=3D1; return @items[@i] unless block_given?; yield @items[@i]; end > > end > > but I do not really know what that would be good for. > > > > Cheers > > Robert > > > > -- > > Deux choses sont infinies : l'univers et la b=EAtise humaine ; en ce qu= i > > concerne l'univers, je n'en ai pas acquis la certitude absolue. > > > > - Albert Einstein > > > > and you want lazy evaluation, converting the Enumeration into an array is not an option, right? I think what we need here are coroutines and we do not have them (yet). I fail to see a solution, hopefully somebody brighter will enlighten us ;) Cheers Robert -- Deux choses sont infinies : l'univers et la b=EAtise humaine ; en ce qui concerne l'univers, je n'en ai pas acquis la certitude absolue. - Albert Einstein ------=_Part_7422_14837052.1143207924293--