I sort of know what an iterator is, but not well enough to explain it.
What would be a one-sentence definition of an iterator?
···
–
Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137
I sort of know what an iterator is, but not well enough to explain it.
What would be a one-sentence definition of an iterator?
–
Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137
From FOLDOC:
An object or routine for accessing items
from a list, array or stream one at a time.
On Wednesday 22 January 2003 05:07 pm, Daniel Carrera wrote:
I sort of know what an iterator is, but not well enough to explain it.
What would be a one-sentence definition of an iterator?
–
Bruce R. Williams :: [iusris/#ruby-lang] :: http://www.codedbliss.com
‘It does not require a majority to prevail, but rather an irate,
tireless minority keen to set brush fires in people’s minds.’
– Samuel Adams
So,
Would it be correct to say that an iterator is a method that lets you
access items one at a time? (this includes both class and instance
methods).
So I cansay that Array#each is an iterator.
Would this be correct?
Thanks.
BTW, what is FOLDOC?
On Thu, Jan 23, 2003 at 07:10:28AM +0900, Bruce Williams wrote:
On Wednesday 22 January 2003 05:07 pm, Daniel Carrera wrote:
I sort of know what an iterator is, but not well enough to explain it.
What would be a one-sentence definition of an iterator?
From FOLDOC:
An object or routine for accessing items
from a list, array or stream one at a time.–
Bruce R. Williams :: [iusris/#ruby-lang] :: http://www.codedbliss.com‘It does not require a majority to prevail, but rather an irate,
tireless minority keen to set brush fires in people’s minds.’
– Samuel Adams
–
Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137
So,
Would it be correct to say that an iterator is a method that lets you
access items one at a time? (this includes both class and instance
methods).
Yes, an iterator can be a method that functions thus.
So I cansay that Array#each is an iterator.
Would this be correct?
Yes. Array#each is definitely an iterator.
Thanks.
BTW, what is FOLDOC?
Free Online Dictionary of Computing
available at: http://wombat.doc.ic.ac.uk/foldoc/ (and many other locations)
My pleasure,
// Bruce
On Wednesday 22 January 2003 05:23 pm, Daniel Carrera wrote:
–
Bruce R. Williams :: [iusris/#ruby-lang] :: http://www.codedbliss.com
‘It does not require a majority to prevail, but rather an irate,
tireless minority keen to set brush fires in people’s minds.’
– Samuel Adams
That would be true for normal Ruby iterators, but not all iterators.
C++ iterators, for example, are objects rather than methods. The object
can be used to iterate by writing “++it” to go to the next element or
“*it” to retrieve the current element. There are a number of libraries
available for iterating similarly in Ruby.
Paul
On Thu, Jan 23, 2003 at 07:23:14AM +0900, Daniel Carrera wrote:
So,
Would it be correct to say that an iterator is a method that lets you
access items one at a time? (this includes both class and instance
methods).
What is an iterator? Hmmm.
Well, I don’t have a good definition. I think
the FOLDOC definition is a little different
from how we think of things in Ruby.
I would define it by its operation rather than
its usage – something like “a method with the
internal capability of calling a code block
associated with the method call, in such a way
that the method and the block trade control
back and forth like coroutines.”
This is general enough that it even covers
what I call the "non-iterating iterator."
This comes in one or two varieties, but the
most common/intuitive is the "open-close"
kind. See File#open for an example, or
Mutex#synchronize, or the newer form of
Dir.chdir (1.7.x).
I think another kind of non-iterating iterator
occurs in cgi.rb, where blocks are basically
used for string data – don’t really recall
how it works.
Cases like this make the term "iterator"
somewhat inappropriate, of course… but
no one seems to complain about it.
Hal
----- Original Message -----
From: “Bruce Williams” bruce@codedbliss.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Wednesday, January 22, 2003 4:31 PM
Subject: Re: Definition: iterator
Hello Paul,
Thursday, January 23, 2003, 2:27:04 AM, you wrote:
Would it be correct to say that an iterator is a method that lets you
access items one at a time? (this includes both class and instance
methods).
That would be true for normal Ruby iterators, but not all iterators.
C++ iterators, for example, are objects rather than methods. The object
can be used to iterate by writing “++it” to go to the next element or
“*it” to retrieve the current element. There are a number of libraries
available for iterating similarly in Ruby.
technically speaking, iterator is a way to access elements of collection
without knowing details of its implementation
–
Best regards,
Bulat mailto:bulatz@integ.ru
Just because it uses a block doesn’t make it an iterator; a block is used in
itaration, it does not comprise an iterator.
File::open, Dir::chdir, and Mutex#synchronize are not ‘non-iterating
iterators,’ they are merely… not… iterators… because they… don’t…
iterate-- they just make use of a handy block (unless someone wants to coin
the phrase ‘code between an iteration of state’).
– Bruce
On Wednesday 22 January 2003 05:43 pm, Hal E. Fulton wrote:
This is general enough that it even covers
what I call the “non-iterating iterator.”
This comes in one or two varieties, but the
most common/intuitive is the “open-close”
kind. See File#open for an example, or
Mutex#synchronize, or the newer form of
Dir.chdir (1.7.x).
–
Bruce R. Williams :: [iusris/#ruby-lang] :: http://www.codedbliss.com
‘It does not require a majority to prevail, but rather an irate,
tireless minority keen to set brush fires in people’s minds.’
– Samuel Adams
I’d distinguish iterators from the general case of yield-calling
methods, personally. A first approximation to the definition I’d use:
A method that takes in a block, and calls it on successive elements of a
collection.
martin
Hal E. Fulton hal9000@hypermetrics.com wrote:
I would define it by its operation rather than
its usage – something like “a method with the
internal capability of calling a code block
associated with the method call, in such a way
that the method and the block trade control
back and forth like coroutines.”
I certainly complain about it, but limit my complaints to muttering
under my breath. I like the term ‘coroutine’ for describing the
general case of a method accepting a block. And for the “open-close”
usage, I use the term ‘transaction’.
Gavin
On Thursday, January 23, 2003, 9:43:19 AM, Hal wrote:
This is general enough that it even covers
what I call the “non-iterating iterator.”
This comes in one or two varieties, but the
most common/intuitive is the “open-close”
kind. See File#open for an example, or
Mutex#synchronize, or the newer form of
Dir.chdir (1.7.x).
Cases like this make the term “iterator”
somewhat inappropriate, of course… but
no one seems to complain about it.
Hi –
This is general enough that it even covers
what I call the “non-iterating iterator.”
This comes in one or two varieties, but the
most common/intuitive is the “open-close”
kind. See File#open for an example, or
Mutex#synchronize, or the newer form of
Dir.chdir (1.7.x).Just because it uses a block doesn’t make it an iterator; a block is used in
itaration, it does not comprise an iterator.File::open, Dir::chdir, and Mutex#synchronize are not ‘non-iterating
iterators,’ they are merely… not… iterators… because they… don’t…
iterate-- they just make use of a handy block (unless someone wants to coin
the phrase ‘code between an iteration of state’).
I think this is why Hal said that the usage in Ruby is not necessarily
exactly the same as the dictionary definition. In Ruby, the presence
of a block does cause a method to report itself as an iterator:
irb(main):007:0> def it; puts “I’m an iterator!” if iterator?; end
=> nil
irb(main):008:0> it
=> nil
irb(main):009:0> it {}
I’m an iterator!
=> nil
ri reports that iterator? (which is a synonym for block_given?) will
be removed in 1.8, though that hasn’t happened as of 1.8.0preview1.
Assuming that does happen, it will bring the terminology more into
conformity with what you’re describing.
David
On Thu, 23 Jan 2003, Bruce Williams wrote:
On Wednesday 22 January 2003 05:43 pm, Hal E. Fulton wrote:
–
David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav
I would define it by its operation rather than
its usage – something like “a method with the
internal capability of calling a code block
associated with the method call, in such a way
that the method and the block trade control
back and forth like coroutines.”I’d distinguish iterators from the general case of yield-calling
methods, personally. A first approximation to the definition I’d use:A method that takes in a block, and calls it on successive elements of a
collection.
Yes, in a sense I agree.
I do wish we had a name for “methods that
call yield.” In Ruby we’ve traditionally
called them “iterators” (witness the
iterator? method as David pointed out).
That’s a misleading usage, but don’t blame
me for it. I didn’t originate it.
<not_really_serious>
But on the other hand: What is a “collection”?
You say “successive” elements. Well, what
about this:
[1].each {|x| puts x}
It doesn’t operate on successive elements. There
aren’t any successive elements. There is only
one.
We could perhaps view the other cases in a similar
way – File.open acts on a collection of files
(containing only one file). Mutex#synchronize
iterates through a list containing one mutex.
And so on. If you only loop once, are you really
iterating? Or just irritating? What is the sound
of one hand clapping? What is the speed of sound
in a vacuum? Where does your lap go when you
stand up?
</not_really_serious>
Hal
----- Original Message -----
From: “Martin DeMello” martindemello@yahoo.com
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Wednesday, January 22, 2003 5:21 PM
Subject: Re: Definition: iterator
Hal E. Fulton hal9000@hypermetrics.com wrote:
Hello Gavin,
Thursday, January 23, 2003, 5:09:46 PM, you wrote:
I like the term ‘coroutine’ for describing the
general case of a method accepting a block.
… but full-featured cotoutines can do YIELD on both sides
–
Best regards,
Bulat mailto:bulatz@integ.ru
[I wrote]
A method that takes in a block, and calls it on successive elements of a
collection.Yes, in a sense I agree.
I do wish we had a name for “methods that
call yield.” In Ruby we’ve traditionally
called them “iterators” (witness the
iterator? method as David pointed out).That’s a misleading usage, but don’t blame
me for it. I didn’t originate it.
Hm - something involving the phrase ‘coroutines’, perhaps. What’s the
singular of coroutines?
<not_really_serious>
But on the other hand: What is a “collection”?
You say “successive” elements. Well, what
about this:
[1].each {|x| puts x}
It doesn’t operate on successive elements. There
aren’t any successive elements. There is only
one.
Heh I actually expected to be called on methods like each_second,
whch stretches the definition of ‘successive’
And so on. If you only loop once, are you really
iterating? Or just irritating? What is the sound
ROTFL!!
martin
Hal E. Fulton hal9000@hypermetrics.com wrote:
Hello Martin,
Thursday, January 23, 2003, 3:02:10 AM, you wrote:
I do wish we had a name for “methods that
call yield.” In Ruby we’ve traditionally
called them “iterators” (witness the
iterator? method as David pointed out).
Hm - something involving the phrase ‘coroutines’, perhaps. What’s the
singular of coroutines?
technically speaking, it’s a higher-order function, or functional
it’s not coroutine, because
–
Best regards,
Bulat mailto:bulatz@integ.ru
I don’t understand what you mean. I believe that functions that take blocks
meat both these criteria.
def foo
puts “1”
yield
puts “2”
yield
puts “3”
yield
end
foo do
puts “this is a test”
next
puts “you should not see this message”
end
[pbrannan@zaphod tmp]$ ruby foo.rb
1
this is a test
2
this is a test
3
this is a test
Paul
On Fri, Jan 24, 2003 at 08:29:11PM +0900, Bulat Ziganshin wrote:
technically speaking, it’s a higher-order function, or functional
it’s not coroutine, because
- we can call block several times
- we can’t return control from middle of block
Paul Brannan pbrannan@atdesk.com writes:
- we can call block several times
- we can’t return control from middle of block
I don’t understand what you mean. I believe that functions that take blocks
meat both these criteria.
That would be true if you could essentially “yield” back to the calling
routine, so this would say:
foo do
puts “this is a test”
next
puts “you should not see this message”
end
1
this is a test
2
you should not see this message.
Co-routines can switch control both ways.
–
Some people claim that the UNIX learning curve is steep, but at least you
only have to climb it once.
Hello Simon,
Friday, January 24, 2003, 5:08:39 PM, you wrote:
Paul Brannan pbrannan@atdesk.com writes:
- we can call block several times
- we can’t return control from middle of block
Co-routines can switch control both ways.
… and can’t reexecute block from start. the above 2 criteries
is about ruby “iterators”, and it’s their difference from idea
of coroutines
–
Best regards,
Bulat mailto:bulatz@integ.ru