## The essence of the Iterator pattern (2006)

### Cached

### Download Links

Venue: | McBride, Conor, & Uustalu, Tarmo (eds), Mathematically-structured functional programming |

Citations: | 17 - 8 self |

### BibTeX

@INPROCEEDINGS{Gibbons06theessence,

author = {Jeremy Gibbons and Bruno C. D. S. Oliveira},

title = {The essence of the Iterator pattern},

booktitle = {McBride, Conor, & Uustalu, Tarmo (eds), Mathematically-structured functional programming},

year = {2006}

}

### OpenURL

### Abstract

The ITERATOR pattern gives a clean interface for element-by-element access to a collection. Imperative iterations using the pattern have two simultaneous aspects: mapping and accumulating. Various existing functional iterations model one or other of these, but not both simultaneously. We argue that McBride and Paterson’s idioms, and in particular the corresponding traverse operator, do exactly this, and therefore capture the essence of the ITERATOR pattern. We present some axioms for traversal, and illustrate with a simple example, the repmin problem.

### Citations

5292 |
Design patterns: Elements of reusable object-oriented software
- Gamma, Helm, et al.
- 1995
(Show Context)
Citation Context ...h a simple example, the repmin problem. Keywords: Iterator, traversal, design pattern, map, fold, monad, idiom. 1. INTRODUCTION Perhaps the most familiar of the so-called Gang of Four design patterns =-=[5]-=- is the ITERATOR pattern, which ‘provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation’. This is achieved by identifying an ITERATOR ... |

175 | PolyP — a polytypic programming language extension
- Jansson, Jeuring
- 1997
(Show Context)
Citation Context ...o apply to non-linear collections, a datatype-generic counting operation defined using fold would need a datatype-generic numeric algebra as the fold body. Such a thing could be defined polytypically =-=[12, 10]-=-, but the fact remains that fold in isolation does not encapsulate the datatype genericity. Essential to iteration in the sense we are using the term is linear access to collection elements; this was ... |

121 | Derivable type classes
- Hinze, Jones
(Show Context)
Citation Context ...he effect of inserting the occurrences of pure and ⊛ implicitly; apart from these brackets, the definition then looks exactly like a definition of fmap. This definition could be derived automatically =-=[11]-=-, or given polytypically once and for all, assuming some universal representation of datatypes such as sums and products [10] or regular functors [7]: class Bifunctor s ⇒ Bitraversable s where bidist ... |

79 |
Using circular programs to eliminate multiple traversals of data
- Bird
- 1984
(Show Context)
Citation Context ...n lists given above is betrayed by the fact that we get instead labels "abc" = [1, 1, 3, 3, 5, 5 ]. 6. EXAMPLE As a small example of fusion of traversals, we will consider the familiar repmin problem =-=[2]-=-. The problem here is to take a binary tree of integers, compute the minimum element, then replace every element of the tree by that minimum — but to do so in a single traversal rather than the obviou... |

65 |
Distributive laws
- Beck
- 1969
(Show Context)
Citation Context ...ns of the specialized traversals collect and disperse. We also hope to investigate the categorical structure of dist further: naturality in the idiom appears to be related to Beck’s distributive laws =-=[1]-=-, and ‘no duplication’ to linear type theories. 8. ACKNOWLEDGEMENTS We are grateful to the members of IFIP WG2.1, the Algebra of Programming research group at Oxford, the Datatype-Generic Programming ... |

65 | Generic Haskell: practice and theory
- Hinze, Jeuring
- 2003
(Show Context)
Citation Context ...o apply to non-linear collections, a datatype-generic counting operation defined using fold would need a datatype-generic numeric algebra as the fold body. Such a thing could be defined polytypically =-=[12, 10]-=-, but the fact remains that fold in isolation does not encapsulate the datatype genericity. Essential to iteration in the sense we are using the term is linear access to collection elements; this was ... |

48 | Datatype-generic programming
- Gibbons
- 2007
(Show Context)
Citation Context ...datatype-generic recursion operators: maps, folds, unfolds, crushes, and monadic maps. The traversals we discuss in Section 4 generalize all of these. 2.1. Origami In the origami style of programming =-=[6, 7]-=-, the structure of programs is captured by higher-order recursion operators such as map, fold and unfold. These can be made datatype-generic [8], parametrized by the shape of the underlying datatype, ... |

27 | Calculating Functional Programs
- Gibbons
- 2002
(Show Context)
Citation Context ...datatype-generic recursion operators: maps, folds, unfolds, crushes, and monadic maps. The traversals we discuss in Section 4 generalize all of these. 2.1. Origami In the origami style of programming =-=[6, 7]-=-, the structure of programs is captured by higher-order recursion operators such as map, fold and unfold. These can be made datatype-generic [8], parametrized by the shape of the underlying datatype, ... |

26 |
Monadic Maps and Folds for Arbitrary Datatypes. Memoranda Informatica 94-28
- Fokkinga
- 1994
(Show Context)
Citation Context ...spect. 2.3. Monadic Map Haskell’s standard prelude defines a monadic map for lists, which lifts the standard map on lists to the Kleisli category: mapM :: Monad m ⇒ (a → m b) → ([a] → m [b]) Fokkinga =-=[3]-=- showed how to generalize this from lists to an arbitrary regular functor, polytypically. Several authors [23, 25, 13, 26, 18] have observed that monadic map is a promising model of iteration. Monadic... |

25 | Polytypic data conversion programs
- Jansson, Jeuring
- 2002
(Show Context)
Citation Context ...map on lists to the Kleisli category: mapM :: Monad m ⇒ (a → m b) → ([a] → m [b]) Fokkinga [3] showed how to generalize this from lists to an arbitrary regular functor, polytypically. Several authors =-=[23, 25, 13, 26, 18]-=- have observed that monadic map is a promising model of iteration. Monadic maps are very close to the idiomatic traversals that we propose as the essence of imperative iterations; indeed, for monadic ... |

20 | The functional imperative: shape
- Jay, Steckler
- 1998
(Show Context)
Citation Context ...nt to environment monads, with the logarithm as environment.) We therefore expect some further connection with data-parallel and numerically intensive computation, in the style of Jay’s language FISh =-=[14]-=-, but we leave the investigation of that connection for future work. 3.3. Monoidal Idioms Idioms strictly generalize monads; there are idioms that do not arise from monads. A third family of idioms, t... |

19 | Monadic Maps and Folds for Arbitrary Datatypes
- Fokkinga
- 1994
(Show Context)
Citation Context ...spect. 2.3. Monadic map Haskell’s standard prelude defines a monadic map for lists, which lifts the standard map on lists to the Kleisli category: mapM :: Monad m ⇒ (a → m b) → ([a] → m [b]) Fokkinga =-=[3]-=- showed how to generalize this from lists to an arbitrary regular functor, polytypically. Several authors [23, 25, 13, 26, 18] have observed that monadic map is a promising model of iteration. Monadic... |

16 |
Tupling and mutumorphisms
- Fokkinga
- 1990
(Show Context)
Citation Context ...]. We might also explore the possibility of combining some of these approaches. For example, it is clear from the definitions above that map is an instance of fold. Moreover, the banana split theorem =-=[4]-=- states that two folds in parallel on the same data structure can be fused into one. Therefore, a map and a fold in parallel fuse to a single fold, yielding both a new collection and an accumulated me... |

14 | Design patterns as higher-order datatype-generic programs
- Gibbons
- 2006
(Show Context)
Citation Context ...se. 2.1. Origami In the origami style of programming [6, 7], the structure of programs is captured by higher-order recursion operators such as map, fold and unfold. These can be made datatype-generic =-=[8]-=-, parametrized by the shape of the underlying datatype, as shown below. class Bifunctor s where bimap :: (a → b) → (c → d) → s a c → s b d data Fix s a = In{out :: s a (Fix s a)} map :: Bifunctor s ⇒ ... |

9 | When is a function a fold or an unfold
- Gibbons, Hutton, et al.
(Show Context)
Citation Context ...ss only the accumulating aspect of the C# iteration, and not the mapping aspect — it loses the shape of the original structure. Moreover, the sequence of elements is not always definable as an unfold =-=[9]-=-. We might also explore the possibility of combining some of these approaches. For example, it is clear from the definitions above that map is an instance of fold. Moreover, the banana split theorem [... |

2 |
When is a function a fold or an unfold? Electronic
- Gibbons, Hutton, et al.
(Show Context)
Citation Context ...ss only the accumulating aspect of the C# iteration, and not the mapping aspect — it loses the shape of the original structure. Moreover, the sequence of elements is not always definable as an unfold =-=[9]-=-. We might also explore the possibility of combining some of these approaches. For example, it is clear from the definitions above that map is an instance of fold. Moreover, the banana split theorem [... |