You should look into iteratees. It's the same insight: if you have the concept of something that receives values and eventually yields a value of a given type, that's a structure that has some very nice algebraic properties (e.g. they're monads).
It would be like a function that takes
- a Collector that produces an X
- a function that takes an X and returns a Collector that produces an Y
and returns a Collector that produces an Y.
The thing is: while being fed, the result Collector should first feed the initial Collector and, at some time, "switch" to the Collector produced by the function. But when to perform the switch?