module Piped.Extras where
import Data.Function
import Piped.Internal
sourceList :: (Monad m, Foldable t) => t o -> Pipe () o m ()
sourceList = foldMap yield
{-# INLINE sourceList #-}
sinkList :: Pipe i Void m [i]
sinkList = Pipe $
\rest l r ->
fix2 l id $
\go l fxs ->
let term = rest termLeft r $ fxs []
in runAwait l term $ \i l -> go l $ fxs . (i:)
{-# INLINE sinkList #-}
awaitForever :: Monad m => (i -> Pipe i o m a) -> Pipe i o m ()
awaitForever f = Pipe $
\rest ->
fix $
\next await yield ->
runAwait await (rest termLeft yield ()) $
\i await ->
unPipe (f i) (\l r _ -> next l r) await yield
{-# INLINE awaitForever #-}
sinkNull :: Monad m => Pipe i Void m ()
sinkNull = Pipe $
\rest l r -> do
let term = rest termLeft r ()
f l = runAwait l term $ \_ -> f
in f l
{-# INLINE sinkNull #-}
awaitMaybe :: Pipe i o m a -> (i -> Pipe i o m a) -> Pipe i o m a
awaitMaybe def act = Pipe $
\rest l r ->
runAwait l (unPipe def rest termLeft r)
(\i l -> unPipe (act i) rest l r)
{-# INLINE awaitMaybe #-}
awaitJust :: Monad m => (i -> Pipe i o m ()) -> Pipe i o m ()
awaitJust act = Pipe $
\rest l r ->
runAwait l (rest termLeft r ()) $
\i l -> unPipe (act i) rest l r
{-# INLINE awaitJust #-}
yieldM :: Monad m => Pipe i o m o -> Pipe i o m ()
yieldM = (>>= yield)
{-# INLINE yieldM #-}
bracketPipe :: Monad m => m b -> (b -> m ()) -> (b -> Pipe i o m a) -> Pipe i o m a
bracketPipe alloc free inside = Pipe $
\rest l r -> do
b <- alloc
let rest' l r a = free b >> rest l r a
unPipe (inside b) rest' l r