Personal tools

Enumerator and iteratee

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
(link to "separation of concerns")
(link to an independent article)
Line 27: Line 27:
 
== See also ==
 
== See also ==
   
* Haskell-Cafe on [http://www.haskell.org/pipermail/haskell-cafe/2008-December/052181.html understanding enumerator/iteratee]
 
 
* Oleg Kiselyov: "[http://okmij.org/ftp/Haskell/Iteratee/DEFUN08-talk-notes.pdf Incremental multi-level input processing with left-fold enumerator] - predictable, high-performance, safe, and elegant"
 
* Oleg Kiselyov: "[http://okmij.org/ftp/Haskell/Iteratee/DEFUN08-talk-notes.pdf Incremental multi-level input processing with left-fold enumerator] - predictable, high-performance, safe, and elegant"
  +
* Haskell-Cafe on [http://www.haskell.org/pipermail/haskell-cafe/2008-December/052181.html understanding enumerator/iteratee]
  +
* Haskell-Cafe on [http://www.haskell.org/pipermail/haskell-cafe/2009-February/056816.html Left fold enumerator - a real pearl overlooked?]
  +
* John Lato's cabalized [http://inmachina.net/~jwlato/haskell/iteratee/ package] of Oleg's code
  +
* [[Iteratee I/O]]
   
 
[[Category:Idioms]]
 
[[Category:Idioms]]

Revision as of 00:21, 3 March 2009

An enumerator is something that knows how to process a data structure and an iteratee is something that does one step in processing another piece of the big data structure. E.g. to sum up all elements of Data.Map, we do

Map.fold (+) 0 mp

to sum up all elements of a set we do

Set.fold (+) 0 st
Then
fold
is the enumerator and
(+)
and
 
are the iteratee.

Ditto for any other foldable data structure. Clearly the function that sums the current element with the accumulator, (+), doesn't know or care from which collection the elements are coming from. The initial seed, 0, is again unaware of the collection.

Iteratee is indeed the function that you pass to fold (combined with the seed for practical reasons). One may conceptually consider iteratee to be a pair of the function to feed to fold, and the initial seed (the accumulator in the above example). That achieves the separation of concerns: fold (aka, enumerator) has the intimate knowledge of the collection and how to get to the next element; iteratee knows what to do with the current element.


See also