Monoid Instance on Conduit

This past weekend I had reason to use conduit to process the contents of a large number of files as a single stream. Fortunately conduit supplies a Monoid instance. Monoid allows us  to append things together. This example is very short but very useful. This little code block creates many sources and collapses them together into a single source that will be ordered by the file name. Quite cool.

let source = foldl1 (<>) $ map sourceFile $ sort files
runResourceT $ do
  source $$
    someConduit chain ...

Thanks to drb226 for his suggestions.


So obviously there are other ways of writing this. Tekmo suggested using the >> binad and discard operator which would make our code look like:

let source = foldl1 (<>) $ map sourceFile $ sort files

Sjoerd Visscher suggested using foldMap from Data.Foldable. So that gives us:

let source = foldMap sourceFile $ sort files

Thanks for the feedback.

  1. I suggest, based only on that you started with foldl1, using foldMap1 from Foldable1, doing away with Monoid and requiring only Semigroup. As a style issue, prefer: f . g $ x instead of f $ g $ x.

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>