External Iterator

An ExternalIterator is an object that represents a location within a collection. They are used in conjunction with a loop to traverse all the elements in the collection. Each iteration of the loop contains a statement that increments the logical position of the object within the collection. The rest of the instructions in the loop body can use the ExternalIterator to examine and, often, change the contents of the collection at the location it represents.

ExternalIterators are very flexible. They can be used together to create complex simultaneous iterations, they can be used to share and update the state of the iteration across multiple function calls, they can even be used as object references in some cases. But all manipulations of ExternalIterators are manual, so they require a new loop construct to be written every single time they are used for their normal task of iterating through the members of a collection. Often this means introducing a low level construct into an otherwise high-level method thereby obscuring intention. These loops are also a potential source of bugs, such as the IteratorInvalidationProblem.

Compare with InternalIterators and see InternalizeExternalIterators and TransfoldPattern for ways to avoid the limitations of ExternalIterators.

Are Rubyists and Smalltalkers aware of the family of zipWiths? zipWiths capture at least the "go down N lists simultaneously while applying this function to each of them" kind of external iteration. If you need some other kind of iteration, it's likely that you're flattening a more accurate representation of your problem into a list unnecessarily.


CategoryCodingConventions

EditText of this page (last edited April 27, 2008)
FindPage by searching (or browse LikePages or take a VisualTour)