{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RankNTypes #-}
module Data.Conduino.Combinators (
unfold
, iterate
, repeat
, unfoldMaybe
, unfoldEither
, iterateMaybe
, iterateEither
, sourceList
, replicate
, repeatM
, repeatMaybeM
, repeatEitherM
, replicateM
, sourceHandleLines
, stdinLines
, sourceHandle
, stdin
, map
, mapM
, iterM
, scan
, mapAccum
, take
, takeWhile
, filter
, concatMap
, concat
, pairs
, consecutive
, drop
, dropWhile
, foldr
, foldl
, foldMap
, fold
, sinkNull
, sinkList
, last
, sinkHandle
, stdout
, stderr
) where
import Control.Applicative
import Control.Exception
import Control.Monad hiding (mapM, replicateM)
import Control.Monad.IO.Class
import Control.Monad.Trans.Class
import Control.Monad.Trans.Maybe
import Data.Conduino
import Data.Either
import Data.Foldable hiding (foldr, foldl, fold, concat, concatMap, foldMap)
import Data.Maybe
import Data.Semigroup
import Prelude hiding (map, iterate, mapM, replicate, repeat, foldr, drop, foldl, last, take, concatMap, filter, concat, takeWhile, dropWhile, foldMap)
import System.IO.Error
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy.Internal as BSL
import qualified Data.Sequence as Seq
import qualified System.IO as S
unfoldEither
:: (s -> Either a (o, s))
-> s
-> Pipe i o u m a
unfoldEither :: (s -> Either a (o, s)) -> s -> Pipe i o u m a
unfoldEither f :: s -> Either a (o, s)
f = s -> Pipe i o u m a
forall i u (m :: * -> *). s -> Pipe i o u m a
go
where
go :: s -> Pipe i o u m a
go z :: s
z = case s -> Either a (o, s)
f s
z of
Left r :: a
r -> a -> Pipe i o u m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
r
Right (x :: o
x, z' :: s
z') -> o -> Pipe i o u m ()
forall o i u (m :: * -> *). o -> Pipe i o u m ()
yield o
x Pipe i o u m () -> Pipe i o u m a -> Pipe i o u m a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> s -> Pipe i o u m a
go s
z'
unfoldMaybe
:: (s -> Maybe (o, s))
-> s
-> Pipe i o u m ()
unfoldMaybe :: (s -> Maybe (o, s)) -> s -> Pipe i o u m ()
unfoldMaybe f :: s -> Maybe (o, s)
f = (s -> Either () (o, s)) -> s -> Pipe i o u m ()
forall s a o i u (m :: * -> *).
(s -> Either a (o, s)) -> s -> Pipe i o u m a
unfoldEither (Either () (o, s)
-> ((o, s) -> Either () (o, s)) -> Maybe (o, s) -> Either () (o, s)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (() -> Either () (o, s)
forall a b. a -> Either a b
Left ()) (o, s) -> Either () (o, s)
forall a b. b -> Either a b
Right (Maybe (o, s) -> Either () (o, s))
-> (s -> Maybe (o, s)) -> s -> Either () (o, s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Maybe (o, s)
f)
unfold
:: (s -> (o, s))
-> s
-> Pipe i o u m a
unfold :: (s -> (o, s)) -> s -> Pipe i o u m a
unfold f :: s -> (o, s)
f = s -> Pipe i o u m a
forall i u (m :: * -> *) b. s -> Pipe i o u m b
go
where
go :: s -> Pipe i o u m b
go z :: s
z = o -> Pipe i o u m ()
forall o i u (m :: * -> *). o -> Pipe i o u m ()
yield o
x Pipe i o u m () -> Pipe i o u m b -> Pipe i o u m b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> s -> Pipe i o u m b
go s
z'
where
(x :: o
x, z' :: s
z') = s -> (o, s)
f s
z
iterateEither
:: (o -> Either a o)
-> o
-> Pipe i o u m a
iterateEither :: (o -> Either a o) -> o -> Pipe i o u m a
iterateEither f :: o -> Either a o
f = (o -> Either a (o, o)) -> o -> Pipe i o u m a
forall s a o i u (m :: * -> *).
(s -> Either a (o, s)) -> s -> Pipe i o u m a
unfoldEither ((o -> (o, o)) -> Either a o -> Either a (o, o)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((o -> o -> (o, o)) -> o -> (o, o)
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (,)) (Either a o -> Either a (o, o))
-> (o -> Either a o) -> o -> Either a (o, o)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. o -> Either a o
f)
iterateMaybe
:: (o -> Maybe o)
-> o
-> Pipe i o u m ()
iterateMaybe :: (o -> Maybe o) -> o -> Pipe i o u m ()
iterateMaybe f :: o -> Maybe o
f = (o -> Maybe (o, o)) -> o -> Pipe i o u m ()
forall s o i u (m :: * -> *).
(s -> Maybe (o, s)) -> s -> Pipe i o u m ()
unfoldMaybe ((o -> (o, o)) -> Maybe o -> Maybe (o, o)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((o -> o -> (o, o)) -> o -> (o, o)
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (,)) (Maybe o -> Maybe (o, o)) -> (o -> Maybe o) -> o -> Maybe (o, o)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. o -> Maybe o
f)
iterate
:: (o -> o)
-> o
-> Pipe i o u m a
iterate :: (o -> o) -> o -> Pipe i o u m a
iterate f :: o -> o
f = (o -> (o, o)) -> o -> Pipe i o u m a
forall s o i u (m :: * -> *) a.
(s -> (o, s)) -> s -> Pipe i o u m a
unfold ((o -> o -> (o, o)) -> o -> (o, o)
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (,) (o -> (o, o)) -> (o -> o) -> o -> (o, o)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. o -> o
f)
sourceList :: Foldable t => t a -> Pipe i a u m ()
sourceList :: t a -> Pipe i a u m ()
sourceList = (a -> Pipe i a u m ()) -> t a -> Pipe i a u m ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ a -> Pipe i a u m ()
forall o i u (m :: * -> *). o -> Pipe i o u m ()
yield
repeat :: o -> Pipe i o u m a
repeat :: o -> Pipe i o u m a
repeat = Pipe i o u m () -> Pipe i o u m a
forall (f :: * -> *) a b. Applicative f => f a -> f b
forever (Pipe i o u m () -> Pipe i o u m a)
-> (o -> Pipe i o u m ()) -> o -> Pipe i o u m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. o -> Pipe i o u m ()
forall o i u (m :: * -> *). o -> Pipe i o u m ()
yield
replicate :: Int -> o -> Pipe i o u m ()
replicate :: Int -> o -> Pipe i o u m ()
replicate n :: Int
n = Int -> Pipe i o u m () -> Pipe i o u m ()
forall (m :: * -> *) a. Applicative m => Int -> m a -> m ()
replicateM_ Int
n (Pipe i o u m () -> Pipe i o u m ())
-> (o -> Pipe i o u m ()) -> o -> Pipe i o u m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. o -> Pipe i o u m ()
forall o i u (m :: * -> *). o -> Pipe i o u m ()
yield
repeatEitherM
:: Monad m
=> m (Either a o)
-> Pipe i o u m a
repeatEitherM :: m (Either a o) -> Pipe i o u m a
repeatEitherM x :: m (Either a o)
x = Pipe i o u m a
forall i u. Pipe i o u m a
go
where
go :: Pipe i o u m a
go = m (Either a o) -> Pipe i o u m (Either a o)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Either a o)
x Pipe i o u m (Either a o)
-> (Either a o -> Pipe i o u m a) -> Pipe i o u m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Left r :: a
r -> a -> Pipe i o u m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
r
Right y :: o
y -> o -> Pipe i o u m ()
forall o i u (m :: * -> *). o -> Pipe i o u m ()
yield o
y Pipe i o u m () -> Pipe i o u m a -> Pipe i o u m a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Pipe i o u m a
go
repeatMaybeM
:: Monad m
=> m (Maybe o)
-> Pipe i o u m ()
repeatMaybeM :: m (Maybe o) -> Pipe i o u m ()
repeatMaybeM = m (Either () o) -> Pipe i o u m ()
forall (m :: * -> *) a o i u.
Monad m =>
m (Either a o) -> Pipe i o u m a
repeatEitherM (m (Either () o) -> Pipe i o u m ())
-> (m (Maybe o) -> m (Either () o))
-> m (Maybe o)
-> Pipe i o u m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe o -> Either () o) -> m (Maybe o) -> m (Either () o)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Either () o -> (o -> Either () o) -> Maybe o -> Either () o
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (() -> Either () o
forall a b. a -> Either a b
Left ()) o -> Either () o
forall a b. b -> Either a b
Right)
replicateM
:: Monad m
=> Int
-> m o
-> Pipe i o u m ()
replicateM :: Int -> m o -> Pipe i o u m ()
replicateM n :: Int
n x :: m o
x = Int -> Pipe i o u m () -> Pipe i o u m ()
forall (m :: * -> *) a. Applicative m => Int -> m a -> m ()
replicateM_ Int
n (Pipe i o u m () -> Pipe i o u m ())
-> Pipe i o u m () -> Pipe i o u m ()
forall a b. (a -> b) -> a -> b
$ m o -> Pipe i o u m o
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m o
x Pipe i o u m o -> (o -> Pipe i o u m ()) -> Pipe i o u m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= o -> Pipe i o u m ()
forall o i u (m :: * -> *). o -> Pipe i o u m ()
yield
stdinLines :: MonadIO m => Pipe i String u m ()
stdinLines :: Pipe i String u m ()
stdinLines = Handle -> Pipe i String u m ()
forall (m :: * -> *) i u.
MonadIO m =>
Handle -> Pipe i String u m ()
sourceHandleLines Handle
S.stdin
stdin :: MonadIO m => Pipe i BS.ByteString u m ()
stdin :: Pipe i ByteString u m ()
stdin = Handle -> Pipe i ByteString u m ()
forall (m :: * -> *) i u.
MonadIO m =>
Handle -> Pipe i ByteString u m ()
sourceHandle Handle
S.stdin
sourceHandleLines
:: MonadIO m
=> S.Handle
-> Pipe i String u m ()
sourceHandleLines :: Handle -> Pipe i String u m ()
sourceHandleLines h :: Handle
h = m (Maybe String) -> Pipe i String u m ()
forall (m :: * -> *) o i u.
Monad m =>
m (Maybe o) -> Pipe i o u m ()
repeatMaybeM (m (Maybe String) -> Pipe i String u m ())
-> m (Maybe String) -> Pipe i String u m ()
forall a b. (a -> b) -> a -> b
$ do
Bool
d <- IO Bool -> m Bool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> m Bool) -> IO Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ Handle -> IO Bool
S.hIsEOF Handle
h
if Bool
d
then Maybe String -> m (Maybe String)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe String
forall a. Maybe a
Nothing
else IO (Maybe String) -> m (Maybe String)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Maybe String) -> m (Maybe String))
-> ((() -> IO (Maybe String)) -> IO (Maybe String))
-> (() -> IO (Maybe String))
-> m (Maybe String)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (IOError -> Maybe ())
-> IO (Maybe String)
-> (() -> IO (Maybe String))
-> IO (Maybe String)
forall e b a.
Exception e =>
(e -> Maybe b) -> IO a -> (b -> IO a) -> IO a
catchJust
(Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Maybe ()) -> (IOError -> Bool) -> IOError -> Maybe ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IOError -> Bool
isEOFError)
((String -> Bool) -> Maybe String -> Maybe String
forall (m :: * -> *) a. MonadPlus m => (a -> Bool) -> m a -> m a
mfilter (Bool -> Bool
not (Bool -> Bool) -> (String -> Bool) -> String -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null) (Maybe String -> Maybe String)
-> (String -> Maybe String) -> String -> Maybe String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Maybe String
forall a. a -> Maybe a
Just (String -> Maybe String) -> IO String -> IO (Maybe String)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Handle -> IO String
S.hGetLine Handle
h)
((() -> IO (Maybe String)) -> m (Maybe String))
-> (() -> IO (Maybe String)) -> m (Maybe String)
forall a b. (a -> b) -> a -> b
$ \_ -> Maybe String -> IO (Maybe String)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe String
forall a. Maybe a
Nothing
sourceHandle
:: MonadIO m
=> S.Handle
-> Pipe i BS.ByteString u m ()
sourceHandle :: Handle -> Pipe i ByteString u m ()
sourceHandle h :: Handle
h = m (Maybe ByteString) -> Pipe i ByteString u m ()
forall (m :: * -> *) o i u.
Monad m =>
m (Maybe o) -> Pipe i o u m ()
repeatMaybeM
(m (Maybe ByteString) -> Pipe i ByteString u m ())
-> (IO ByteString -> m (Maybe ByteString))
-> IO ByteString
-> Pipe i ByteString u m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString -> Maybe ByteString)
-> m ByteString -> m (Maybe ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((ByteString -> Bool) -> Maybe ByteString -> Maybe ByteString
forall (m :: * -> *) a. MonadPlus m => (a -> Bool) -> m a -> m a
mfilter (Bool -> Bool
not (Bool -> Bool) -> (ByteString -> Bool) -> ByteString -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Bool
BS.null) (Maybe ByteString -> Maybe ByteString)
-> (ByteString -> Maybe ByteString)
-> ByteString
-> Maybe ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just)
(m ByteString -> m (Maybe ByteString))
-> (IO ByteString -> m ByteString)
-> IO ByteString
-> m (Maybe ByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO ByteString -> m ByteString
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
(IO ByteString -> Pipe i ByteString u m ())
-> IO ByteString -> Pipe i ByteString u m ()
forall a b. (a -> b) -> a -> b
$ Handle -> Int -> IO ByteString
BS.hGetSome Handle
h Int
BSL.defaultChunkSize
sinkHandle
:: MonadIO m
=> S.Handle
-> Pipe BS.ByteString o u m ()
sinkHandle :: Handle -> Pipe ByteString o u m ()
sinkHandle h :: Handle
h = (ByteString -> m ()) -> Pipe ByteString () u m u
forall (m :: * -> *) i o u. Monad m => (i -> m o) -> Pipe i o u m u
mapM (IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (ByteString -> IO ()) -> ByteString -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> ByteString -> IO ()
BS.hPut Handle
h)
Pipe ByteString () u m u
-> Pipe () o u m () -> Pipe ByteString o u m ()
forall (m :: * -> *) a b u v c r.
Monad m =>
Pipe a b u m v -> Pipe b c v m r -> Pipe a c u m r
.| Pipe () o u m ()
forall i o u (m :: * -> *). Pipe i o u m ()
sinkNull
stdout :: MonadIO m => Pipe BS.ByteString o u m ()
stdout :: Pipe ByteString o u m ()
stdout = Handle -> Pipe ByteString o u m ()
forall (m :: * -> *) o u.
MonadIO m =>
Handle -> Pipe ByteString o u m ()
sinkHandle Handle
S.stdout
stderr :: MonadIO m => Pipe BS.ByteString o u m ()
stderr :: Pipe ByteString o u m ()
stderr = Handle -> Pipe ByteString o u m ()
forall (m :: * -> *) o u.
MonadIO m =>
Handle -> Pipe ByteString o u m ()
sinkHandle Handle
S.stderr
repeatM
:: Monad m
=> m o
-> Pipe i o u m a
repeatM :: m o -> Pipe i o u m a
repeatM x :: m o
x = Pipe i o u m a
forall i u b. Pipe i o u m b
go
where
go :: Pipe i o u m b
go = (o -> Pipe i o u m ()
forall o i u (m :: * -> *). o -> Pipe i o u m ()
yield (o -> Pipe i o u m ()) -> Pipe i o u m o -> Pipe i o u m ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< m o -> Pipe i o u m o
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m o
x) Pipe i o u m () -> Pipe i o u m b -> Pipe i o u m b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Pipe i o u m b
go
map :: (i -> o) -> Pipe i o u m u
map :: (i -> o) -> Pipe i o u m u
map f :: i -> o
f = (i -> Pipe i o u m ()) -> Pipe i o u m u
forall i o u (m :: * -> *) a.
(i -> Pipe i o u m a) -> Pipe i o u m u
awaitForever (o -> Pipe i o u m ()
forall o i u (m :: * -> *). o -> Pipe i o u m ()
yield (o -> Pipe i o u m ()) -> (i -> o) -> i -> Pipe i o u m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. i -> o
f)
mapM :: Monad m => (i -> m o) -> Pipe i o u m u
mapM :: (i -> m o) -> Pipe i o u m u
mapM f :: i -> m o
f = (i -> Pipe i o u m ()) -> Pipe i o u m u
forall i o u (m :: * -> *) a.
(i -> Pipe i o u m a) -> Pipe i o u m u
awaitForever ((o -> Pipe i o u m ()
forall o i u (m :: * -> *). o -> Pipe i o u m ()
yield (o -> Pipe i o u m ()) -> Pipe i o u m o -> Pipe i o u m ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<) (Pipe i o u m o -> Pipe i o u m ())
-> (i -> Pipe i o u m o) -> i -> Pipe i o u m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m o -> Pipe i o u m o
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m o -> Pipe i o u m o) -> (i -> m o) -> i -> Pipe i o u m o
forall b c a. (b -> c) -> (a -> b) -> a -> c
. i -> m o
f)
iterM :: Monad m => (i -> m ()) -> Pipe i i u m u
iterM :: (i -> m ()) -> Pipe i i u m u
iterM f :: i -> m ()
f = (i -> m i) -> Pipe i i u m u
forall (m :: * -> *) i o u. Monad m => (i -> m o) -> Pipe i o u m u
mapM (\x :: i
x -> i
x i -> m () -> m i
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ i -> m ()
f i
x)
mapAccum
:: (i -> s -> (s, o))
-> s
-> Pipe i o u m u
mapAccum :: (i -> s -> (s, o)) -> s -> Pipe i o u m u
mapAccum f :: i -> s -> (s, o)
f = s -> Pipe i o u m u
forall u (m :: * -> *). s -> Pipe i o u m u
go
where
go :: s -> Pipe i o u m u
go !s
x = (i -> Pipe i o u m u) -> Pipe i o u m u
forall i o u (m :: * -> *). (i -> Pipe i o u m u) -> Pipe i o u m u
awaitWith ((i -> Pipe i o u m u) -> Pipe i o u m u)
-> (i -> Pipe i o u m u) -> Pipe i o u m u
forall a b. (a -> b) -> a -> b
$ \y :: i
y ->
let (!s
x', !o
z) = i -> s -> (s, o)
f i
y s
x
in o -> Pipe i o u m ()
forall o i u (m :: * -> *). o -> Pipe i o u m ()
yield o
z Pipe i o u m () -> Pipe i o u m u -> Pipe i o u m u
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> s -> Pipe i o u m u
go s
x'
scan
:: (o -> i -> o)
-> o
-> Pipe i o u m u
scan :: (o -> i -> o) -> o -> Pipe i o u m u
scan f :: o -> i -> o
f = o -> Pipe i o u m u
forall u (m :: * -> *). o -> Pipe i o u m u
go
where
go :: o -> Pipe i o u m u
go !o
x = (i -> Pipe i o u m u) -> Pipe i o u m u
forall i o u (m :: * -> *). (i -> Pipe i o u m u) -> Pipe i o u m u
awaitWith ((i -> Pipe i o u m u) -> Pipe i o u m u)
-> (i -> Pipe i o u m u) -> Pipe i o u m u
forall a b. (a -> b) -> a -> b
$ \y :: i
y ->
let x' :: o
x' = o -> i -> o
f o
x i
y
in o -> Pipe i o u m ()
forall o i u (m :: * -> *). o -> Pipe i o u m ()
yield o
x' Pipe i o u m () -> Pipe i o u m u -> Pipe i o u m u
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> o -> Pipe i o u m u
go o
x'
pairs :: Pipe i (i, i) u m u
pairs :: Pipe i (i, i) u m u
pairs = (i -> Pipe i (i, i) u m u) -> Pipe i (i, i) u m u
forall i o u (m :: * -> *). (i -> Pipe i o u m u) -> Pipe i o u m u
awaitWith i -> Pipe i (i, i) u m u
forall t u (m :: * -> *). t -> Pipe t (t, t) u m u
go
where
go :: t -> Pipe t (t, t) u m u
go x :: t
x = (t -> Pipe t (t, t) u m u) -> Pipe t (t, t) u m u
forall i o u (m :: * -> *). (i -> Pipe i o u m u) -> Pipe i o u m u
awaitWith ((t -> Pipe t (t, t) u m u) -> Pipe t (t, t) u m u)
-> (t -> Pipe t (t, t) u m u) -> Pipe t (t, t) u m u
forall a b. (a -> b) -> a -> b
$ \y :: t
y -> do
(t, t) -> Pipe t (t, t) u m ()
forall o i u (m :: * -> *). o -> Pipe i o u m ()
yield (t
x, t
y)
t -> Pipe t (t, t) u m u
go t
y
consecutive :: Int -> Pipe i (Seq.Seq i) u m u
consecutive :: Int -> Pipe i (Seq i) u m u
consecutive n :: Int
n = Seq i -> Pipe i (Seq i) u m u
forall i b (m :: * -> *). Seq i -> Pipe i (Seq i) b m b
go Seq i
forall a. Seq a
Seq.empty
where
go :: Seq i -> Pipe i (Seq i) b m b
go xs :: Seq i
xs = do
Seq i -> Pipe i (Seq i) b m ()
forall o i u (m :: * -> *). o -> Pipe i o u m ()
yield Seq i
xs
(i -> Pipe i (Seq i) b m b) -> Pipe i (Seq i) b m b
forall i o u (m :: * -> *). (i -> Pipe i o u m u) -> Pipe i o u m u
awaitWith ((i -> Pipe i (Seq i) b m b) -> Pipe i (Seq i) b m b)
-> (i -> Pipe i (Seq i) b m b) -> Pipe i (Seq i) b m b
forall a b. (a -> b) -> a -> b
$ \y :: i
y -> Seq i -> Pipe i (Seq i) b m b
go (Seq i -> Pipe i (Seq i) b m b)
-> (Seq i -> Seq i) -> Seq i -> Pipe i (Seq i) b m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Seq i -> Seq i
forall a. Int -> Seq a -> Seq a
Seq.drop (Seq i -> Int
forall a. Seq a -> Int
Seq.length Seq i
xs Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ 1) (Seq i -> Pipe i (Seq i) b m b) -> Seq i -> Pipe i (Seq i) b m b
forall a b. (a -> b) -> a -> b
$ (Seq i
xs Seq i -> i -> Seq i
forall a. Seq a -> a -> Seq a
Seq.:|> i
y)
take :: Int -> Pipe i i u m ()
take :: Int -> Pipe i i u m ()
take n :: Int
n = Pipe i i u m (Maybe ()) -> Pipe i i u m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Pipe i i u m (Maybe ()) -> Pipe i i u m ())
-> (MaybeT (Pipe i i u m) () -> Pipe i i u m (Maybe ()))
-> MaybeT (Pipe i i u m) ()
-> Pipe i i u m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MaybeT (Pipe i i u m) () -> Pipe i i u m (Maybe ())
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT (MaybeT (Pipe i i u m) () -> Pipe i i u m (Maybe ()))
-> (MaybeT (Pipe i i u m) () -> MaybeT (Pipe i i u m) ())
-> MaybeT (Pipe i i u m) ()
-> Pipe i i u m (Maybe ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> MaybeT (Pipe i i u m) () -> MaybeT (Pipe i i u m) ()
forall (m :: * -> *) a. Applicative m => Int -> m a -> m ()
replicateM_ Int
n (MaybeT (Pipe i i u m) () -> Pipe i i u m ())
-> MaybeT (Pipe i i u m) () -> Pipe i i u m ()
forall a b. (a -> b) -> a -> b
$
Pipe i i u m () -> MaybeT (Pipe i i u m) ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (Pipe i i u m () -> MaybeT (Pipe i i u m) ())
-> (i -> Pipe i i u m ()) -> i -> MaybeT (Pipe i i u m) ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. i -> Pipe i i u m ()
forall o i u (m :: * -> *). o -> Pipe i o u m ()
yield (i -> MaybeT (Pipe i i u m) ())
-> MaybeT (Pipe i i u m) i -> MaybeT (Pipe i i u m) ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Pipe i i u m (Maybe i) -> MaybeT (Pipe i i u m) i
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT Pipe i i u m (Maybe i)
forall i o u (m :: * -> *). Pipe i o u m (Maybe i)
await
takeWhile :: (i -> Bool) -> Pipe i i u m ()
takeWhile :: (i -> Bool) -> Pipe i i u m ()
takeWhile p :: i -> Bool
p = Pipe i i u m ()
forall u (m :: * -> *). Pipe i i u m ()
go
where
go :: Pipe i i u m ()
go = Pipe i i u m (Maybe i)
forall i o u (m :: * -> *). Pipe i o u m (Maybe i)
await Pipe i i u m (Maybe i)
-> (Maybe i -> Pipe i i u m ()) -> Pipe i i u m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Nothing -> () -> Pipe i i u m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
Just x :: i
x
| i -> Bool
p i
x -> i -> Pipe i i u m ()
forall o i u (m :: * -> *). o -> Pipe i o u m ()
yield i
x Pipe i i u m () -> Pipe i i u m () -> Pipe i i u m ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Pipe i i u m ()
go
| Bool
otherwise -> () -> Pipe i i u m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
filter
:: (i -> Bool)
-> Pipe i i u m u
filter :: (i -> Bool) -> Pipe i i u m u
filter p :: i -> Bool
p = (i -> Pipe i i u m ()) -> Pipe i i u m u
forall i o u (m :: * -> *) a.
(i -> Pipe i o u m a) -> Pipe i o u m u
awaitForever ((i -> Pipe i i u m ()) -> Pipe i i u m u)
-> (i -> Pipe i i u m ()) -> Pipe i i u m u
forall a b. (a -> b) -> a -> b
$ \x :: i
x -> Bool -> Pipe i i u m () -> Pipe i i u m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (i -> Bool
p i
x) (Pipe i i u m () -> Pipe i i u m ())
-> Pipe i i u m () -> Pipe i i u m ()
forall a b. (a -> b) -> a -> b
$ i -> Pipe i i u m ()
forall o i u (m :: * -> *). o -> Pipe i o u m ()
yield i
x
concatMap
:: Foldable t
=> (i -> t o)
-> Pipe i o u m u
concatMap :: (i -> t o) -> Pipe i o u m u
concatMap f :: i -> t o
f = (i -> Pipe i o u m ()) -> Pipe i o u m u
forall i o u (m :: * -> *) a.
(i -> Pipe i o u m a) -> Pipe i o u m u
awaitForever (t o -> Pipe i o u m ()
forall (t :: * -> *) a i u (m :: * -> *).
Foldable t =>
t a -> Pipe i a u m ()
sourceList (t o -> Pipe i o u m ()) -> (i -> t o) -> i -> Pipe i o u m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. i -> t o
f)
concat :: Foldable t => Pipe (t i) i u m u
concat :: Pipe (t i) i u m u
concat = (t i -> Pipe (t i) i u m ()) -> Pipe (t i) i u m u
forall i o u (m :: * -> *) a.
(i -> Pipe i o u m a) -> Pipe i o u m u
awaitForever t i -> Pipe (t i) i u m ()
forall (t :: * -> *) a i u (m :: * -> *).
Foldable t =>
t a -> Pipe i a u m ()
sourceList
foldr :: (a -> b -> b) -> b -> Pipe a o u m b
foldr :: (a -> b -> b) -> b -> Pipe a o u m b
foldr f :: a -> b -> b
f z :: b
z = Pipe a o u m b
forall o u (m :: * -> *). Pipe a o u m b
go
where
go :: Pipe a o u m b
go = Pipe a o u m (Maybe a)
forall i o u (m :: * -> *). Pipe i o u m (Maybe i)
await Pipe a o u m (Maybe a)
-> (Maybe a -> Pipe a o u m b) -> Pipe a o u m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Nothing -> b -> Pipe a o u m b
forall (f :: * -> *) a. Applicative f => a -> f a
pure b
z
Just x :: a
x -> a -> b -> b
f a
x (b -> b) -> Pipe a o u m b -> Pipe a o u m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Pipe a o u m b
go
foldl :: (b -> a -> b) -> b -> Pipe a o u m b
foldl :: (b -> a -> b) -> b -> Pipe a o u m b
foldl f :: b -> a -> b
f = b -> Pipe a o u m b
forall o u (m :: * -> *). b -> Pipe a o u m b
go
where
go :: b -> Pipe a o u m b
go !b
z = Pipe a o u m (Maybe a)
forall i o u (m :: * -> *). Pipe i o u m (Maybe i)
await Pipe a o u m (Maybe a)
-> (Maybe a -> Pipe a o u m b) -> Pipe a o u m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Nothing -> b -> Pipe a o u m b
forall (f :: * -> *) a. Applicative f => a -> f a
pure b
z
Just !a
x -> b -> Pipe a o u m b
go (b -> a -> b
f b
z a
x)
fold :: Monoid a => Pipe a o u m a
fold :: Pipe a o u m a
fold = (a -> a -> a) -> a -> Pipe a o u m a
forall b a o u (m :: * -> *). (b -> a -> b) -> b -> Pipe a o u m b
foldl a -> a -> a
forall a. Semigroup a => a -> a -> a
(<>) a
forall a. Monoid a => a
mempty
foldMap :: Monoid a => (i -> a) -> Pipe i o u m a
foldMap :: (i -> a) -> Pipe i o u m a
foldMap f :: i -> a
f = (a -> i -> a) -> a -> Pipe i o u m a
forall b a o u (m :: * -> *). (b -> a -> b) -> b -> Pipe a o u m b
foldl (\x :: a
x y :: i
y -> a
x a -> a -> a
forall a. Semigroup a => a -> a -> a
<> i -> a
f i
y) a
forall a. Monoid a => a
mempty
sinkList :: Pipe i o u m [i]
sinkList :: Pipe i o u m [i]
sinkList = (i -> [i] -> [i]) -> [i] -> Pipe i o u m [i]
forall a b o u (m :: * -> *). (a -> b -> b) -> b -> Pipe a o u m b
foldr (:) []
drop :: Int -> Pipe i o u m ()
drop :: Int -> Pipe i o u m ()
drop n :: Int
n = Int -> Pipe i o u m (Maybe i) -> Pipe i o u m ()
forall (m :: * -> *) a. Applicative m => Int -> m a -> m ()
replicateM_ Int
n Pipe i o u m (Maybe i)
forall i o u (m :: * -> *). Pipe i o u m (Maybe i)
await
dropWhile
:: (i -> Bool)
-> Pipe i o u m ()
dropWhile :: (i -> Bool) -> Pipe i o u m ()
dropWhile p :: i -> Bool
p = Pipe i o u m ()
forall o u (m :: * -> *). Pipe i o u m ()
go
where
go :: Pipe i o u m ()
go = Pipe i o u m (Maybe i)
forall i o u (m :: * -> *). Pipe i o u m (Maybe i)
await Pipe i o u m (Maybe i)
-> (Maybe i -> Pipe i o u m ()) -> Pipe i o u m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Nothing -> () -> Pipe i o u m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
Just x :: i
x
| i -> Bool
p i
x -> Pipe i o u m ()
go
| Bool
otherwise -> () -> Pipe i o u m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
sinkNull :: Pipe i o u m ()
sinkNull :: Pipe i o u m ()
sinkNull = Pipe i o u m (Maybe i)
forall i o u (m :: * -> *). Pipe i o u m (Maybe i)
await Pipe i o u m (Maybe i)
-> (Maybe i -> Pipe i o u m ()) -> Pipe i o u m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Nothing -> () -> Pipe i o u m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
Just _ -> Pipe i o u m ()
forall i o u (m :: * -> *). Pipe i o u m ()
sinkNull
last :: Pipe i o u m (Maybe i)
last :: Pipe i o u m (Maybe i)
last = (Last i -> i) -> Maybe (Last i) -> Maybe i
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Last i -> i
forall a. Last a -> a
getLast (Maybe (Last i) -> Maybe i)
-> Pipe i o u m (Maybe (Last i)) -> Pipe i o u m (Maybe i)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (i -> Maybe (Last i)) -> Pipe i o u m (Maybe (Last i))
forall a i o u (m :: * -> *).
Monoid a =>
(i -> a) -> Pipe i o u m a
foldMap (Last i -> Maybe (Last i)
forall a. a -> Maybe a
Just (Last i -> Maybe (Last i)) -> (i -> Last i) -> i -> Maybe (Last i)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. i -> Last i
forall a. a -> Last a
Last)