From: Robert Klemme Date: 2004-05-07T17:38:57+09:00 Subject: Re: [ann] iterator-0.7 "Simon Strandgaard" schrieb im Newsbeitrag news:20040506182920.02fd89f5.neoneye@adslhome.dk... > "Robert Klemme" wrote: > > "Simon Strandgaard" schrieb im Newsbeitrag > > news:20040506120251.43669286.neoneye@adslhome.dk... > > > homepage: > > > http://raa.ruby-lang.org/list.rhtml?name=iterator > > > > > > download: > > > http://rubyforge.org/frs/?group_id=18&release_id=467 > > > > > > > > > External iterators with some STL inspiration. > [snip] > > Great! This looks good. I'd just do some small interface changes. > > > > I am always open to suggestions ;-) > > > > Move Array#create_iterator to Enumerable and rename it to > > Enumerable#iterator (we're lazy typers) if possible. > > Good suggestion.. will change it. > > > > Alternatively you could provide this wrapper: > > > > module Enumerable > > def iterator; Iterator::Continuation.new(self, :each); end > > end > > > > Also a good suggestion.. will change it. > > > > > Make Iterator::Concat#initialize to be able to do > > iterator = Iterator::Concat.new(i1, i2, i3) # brackets removed > > iterator = Iterator::Concat.new(i1, ary2, i3) > > > > Major dillema, I don't know how to do it. > My current Concat#initialize looks like "def initialize(iterators, position=nil)" Ah, I see. > If I do "def initialize(*iterators, position=nil)".. that won't work ? > > irb(main):001:0> def m(*args, x=nil) > irb(main):002:1> p args > irb(main):003:1> p x > irb(main):004:1> end > SyntaxError: compile error > (irb):1: syntax error > def m(*args, x=nil) > ^ > (irb):1: syntax error > from (irb):1 > irb(main):005:0> > > Any hints on how to pass 'position' to initialize ? def init(*args) if Numeric === args[-1] pos = args[-1] args.slice!(-1, 1) else pos = nil end puts "iterators: #{args.inspect}" puts "position : #{pos.inspect}" end irb(main):066:0> init "a","s",1 iterators: ["a", "s"] position : 1 => nil irb(main):067:0> init "a","s" iterators: ["a", "s"] position : nil => nil irb(main):068:0> init iterators: [] position : nil => nil Or even def init2(*args) pos = nil iters = [] args.each do |arg| case arg when Numeric raise ArgumentError, "duplicate start positions" if pos pos = arg when Iterator # place appropriate type(s) here iters << arg when Enumerable iters << Iterator::Continuation.new(arg, :each) else raise ArgumentError, "Don't know what to do with #{arg.inspect}" end end puts "iterators: #{iters.inspect}" puts "position : #{pos.inspect}" end > > Nice to have but maybe not worth the effort: > > Iterator#reset > > Its already there.. #first and #last both resets the iterator. Ooops, overlooked that one. > Thanks for these great suggestions.. keep them coming. You're welcome. Kind regards robert