Skip to main content
×
×
Home

The essence of the Iterator pattern

  • JEREMY GIBBONS (a1) and BRUNO C. d. S. OLIVEIRA (a1)
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.

Copyright
References
Hide All
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.
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.
Fokkinga, M. (1990) Tupling and mutumorphisms, Tupling and mutumorphisms 1 (4): 8182.
Fokkinga, M. (1994) Monadic maps and folds for arbitrary datatypes. Memoranda Informatica 94-28. Department of Computer Science, University of Twente.
Fridlender, D. & Indrika, M. (2000) Do we need dependent types? J. Funct. Program., 10 (4): 409415.
Gallagher, K. B. & Lyle, J. R. (1991) Using program slicing in software maintenance, Using program slicing in software maintenance 17 (8): 751761.
Gamma, E., Helm, R., Johnson, R., & Vlissides, J. (1995) Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley.
GHC Team. (2006) Haskell hierarchical libraries. http://www.haskell.org/ghc/docs/latest/html/libraries/.
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.
Gibbons, J. (2003) Origami programming. In The Fun of Programming, Gibbons, J. & de Moor, O. (eds), Cornerstones in Computing. Palgrave MacMillan, pp. 4160.
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.
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.
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.
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.
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.
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.
Hughes, J. (1989) Why functional programming matters, Comput. J., 32 (2): 98107.
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.
Jansson, P. & Jeuring, J. (2002) Polytypic data conversion programs. Sci. Comput. Program., 43 (1): 3575.
Jay, C. B. (1995) A semantics for shape. Sci. Comput. Program., 25, 251283.
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.
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.
Jones, M. P. & Duponcheel, L. (1993) Composing monads. Technical Report. YALEU/DCS/RR-1004. Department of Computer Science, Yale University.
Kernighan, B. W. & Ritchie, D. M. (1988) The C Programming Language. Prentice Hall.
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.
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).
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.
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.
McBride, C. & Paterson, R. (2008) Applicative programming with effects, Applicative programming with effects 18 (1): 113.
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.
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.
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.
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.
Moggi, E. (1991) Notions of computation and monads. Inform. Comput., 93 (1): 5592.
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.
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.
Peyton Jones, S. (ed.) (2003) The Haskell 98 Language and Libraries: The Revised Report. Cambridge University Press.
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.
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.
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.
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.
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.
Recommend this journal

Email your librarian or administrator to recommend adding this journal to your organisation's collection.

Journal of Functional Programming
  • ISSN: 0956-7968
  • EISSN: 1469-7653
  • URL: /core/journals/journal-of-functional-programming
Please enter your name
Please enter a valid email address
Who would you like to send this to? *
×

Metrics

Full text views

Total number of HTML views: 0
Total number of PDF views: 0 *
Loading metrics...

Abstract views

Total abstract views: 0 *
Loading metrics...

* Views captured on Cambridge Core between <date>. This data will be updated every 24 hours.

Usage data cannot currently be displayed

The essence of the Iterator pattern

  • JEREMY GIBBONS (a1) and BRUNO C. d. S. OLIVEIRA (a1)
Submit a response

Discussions

No Discussions have been published for this article.

×

Reply to: Submit a response


Your details


Conflicting interests

Do you have any conflicting interests? *