The essence of the iterator pattern

  • Authors:
  • Jeremy Gibbons;Bruno c. d. s. Oliveira

  • Affiliations:
  • Oxford university computing laboratory, wolfson building, parks road, oxford ox1 3qd, uk (e-mail: jeremy.gibbons@comlab.ox.ac.uk, bruno.oliveira@comlab.ox.ac.uk);Oxford university computing laboratory, wolfson building, parks road, oxford ox1 3qd, uk (e-mail: jeremy.gibbons@comlab.ox.ac.uk, bruno.oliveira@comlab.ox.ac.uk)

  • Venue:
  • Journal of Functional Programming
  • Year:
  • 2009

Quantified Score

Hi-index 0.00

Visualization

Abstract

The Iterator pattern gives a clean interface for element-by-element access to a collection, independent of the collection's shape. Imperative iterations using the pattern have two simultaneous aspects: mapping and accumulating. Various existing functional models of iteration capture one or other of these aspects, but not both simultaneously. We argue that C. McBride and R. Paterson's applicative functors (Applicative programming with effects, J. Funct. Program., 18 (1): 1–13, 2008), and in particular the corresponding traverse operator, do exactly this, and therefore capture the essence of the Iterator pattern. Moreover, they do so in a way that nicely supports modular programming. We present some axioms for traversal, discuss modularity concerns and illustrate with a simple example, the wordcount problem.