[ruby-talk:02383] Re: Iterator into array

From: Dave Thomas <Dave@...>
Date: 2000-04-07 19:41:42 UTC
List: ruby-talk #2383
mengx@nielsenmedia.com writes:

> > 
> >  result = FibGen.new(20).collect
> > 
> If I understood correctly, you could use  'iterator?' to
> return the array directly.

True, but that then means that every iterator would have to implement
the test. It would seem better to make the implementation part of the
general set of collections, rather than of the methods that fill them.

> Or best of all, not to use iterator style method at all :-). I do
> not like iterators (except for callback).  I prefer to use different
> methods to generate different sequences and use for loop to get
> things done. I also noticed that iterators are consistently
> slower(at least 5%) than a for loop.

I use iterators mostly because they put the implementation of the
traversal of a collection back inside the collection where it
belongs. This seems like a good idea.

In terms of performance, I agree there's a difference (I'd actually
say it's in excess of 5%). I wonder how much would break if the
rb_eval in eval.c was replaced with an rb_call?

Dave


In This Thread

Prev Next