[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Is iterating in lock-step possible?

Roshan James

3/15/2005 10:08:00 AM


// Would you be happy if you hadn't looked at the code? Would
// you be happy if Ruby continuations were really really fast?
// Would you be happy if these generator objects were built-in
// to the Ruby C api and the implementation were effective
// obscured from you?
//
// What is it, exactly, you want Ruby to do?

At this point I need to ask you a question -
How do internal iterators work? How do they manage state? I havent seen
a description of this anywhere.
I know about creating objects for iteration. How do the internal
itertors differ from these?


I had thought that ruby would create an object for iteration, by
examining the proc code. It would then live in the context of the Ruby
GC like all objects and maintain state. The only difference I saw with
the 'internal' iterator is that since the syntax of ruby limits the
usage of the iterator to an explicit code block that catches it, the
details of the itertor object have been hidden from the user.

Which is to say that it is 'internal' only in the sense that
traditionally there was no need to give anyone explict access to the
object. The moment the idea of using multiple iterators in tandem is
considered, this assumption of hiding the object does not seem like the
right approach. The correct solution would have been to expose the
iterator object itself.

If however internal iterators are implmented in some way that have
different semantics from normal objects, then wrapping with
continuations is an acceptable solution for what is a design limitation
in the language. Wrapping itertors in continuations should not be
accepted way to do this in the language when there is a simpler
alternative possible.


So you see I really need to understand how internal iterators work,
before taking this ahead.
See the difference I see is that I see this as something the language
should have solved and not the library.

Roshan



1 Answer

William Morgan

3/15/2005 3:33:00 PM

0

Excerpts from Roshan James's mail of 15 Mar 2005 (EST):
> At this point I need to ask you a question -
> How do internal iterators work? How do they manage state? I havent seen
> a description of this anywhere.
> I know about creating objects for iteration. How do the internal
> itertors differ from these?

The iteration state is all in the iterator method. For example, look at
how the Fibonacci series is written:

def fib
a = 1
b = 0
yield 0
while true
x = a + b
yield x
a = b
b = x
end
end

fib { |n| puts n; break if n > 100 }

All the state (a, b, and x) is in the iterator method.

So the difference between external and internal iterators is: rather
than repeatedly calling an "step" method on an "iterator object" and
doing something with the result, you call an "iterator method" once, and
pass it a lexical closure (in Ruby, a Proc object), and *it* then calls
the Proc on each element.

Internal iterators are not a Ruby-specific construct, nor are they
explicitly built-in to Ruby, except in the sense that the syntactic
sugar makes them very easy to do, and the API uses them all over. Think
of them as a different "style" of iteration.

Hope that answers your question.

--
William <wmorgan-ruby-talk@masanjin.net>