There have been some interesting papers and talks about approaches to handling i/o using left folds recently.
We explain input processing with left-fold enumerator, using as an example HTTP request processing in Haskell. The approach is general and applies to processing data from various collections, from in-memory data structures to databases, files, sockets, etc.
Our approach differs in: permitting incremental processing; i/o interleaving comes by default; we shall see an example of i/o multiplexing with no need for threads and related locking and synchronization.
Unlike lazy IO, our approach is correct. There is not even hint of UnsafePerformIO. Unlike Handle-based IO, accessing a disposed resource like a closed handle is just impossible in our approach. Our approach has some other nice properties, permitting composing streams and stream processors.
One can use the same processor to handle several streams one after another. Or use two processors to process parts of the same source. One can combine processors vertically, which is very useful when one stream is embedded (chunk-encoded, escaped, UTF8-encoded) into another. Enumerators and iteratees, which generalize fold, have nice algebraic properties. But we won’t talk about them.
Interesting weekend reading!