IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New What do you think of Ruby's iterators and generators?
I am not sure what Smalltalk's are like. But Ruby has had iterators and generators for a long time and they are a basic part of the language.

What you do is define a class, give it a method called each which will when called yield each element in turn and then return. Then mixin [link|http://www.rubycentral.com/book/ref_m_enumerable.html|Enumerable]. And you are done. You have a class whose objects suport the basic list-oriented access methods. You can loop over it. You can call find, collect, etc. And if you add the <=> method, then you can call methods like max, sort, etc.

How well integrated is this into the language? Well yield is basic. A fundamental paradigm throughout the language is transfering control back to the caller with yield. (Ruby actually supports full continuations.) Mixins are basic. They allow many classes to define common methods from a few basic ones. You just write the code once in a module, then include that module in many classes. You can think of them as a controlled form of multiple inheritance. (Ruby has single inheritance.)

In point of fact I think that Enumerable is written in C for performance. But if you wanted to write it in Ruby, it would be trivial to do so...

Cheers,
Ben
New Re: What do you think of Ruby's iterators and generators?
I am not sure what Smalltalk's are like. But Ruby has had iterators and generators for a long time and they are a basic part of the language.

Smalltalk doesn't even have iterators as such as part of the language. It implements two kinds of iterators using more primitive language parts. Basically, you implement itteration using just objects (especially closures) and message sends.

Internal iterators using block closures that loop over the elements ina collection, executing the closure for each element in the collection and doing something with the result. They are called 'internal' because basically you hand the collection a closure and it executes the closure for each element in the collection, sequentially, without interruption

An example would be a simple loop
1 to: 10: do: [:i | Transcript cr; show: i printString]

aCollection do: [: each | each doSomething]



External iterators are usually called "streams" and involve building a wrapper around a collection where the wrapper maintains positioning information about it's location in regards to the collection. External iterators can be more powerful and flexible because they allow greater ways to interrupt the flow of control or to skip ahead or grab arbitary pieces of the stream



"Ignore the first three and then print the subsequent sums of the rest of the stream in pairs"

str = ReadStream on: #(1,2,3,4,5,7,8,9,10)
str skipTo: 3
[str atEnd] whileFalse: [
nextTwo := str next: 2
Transript cr; show: (nextTwo first + nextTwo last) printString
]

That's a bit trickier with internal iterators


However, I really don't care about "Smalltalk versus Python" iterators froma 'which is better' point of view. I'm just disgruntled at the way Python chose to develop iterators as a change to the language rather then building on the existing structure of the language. (Note that the python 'iterators' we're talking about here are not normal loops but are a new addition to add iteration capability to custom built classes )
Jay O'Connor

"Going places unmapped
to do things unplanned
to people unsuspecting"
     Python 2.2... - (admin) - (5)
         Interesting. - (static) - (4)
             Nope - (Fearless Freep) - (3)
                 Don't know Python. - (static)
                 What do you think of Ruby's iterators and generators? - (ben_tilly) - (1)
                     Re: What do you think of Ruby's iterators and generators? - (Fearless Freep)

Then when the mother turned back around I stuck my tongue out at the kid again.
57 ms