Browse files

Added Control.Pipe.Tee.

Combinators which act like idP, but also divert a copy of the input to a
Consumer. Similar to the unix 'tee' command.
  • Loading branch information...
1 parent a8de943 commit dc6ea607491819580bf0e24e01faca2afe792941 @stepcut stepcut committed with Mar 8, 2012
Showing with 46 additions and 0 deletions.
  1. +45 −0 pipes-extra/Control/Pipe/Tee.hs
  2. +1 −0 pipes-extra/pipes-extra.cabal
View
45 pipes-extra/Control/Pipe/Tee.hs
@@ -0,0 +1,45 @@
+{- |
+Module : Control.Pipe.Tee
+Description : Combinators similar to the unix 'tee' command.
+Copyright : (c) 2012 Jeremy Shaw
+License : BSD3
+
+Maintainer : jeremy@n-heptane.com
+Stability : experimental
+Portability : portable
+
+The 'tee' combinators act like 'idP', but also send a copy of the input to the
+supplied 'Consumer', a file, etc. This is typically done for the purpose of
+logging a pipeline, showing progress, etc.
+-}
+module Control.Pipe.Tee where
+
+import Control.Monad.Trans (MonadIO(..))
+import Control.Pipe (Pipe, (>+>), firstP, discardL, idP, pipe)
+import Control.Pipe.Binary (fileWriter)
+import Control.Pipe.Monoidal ((***), splitP)
+import Data.ByteString (ByteString)
+import Data.Void (Void)
+
+-- | acts like 'idP', but also passes a copy to the supplied consumer
+tee :: (Monad m) =>
+ Pipe a Void m r -- ^ 'Consumer' that will receive a copy of all the input
+ -> Pipe a a m r
+tee consumer =
+ splitP >+> firstP consumer >+> discardL
+
+-- | acts like 'idP', but also writes a copy to a file
+teeFile :: (MonadIO m) =>
+ (a -> ByteString) -- ^ function to convert the value to a 'ByteString' which can be written to the log
+ -> FilePath -- ^ file to log to
+ -> Pipe a a m ()
+teeFile showBS logFile =
+ tee (pipe showBS >+> fileWriter logFile)
+
+-- | acts like, 'idP', but also writes a copy to the specified log file
+--
+-- just an alias for @teeFile id@
+teeFileBS :: (MonadIO m) =>
+ FilePath -- ^ file to log to
+ -> Pipe ByteString ByteString m ()
+teeFileBS = teeFile id
View
1 pipes-extra/pipes-extra.cabal
@@ -23,4 +23,5 @@ Library
Control.Pipe.Binary,
Control.Pipe.Coroutine,
Control.Pipe.ChunkPipe,
+ Control.Pipe.Tee,
Control.Pipe.Zip

0 comments on commit dc6ea60

Please sign in to comment.