[Haskell-cafe] "branching" conduits

Simon Marechal simon at banquise.net
Thu Jan 31 14:24:10 CET 2013

On 31/01/2013 13:50, Michael Snoyman wrote:
> * To simplify, we start off with a call to injectLeftovers. This means
> that we can entirely ignore the Leftover constructor in the main function.
> * Since a Sink will never yield values, we can also ignore the
> HaveOutput constructor.
> * As soon as either of the Sinks terminates, we terminate the other one
> as well and return the results.

Your gist is extremely informative to me. I figured it would be
something along these lines, but was very scared to try it myself. I
have however realized that my first use case doesn't cover my need, as I
will want to feed an arbitrary set of sinks with any value ...

I started coding right after I sent that mail and wrote this:

It is not very elegant as the "branching" functions outputs [Int].

I haven't tested it yet, but it should branch with any number of sinks.
Another point that might (or might not) be of interest is the
distribution of distinct branches on separate threads.

> You can also consider going the mutable container route if you like.
> Instead of creating a lot of stuff from scratch with MVars, you could
> use stm-conduit[2]. In fact, that package already contains some kind of
> merging behavior for sources, it might make sense to ask the author
> about including unmerging behavior for Sinks.

I did not think of bounded channels. They are a indeed a better match
than MVars !

I can see it uses resourceForkIO, which I believe is OK for sources that
will be used in your 'main' thread. But for multiple Sinks, you need a
way to wait for the all Sinks to terminate. I used stuff from
Control.Concurrent.ParallelIO, but I am not sure it is ideal.

More information about the Haskell-Cafe mailing list