From: "Hal E. Fulton" Date: 2003-01-23T08:33:24+09:00 Subject: Re: Definition: iterator ----- Original Message ----- From: "Martin DeMello" Newsgroups: comp.lang.ruby To: "ruby-talk ML" Sent: Wednesday, January 22, 2003 5:21 PM Subject: Re: Definition: iterator > Hal E. Fulton 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'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. 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? Hal