module Data.Conduit.Util
(
zip
, zipSources
, zipSinks
, passthroughSink
) where
import Prelude hiding (zip)
import Data.Conduit.Internal (Pipe (..), Source, Sink, ConduitM (..), Conduit, awaitForever, yield, await, zipSinks, zipSources)
import Data.Void (absurd)
import Control.Monad.Trans.Class (lift)
zip :: Monad m => Source m a -> Source m b -> Source m (a, b)
zip = zipSources
passthroughSink :: Monad m
=> Sink i m r
-> (r -> m ())
-> Conduit i m i
passthroughSink (ConduitM sink0) final =
ConduitM $ go [] sink0
where
go _ (Done r) = do
lift $ final r
awaitForever yield
go is (Leftover sink i) = go (i:is) sink
go _ (HaveOutput _ _ o) = absurd o
go is (PipeM mx) = do
x <- lift mx
go is x
go (i:is) (NeedInput next _) = go is (next i)
go [] (NeedInput next done) = do
mx <- await
case mx of
Nothing -> go [] (done ())
Just x -> do
yield x
go [] (next x)