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 )