Thu Mar 17 11:53:12 EDT 2011


When you have list structures moving between layers of code, say
A->B->C, there is a choice to be made about the form of the data that
flows between the functions.  The simplest approach is to put a bunch
of data in a (large) buffer and pass it on to the next function.  An
alternative way is to pass only a single element at a time, and place
the A->B->C components inside a loop.

Taking some ideas from [1], and taking into account the limitations of
C, the optimal seems to be to provide a "fold" for each data
structure.  In C code I prefer to use the word "for".

The main advantage of such an approach is that it avoids intermediate
data structure storage such as large buffers to pass lists of elements
from one function to the next.  Using the "for" loop aproach instead
of the iterator object approach -- i.e. methods that implement first,
next, last -- has the added advantage that locking and other resource
management can be abstracted away from the callback function.

[1] http://okmij.org/ftp/Streams.html#enumerator-stream