views:

161

answers:

3

What are the meanings of "iterator" and "iterable", and what are the benefits of using them. Please answer as I have an exam tomorrow.

+3  A: 

Just google a bit, takes just 20 seconds...

Iterator pattern

InsertNickHere
The Iterator pattern doesn't explain what an iterator **is**. Sure, the flip answer is *"An iterator is something that iterates"*, but what is the something? That other word, *iterable*, would almost be self-explanatory (e.g. it's something that can be iterated). But isn't everything iterable? Would you want everything to be iterable? Is there any case when you wouldn't want something to be iterable?
Ian Boyd
Yeah, but iterator is linked in this artikle, its the 12th word. ;-)
InsertNickHere
This site would only have 30% of the questions it does, if people googled a bit
Ed B
+2  A: 

Iterator:

http://www.google.com/search?q=define%3Aiterator

Iterable:

http://www.google.com/search?q=define%3Aiterable

OscarRyz
-1 The english language definitions don't help understand the intricacies of a computing concept. i went to answer his question, but i realized i don't really know what they are, aside from *Something that iterates* / *Something that can be iterated.* (which doesn't answer his question)
Ian Boyd
@Ian: Your dv seems reasonable. Honestly I don't expect to fully clarify all the doubts the OP may have with my answer, but this is at least a **very good start**. From there the OP may ask: *I didn't understand what does it mean "traverse..."* or something like that. As per the quality of the question, I think this is the most helpful answer I can give.
OscarRyz
In all honesty, the best answer i can come up with is the example `foreach (Thing foo in listOfThings) { }`. Which isn't an answer - Donald Knuth would not be happy :(
Ian Boyd
+2  A: 

You need to know is the definition of 'iteration' to understand 'iterator' and 'iterable'. Essentially, it means to go over all of the items in a collection (using that word informally) one by one.

So, an iterator is something that goes over elements of an array/object/hash/dictionary/whatever one by one. An iterable is something that can have that done to it.

The technical implementations vary from language to language. Typically an iterable responds to a next() method, yielding, of course, the next item to be acted upon.

Check out http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Iterable.html for instance:

public interface Iterable
Implementing this interface allows an object to be the target of the "foreach" statement.

Alex JL