(>2)-Input Conduits

Last time I described how to create a conduit that can read from either one of two input streams. Today I want to look at conduits with more than two input streams.

The key idea in my last post was to use "stateful monad morphisms", which can be hoisted into some monad transformers (specifically, the ConduitM i o transformer).

This works great to fuse to the inner input of a 2-input conduit. When I tried a 3-input conduit, however, I realized I got the abstraction a little wrong. I start off with fuseStateful on the inner input, then statefulHoist that to the second layer. But then I'm left with a plain old function, which I can't statefulHoist again to the outer layer. Using plain-old transPipe/hoist to get back to the outer conduit is no good for the same reason that it doesn't work for the 2-input conduit: all my careful stateful fusing and hoistng gets reset whenever there is an action in the outer conduit.

Luckily this is easy to fix: I just need fuseStateful to return another StatefulMorph rather than a function:

Now I can fuse to conduits of arbitrary depth:

I have updated the repository with these changes.