Pattern 5 | Replacing Iterator |
Intent
To iterate through the elements of a sequence in order, without having to index into it
Overview
An iterator is an object that allows us to iterate over all the objects in a sequence. It does so by maintaining an internal bit of state that keeps track of where in the sequence the iterator is currently. At its simplest, an implementation of Iterator just requires a method that returns the next item in the sequence, with some sentinel value returned when there are no more items.
Most implementations have a separate method to check to see if the iterator has any more items, rather than using a sentinel to check. Some implementations of Iterator allow the underlying collection to be modified by removing the current ...
Get Functional Programming Patterns in Scala and Clojure now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.