I use Haskell stream processing library pipes to write a command line tool. Each command line actions may output result to stdout
and logs to stderr
with pipes
API.
I need Consumer
which has type as Consumer (Either String String) m r
to print chunk of data (Left
to stderr
, Right
to stdout
) with single Consumer
.
Code I wrote (should be improved)
This function consumeEither
doesn't have flexibility so I want to improve it.
consumeEither :: (MonadIO m) => Consumer (Either String String) m ()
consumeEither = do
eitherS <- await
case eitherS of
(Left l) -> for (yield l) (liftIO . (IO.hPutStrLn IO.stderr))
(Right r) -> for (yiled r) (liftIO . putStrLn)
Furthermore it would be useful to provide a function which takes two Consumer
s and merge them into one Consumer
.
Question
Does anybody know good example or implementation of the following interface?
merge :: (Monad m) => Consumer a m r -> Consumer b m r -> Consumer (Either a b) m r
- 1st argument as
stderr
- 2nd argument as
stdout
Usage of the function
import Pipes
import qualified Pipes.Prelude as P
import qualified System.IO as IO
stdoutOrErr :: Consumer (Either String String) IO ()
stdoutOrErr = merge (P.toHandle IO.stderr) P.stdoutLn
Thanks