Hostname: page-component-8448b6f56d-gtxcr Total loading time: 0 Render date: 2024-04-18T07:29:35.068Z Has data issue: false hasContentIssue false

The essence of the Iterator pattern

Published online by Cambridge University Press:  01 July 2009

JEREMY GIBBONS
Affiliation:
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)
BRUNO C. d. S. OLIVEIRA
Affiliation:
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)
Rights & Permissions [Opens in a new window]

Abstract

Core share and HTML view are not available for this content. However, as you have access to this content, a full PDF is available via the ‘Save PDF’ action button.

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.

Type
Articles
Copyright
Copyright © Cambridge University Press 2009

References

Beck, J. (1969) Distributive laws. In Seminar on Triples and Categorical Homology Theory, ETH Zürich, 1966/67, Lecture Notes in Math., vol. 80. Springer, pp. 119140.Google Scholar
Bird, R. S. & Meertens, L. (1998) Nested datatypes. In Proceedings of the 4th International Conference on Mathematics of Program Construction, MPC '98 (Marstrand, June 1998), Jeuring, J. (ed.), Lecture Notes in Computer Science, vol. 1422. Springer, pp. 5267.Google Scholar
Fokkinga, M. (1990) Tupling and mutumorphisms, Tupling and mutumorphisms 1 (4): 8182.Google Scholar
Fokkinga, M. (1994) Monadic maps and folds for arbitrary datatypes. Memoranda Informatica 94-28. Department of Computer Science, University of Twente.Google Scholar
Fridlender, D. & Indrika, M. (2000) Do we need dependent types? J. Funct. Program., 10 (4): 409415.Google Scholar
Gallagher, K. B. & Lyle, J. R. (1991) Using program slicing in software maintenance, Using program slicing in software maintenance 17 (8): 751761.Google Scholar
Gamma, E., Helm, R., Johnson, R., & Vlissides, J. (1995) Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley.Google Scholar
GHC Team. (2006) Haskell hierarchical libraries. http://www.haskell.org/ghc/docs/latest/html/libraries/.Google Scholar
Gibbons, J. (2002) Calculating functional programs. In Revised Lectures from International Summer School and Workshop on Algebraic and Coalgebraic Methods in the Mathematics of Program Construction, ACMMPC 2000, Oxford, Apr. 2000, Backhouse, R., Crole, R., & Gibbons, J. (eds), Lecture Notes in Computer Science, vol. 2297. Springer, pp. 148203.Google Scholar
Gibbons, J. (2003) Origami programming. In The Fun of Programming, Gibbons, J. & de Moor, O. (eds), Cornerstones in Computing. Palgrave MacMillan, pp. 4160.Google Scholar
Gibbons, J. (2006a) Design patterns as higher-order datatype-generic programs. In Proceedings of the 2006 ACM SIGPLAN Workshop on Generic Programming, WGP '06 (Portland, OR, Sept. 2006). ACM Press, pp. 112.Google Scholar
Gibbons, J. (2006b) Fission for program comprehension. In Proceedings of the 8th International Conference on Mathematics of Program Construction, MPC 2006 (Kuressaare, July 2006), Uustalu, T. (ed.), Lecture Notes in Computer Science, vol. 4014. Springer, pp. 162179.Google Scholar
Gibbons, J., Hutton, G. & Altenkirch, T. (2001) When is a function a fold or an unfold? In Proceedings of the 4th Workshop on Coalgebraic Methods in Computer Science, CMCS 2001 (Genova, Apr. 2001), Corradini, A., Lenisa, M., & Montanari, U. (eds), Electronic Notes in Theoretical Computer Science, vol. 44(1). Elsevier, pp. 146160.Google Scholar
Gibbons, J. & Oliveira, B. C. d. S. (2006) The essence of the iterator pattern. In Proceedings of the Workshop on Mathematically Structured Functional Programming, MSFP 2006 (Kuressaare, July 2006), McBride, C. & Uustalu, T. (eds), Electronic Workshops in Computing. BCS.Google Scholar
Hinze, R. & Jeuring, J. (2003) Generic Haskell: Practice and theory. In Advanced Lectures from International Summer School and Workshop on Generic Programming, SSGP 2002, Oxford, Aug. 2002, Backhouse, R. & Gibbons, J. (eds), Lecture Notes in Computer Science, vol. 2793. Springer, pp. 156.Google Scholar
Hinze, R. & PeytonJones, S. Jones, S. (2001) Derivable type classes. In Proceedings of the 2000 ACM SIGPLAN Haskell Workshop, Haskell 2000 (Montreal, Sept. 2000), Hutton, G. (ed.), Electronic Notes in Theoretical Computer Science, vol. 41(1). Elsevier, pp. 535.Google Scholar
Hughes, J. (1989) Why functional programming matters, Comput. J., 32 (2): 98107.Google Scholar
Jansson, P. & Jeuring, J. (1997) PolyP – A polytypic programming language extension. In Conference Record of 24th ACM SIGPLAN-SIGACT Symposium Principles of Programming Languages, POPL '97, Paris, Jan. 1997. ACM Press, pp. 470482.Google Scholar
Jansson, P. & Jeuring, J. (2002) Polytypic data conversion programs. Sci. Comput. Program., 43 (1): 3575.Google Scholar
Jay, C. B. (1995) A semantics for shape. Sci. Comput. Program., 25, 251283.CrossRefGoogle Scholar
Jay, C. B. & Steckler, P. (1998) The functional imperative: Shape! In Proceedings of the 7th European Symposium on Programming, ESOP '98 (Lisbon, March/Apr. 1998), Hankin, C. (ed.), Lecture Notes in Computer Science, vol. 1381. Springer, pp. 139153.Google Scholar
Jones, M. P. (1995) Functional programming with overloading and higher-order polymorphism. In Tutorial Text from 1st International Spring School on Advanced Functional Programming Techniques, AFP '95, Båstad, May 1995, Jeuring, J. & Meijer, E. (eds), Lecture Notes in Computer Science, vol. 925. Springer, pp. 97136.Google Scholar
Jones, M. P. & Duponcheel, L. (1993) Composing monads. Technical Report. YALEU/DCS/RR-1004. Department of Computer Science, Yale University.Google Scholar
Kernighan, B. W. & Ritchie, D. M. (1988) The C Programming Language. Prentice Hall.Google Scholar
King, D. J. & Wadler, P. (1993) Combining monads. In Proceedings of the 1992 Glasgow Workshop on Functional Programming, (Ayr, July 1992), Launchbury, J., & Sansom, P. M. (eds), Workshops in Computing. Springer, pp. 134143.Google Scholar
Kiselyov, O. & Lämmel, R. (2005) Haskell's overlooked object system. ArXiv preprint arXiv:cs/0509027v1. Available at http://arxiv.org/abs/cs/0509027v1. (Accessed 4 May 2009).Google Scholar
Kühne, T. (1999) Internal iteration externalized. In Proceedings of the 13th European Conference on Object-Oriented Programming, ECOOP '99 (Lisbon, June 1999), Guerraoui, R. (ed.), Lecture Notes in Computer Science, vol. 1628. Springer, pp. 329350.CrossRefGoogle Scholar
Leroy, X. (1995) Applicative functors and fully transparent higher-order modules. In Conference Record of 22nd ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL '95, San Francisco, CA, Jan. 1995. ACM Press, pp. 142153.Google Scholar
McBride, C. & Paterson, R. (2008) Applicative programming with effects, Applicative programming with effects 18 (1): 113.Google Scholar
Meertens, L. (1996) Calculate polytypically! In Proceedings of the 8th International Symposium on Programming Languages: Implementations, Logics, and Programs, PLILP '96 (Aachen, Sept. 1996), Kuchen, H. & Swierstra, S. D. (eds), Lecture Notes in Computer Science, vol. 1140. Springer, pp. 116.Google Scholar
Meertens, L. (1998) Functor pulling. In Proceedings of the Workshop on Generic Programming, WGP '98 (Marstrand, June 1998), Backhouse, R. & Sheard, T. (eds), Department of Computing Science, Chalmers University of Technology.Google Scholar
Meijer, E., Fokkinga, M. & Paterson, R. (1991) Functional programming with bananas, lenses, envelopes and barbed wire. In Procedings of the 5th ACM Conference on Functional Programming Languages and Computer Architecture, FPCA '91 (Cambridge, MA, Aug. 1991), Hughes, J. (ed.), Lecture Notes in Computer Science, vol. 523. Springer, pp. 124144.Google Scholar
Meijer, E. & Jeuring, J. (1995) Merging monads and folds for functional programming. In Tutorial Text from 1st International Spring School on Advanced Functional Programming Techniques, AFP '95, Båstad, May 1995, Jeuring, J. & Meijer, E. (eds), Lecture Notes in Computer Science, vol. 925. Springer, pp. 228266.Google Scholar
Moggi, E. (1991) Notions of computation and monads. Inform. Comput., 93 (1): 5592.Google Scholar
Moggi, E., Bellè, G. & Jay, C. B. (1999) Monads, shapely functors and traversals. In Proceedings of the 8th International Conference on Category Theory and Computer Science, CTCS '99 (Edinburgh, Sept. 1999), Hofmann, M., Pavlovic, D., & Rosolini, P. (eds), Electronic Notes in Theoretical Computer Science, vol. 29. Elsevier, pp. 187208.Google Scholar
Pardo, A. (2005) Combining datatypes and effects. In Revised Lectures from 5th International School on Advanced Functional Programming, AFP 2004, Tartu, Aug. 2004, Vene, V. & Uustalu, T. (eds), Lecture Notes in Computer Science, vol. 3622. Springer, pp. 171209.Google Scholar
Peyton Jones, S. (ed.) (2003) The Haskell 98 Language and Libraries: The Revised Report. Cambridge University Press.Google Scholar
Peyton Jones, S. L. & Wadler, P. (1993) Imperative functional programming. In Proceedings of the 20th ACM Symposium on Principles of Programming Languages, POPL '93 (Charleston, SC, Jan. 1993). ACM Press, pp. 7184.Google Scholar
Reynolds, J. C. (1983) Types, abstraction and parametric polymorphism. In Proceedings of the 9th IFIP World Computer Congress, Information Processing '83 (Paris, Sept. 1983), Mason, R. E. A. (ed.). North-Holland, pp. 513523.Google Scholar
Villavicencio, G. & Oliveira, J. N. (2001) Reverse program calculation supported by code slicing. In Proceedings of the 8th Working Conference on Reverse Engineering, WCRE 2001 (Stuttgart, Oct. 2001). IEEE CS Press, pp. 3548.Google Scholar
Wadler, P. (1989) Theorems for free! In Proceedings of the 4th International Conference on Functional Programming Languages and Computer Architecture, FPCA '89 (London, Sept. 1989). ACM Press, pp. 347359.Google Scholar
Wadler, P. (1993) Monads for functional programming. In Proceedings of NATO ASI on Program Design Calculi (Marktoberdorf, Aug. 1992), Broy, M. (ed.), NATO ASI Series F, vol. 118. Springer, pp. 233264.Google Scholar
Submit a response

Discussions

No Discussions have been published for this article.