Forking the streaming flow in haskell-pipes -


i'm having trouble directing flow though pipeline haskell-pipes. basically, analyze bunch of files , have either

  1. print results terminal in human-friendly way
  2. encode results json

the chosen path depends upon command line option.
in second case, have output opening bracket, every incoming value followed comma , closing bracket. insertcommas never terminates, closing bracket never outputted.

import pipes import data.bytestring.lazy b import data.aeson (encode)  insertcommas :: consumer b.bytestring io () insertcommas =     first <- await     lift $ b.putstr first     cat $ \obj -> lift $         putstr ","         b.putstr obj  jsonexporter :: consumer (filepath, analysisresult) io () jsonexporter =     lift $ putstr "["     p.map encode >-> insertcommas     lift $ putstr "]"  exportstream :: config -> consumer (filepath, analysisresult) io () exportstream conf =     case outputmode conf of       json -> jsonexporter       _    -> p.map (export conf) >-> p.stdoutln  main :: io () main =     -- first 2 lines docopt stuff, not relevant     args <- parseargsorexit patterns =<< getargs     ins  <- allfiles $ args `getallargs` argument "paths"     let conf = readconfig args     runeffect $ each ins              >-> p.mapm analyze              >-> p.map (filterresults conf)              >-> p.filter filternulls              >-> exportstream conf 

i think should 'commify' pipes-group. has intercalates, not intersperse, it's not big deal write. should stay away consumer end, think, sort of problem.

{-#language overloadedstrings #-} import pipes import qualified pipes.prelude p import qualified data.bytestring.lazy.char8 b import pipes.group import lens.simple  -- or control.lens or lens.micro or view/^. import system.environment  intersperse_ :: monad m => -> producer m r -> producer m r intersperse_ producer = intercalates (yield a) (producer ^. chunksof 1)   main =    args <- getargs   let op prod = case args of          "json":_ -> yield "[" *> intersperse_ "," prod <* yield "]"         _        -> intersperse_ " " prod   runeffect $ op producer >-> p.mapm_ b.putstr   putstrln ""        producer = mapm_ yield (b.words "this test") 

which give me this

    >>> :main json     [this,is,a,test]     >>> :main ---     test 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -