Skip to content.

Manageability

Sections
Personal tools
You are here: Home » blog » archive » Six Degrees of Iterator

Six Degrees of Iterator

20030516095609

I guess you've heard of the game "Six Degrees of Kevin Bacon" and the mathematical theory behind it.  I'm looking at the Iterator design pattern and I begin to realize that its connected to lots of things.

Take a look at the back cover of the GOF book, it shows Iterator directly connected to the Composite, Memento and Visitor patterns.  That's of course the most obvious, however there's more.

The Iterator is actually a specialization of the more general pattern of Curried Object.  The Curried Object pattern in essence encapsulates object interation providing the client with a much simpler interface.  This interface may be polymorphic which leads us to Generic Programming (remember STL) that allows us to reuse algorithms. 

The Iterator maintains state of the iteration, this is a specialization of the Generator concept.  That there are functions that you can call repeatedly and can give you different results on each invocation.  C# in fact will be adding a new feature, essentially a generator, that makes it easier to write iterators.  

The project JMatch has a parser that makes it easy to write generators.  Interestingly enough, JMatch considers iteration as the "reverse" computation of the contains method of a container.  Interesting perspective!

Now of course, Generators are related to Streams, that is you call a stream many times receiving a new chunk of data on each invocation.  Now streams are nice because they follow the Decorator pattern which allows you to chain things together. 

There are also Internal Iterators, that's something what you see in Smalltalk, were you pass a Closure to a container to do the work. Closures of course are a specialization of Continuations, and Generators just happen to be another kind of Continuation.

Iterators are a natural combination with Monoids (i.e. an algebraic structure that has a binary composition operator and a neutral element).  Iterators and Monoids allow you to express  comprehensions  (see list comprehension in python).  This allows you to specify powerful navigation constructs just like you see in XPath. This gets you to Visitor, and according to GOF (enumerating children) back to Iterator.

We can iterate on forever, but you get the point, right? I on the otherhand get this funny feeling that the true essence of Object Oriented Programming is the Iterator.  Inheritance, Encapsulation and Identity could just be incidental!

Created by admin
Last modified 2003-08-02 08:43 AM
visitors
reading
 
 

Powered by Plone

This site conforms to the following standards: