{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE Trustworthy #-}
module Data.Conduit.Internal.List.Stream where

import           Control.Monad (liftM)
import           Data.Conduit.Internal.Fusion
import qualified Data.Foldable as F

--FIXME: Should streamSource / streamSourcePure be used for sources?

unfoldS :: Monad m
        => (b -> Maybe (a, b))
        -> b
        -> StreamProducer m a
unfoldS :: (b -> Maybe (a, b)) -> b -> StreamProducer m a
unfoldS b -> Maybe (a, b)
f b
s0 Stream m i ()
_ =
    (b -> m (Step b a ())) -> m b -> Stream m a ()
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream b -> m (Step b a ())
forall (m :: * -> *). Monad m => b -> m (Step b a ())
step (b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
s0)
  where
    step :: b -> m (Step b a ())
step b
s = Step b a () -> m (Step b a ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step b a () -> m (Step b a ())) -> Step b a () -> m (Step b a ())
forall a b. (a -> b) -> a -> b
$
        case b -> Maybe (a, b)
f b
s of
            Maybe (a, b)
Nothing -> () -> Step b a ()
forall s o r. r -> Step s o r
Stop ()
            Just (a
x, b
s') -> b -> a -> Step b a ()
forall s o r. s -> o -> Step s o r
Emit b
s' a
x
{-# INLINE unfoldS #-}

unfoldEitherS :: Monad m
              => (b -> Either r (a, b))
              -> b
              -> StreamConduitT i a m r
unfoldEitherS :: (b -> Either r (a, b)) -> b -> StreamConduitT i a m r
unfoldEitherS b -> Either r (a, b)
f b
s0 Stream m i ()
_ =
    (b -> m (Step b a r)) -> m b -> Stream m a r
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream b -> m (Step b a r)
forall (m :: * -> *). Monad m => b -> m (Step b a r)
step (b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
s0)
  where
    step :: b -> m (Step b a r)
step b
s = Step b a r -> m (Step b a r)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step b a r -> m (Step b a r)) -> Step b a r -> m (Step b a r)
forall a b. (a -> b) -> a -> b
$
        case b -> Either r (a, b)
f b
s of
            Left r
r        -> r -> Step b a r
forall s o r. r -> Step s o r
Stop r
r
            Right (a
x, b
s') -> b -> a -> Step b a r
forall s o r. s -> o -> Step s o r
Emit b
s' a
x
{-# INLINE unfoldEitherS #-}

unfoldMS :: Monad m
         => (b -> m (Maybe (a, b)))
         -> b
         -> StreamProducer m a
unfoldMS :: (b -> m (Maybe (a, b))) -> b -> StreamProducer m a
unfoldMS b -> m (Maybe (a, b))
f b
s0 Stream m i ()
_ =
    (b -> m (Step b a ())) -> m b -> Stream m a ()
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream b -> m (Step b a ())
step (b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
s0)
  where
    step :: b -> m (Step b a ())
step b
s = do
        Maybe (a, b)
ms' <- b -> m (Maybe (a, b))
f b
s
        Step b a () -> m (Step b a ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step b a () -> m (Step b a ())) -> Step b a () -> m (Step b a ())
forall a b. (a -> b) -> a -> b
$ case Maybe (a, b)
ms' of
            Maybe (a, b)
Nothing -> () -> Step b a ()
forall s o r. r -> Step s o r
Stop ()
            Just (a
x, b
s') -> b -> a -> Step b a ()
forall s o r. s -> o -> Step s o r
Emit b
s' a
x
{-# INLINE unfoldMS #-}

unfoldEitherMS :: Monad m
         => (b -> m (Either r (a, b)))
         -> b
         -> StreamConduitT i a m r
unfoldEitherMS :: (b -> m (Either r (a, b))) -> b -> StreamConduitT i a m r
unfoldEitherMS b -> m (Either r (a, b))
f b
s0 Stream m i ()
_ =
    (b -> m (Step b a r)) -> m b -> Stream m a r
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream b -> m (Step b a r)
step (b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
s0)
  where
    step :: b -> m (Step b a r)
step b
s = do
        Either r (a, b)
ms' <- b -> m (Either r (a, b))
f b
s
        Step b a r -> m (Step b a r)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step b a r -> m (Step b a r)) -> Step b a r -> m (Step b a r)
forall a b. (a -> b) -> a -> b
$ case Either r (a, b)
ms' of
            Left r
r        -> r -> Step b a r
forall s o r. r -> Step s o r
Stop r
r
            Right (a
x, b
s') -> b -> a -> Step b a r
forall s o r. s -> o -> Step s o r
Emit b
s' a
x
{-# INLINE unfoldEitherMS #-}
sourceListS :: Monad m => [a] -> StreamProducer m a
sourceListS :: [a] -> StreamProducer m a
sourceListS [a]
xs0 Stream m i ()
_ =
    ([a] -> m (Step [a] a ())) -> m [a] -> Stream m a ()
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream (Step [a] a () -> m (Step [a] a ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step [a] a () -> m (Step [a] a ()))
-> ([a] -> Step [a] a ()) -> [a] -> m (Step [a] a ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Step [a] a ()
forall o. [o] -> Step [o] o ()
step) ([a] -> m [a]
forall (m :: * -> *) a. Monad m => a -> m a
return [a]
xs0)
  where
    step :: [o] -> Step [o] o ()
step [] = () -> Step [o] o ()
forall s o r. r -> Step s o r
Stop ()
    step (o
x:[o]
xs) = [o] -> o -> Step [o] o ()
forall s o r. s -> o -> Step s o r
Emit [o]
xs o
x
{-# INLINE sourceListS #-}

enumFromToS :: (Enum a, Prelude.Ord a, Monad m)
            => a
            -> a
            -> StreamProducer m a
enumFromToS :: a -> a -> StreamProducer m a
enumFromToS a
x0 a
y Stream m i ()
_ =
    (a -> m (Step a a ())) -> m a -> Stream m a ()
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream a -> m (Step a a ())
forall (m :: * -> *). Monad m => a -> m (Step a a ())
step (a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x0)
  where
    step :: a -> m (Step a a ())
step a
x = Step a a () -> m (Step a a ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step a a () -> m (Step a a ())) -> Step a a () -> m (Step a a ())
forall a b. (a -> b) -> a -> b
$ if a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
Prelude.> a
y
        then () -> Step a a ()
forall s o r. r -> Step s o r
Stop ()
        else a -> a -> Step a a ()
forall s o r. s -> o -> Step s o r
Emit (a -> a
forall a. Enum a => a -> a
Prelude.succ a
x) a
x
{-# INLINE [0] enumFromToS #-}

enumFromToS_int :: (Prelude.Integral a, Monad m)
                => a
                -> a
                -> StreamProducer m a
enumFromToS_int :: a -> a -> StreamProducer m a
enumFromToS_int a
x0 a
y Stream m i ()
_ = a
x0 a -> Stream m a () -> Stream m a ()
`seq` a
y a -> Stream m a () -> Stream m a ()
`seq` (a -> m (Step a a ())) -> m a -> Stream m a ()
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream a -> m (Step a a ())
forall (m :: * -> *). Monad m => a -> m (Step a a ())
step (a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x0)
  where
    step :: a -> m (Step a a ())
step a
x | a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
y    = Step a a () -> m (Step a a ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step a a () -> m (Step a a ())) -> Step a a () -> m (Step a a ())
forall a b. (a -> b) -> a -> b
$ a -> a -> Step a a ()
forall s o r. s -> o -> Step s o r
Emit (a
x a -> a -> a
forall a. Num a => a -> a -> a
Prelude.+ a
1) a
x
           | Bool
otherwise = Step a a () -> m (Step a a ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step a a () -> m (Step a a ())) -> Step a a () -> m (Step a a ())
forall a b. (a -> b) -> a -> b
$ () -> Step a a ()
forall s o r. r -> Step s o r
Stop ()
{-# INLINE enumFromToS_int #-}

{-# RULES "conduit: enumFromTo<Int>" forall f t.
      enumFromToS f t = enumFromToS_int f t :: Monad m => StreamProducer m Int
  #-}

iterateS :: Monad m => (a -> a) -> a -> StreamProducer m a
iterateS :: (a -> a) -> a -> StreamProducer m a
iterateS a -> a
f a
x0 Stream m i ()
_ =
    (a -> m (Step a a ())) -> m a -> Stream m a ()
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream (Step a a () -> m (Step a a ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step a a () -> m (Step a a ()))
-> (a -> Step a a ()) -> a -> m (Step a a ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Step a a ()
forall r. a -> Step a a r
step) (a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x0)
  where
    step :: a -> Step a a r
step a
x = a -> a -> Step a a r
forall s o r. s -> o -> Step s o r
Emit a
x' a
x
      where
        x' :: a
x' = a -> a
f a
x
{-# INLINE iterateS #-}

replicateS :: Monad m => Int -> a -> StreamProducer m a
replicateS :: Int -> a -> StreamProducer m a
replicateS Int
cnt0 a
a Stream m i ()
_ =
    (Int -> m (Step Int a ())) -> m Int -> Stream m a ()
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream Int -> m (Step Int a ())
forall s (m :: * -> *).
(Ord s, Num s, Monad m) =>
s -> m (Step s a ())
step (Int -> m Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
cnt0)
  where
    step :: s -> m (Step s a ())
step s
cnt
        | s
cnt s -> s -> Bool
forall a. Ord a => a -> a -> Bool
<= s
0  = Step s a () -> m (Step s a ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s a () -> m (Step s a ())) -> Step s a () -> m (Step s a ())
forall a b. (a -> b) -> a -> b
$ () -> Step s a ()
forall s o r. r -> Step s o r
Stop ()
        | Bool
otherwise = Step s a () -> m (Step s a ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s a () -> m (Step s a ())) -> Step s a () -> m (Step s a ())
forall a b. (a -> b) -> a -> b
$ s -> a -> Step s a ()
forall s o r. s -> o -> Step s o r
Emit (s
cnt s -> s -> s
forall a. Num a => a -> a -> a
- s
1) a
a
{-# INLINE replicateS #-}

replicateMS :: Monad m => Int -> m a -> StreamProducer m a
replicateMS :: Int -> m a -> StreamProducer m a
replicateMS Int
cnt0 m a
ma Stream m i ()
_ =
    (Int -> m (Step Int a ())) -> m Int -> Stream m a ()
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream Int -> m (Step Int a ())
forall s. (Ord s, Num s) => s -> m (Step s a ())
step (Int -> m Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
cnt0)
  where
    step :: s -> m (Step s a ())
step s
cnt
        | s
cnt s -> s -> Bool
forall a. Ord a => a -> a -> Bool
<= s
0  = Step s a () -> m (Step s a ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s a () -> m (Step s a ())) -> Step s a () -> m (Step s a ())
forall a b. (a -> b) -> a -> b
$ () -> Step s a ()
forall s o r. r -> Step s o r
Stop ()
        | Bool
otherwise = s -> a -> Step s a ()
forall s o r. s -> o -> Step s o r
Emit (s
cnt s -> s -> s
forall a. Num a => a -> a -> a
- s
1) (a -> Step s a ()) -> m a -> m (Step s a ())
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` m a
ma
{-# INLINE replicateMS #-}

foldS :: Monad m => (b -> a -> b) -> b -> StreamConsumer a m b
foldS :: (b -> a -> b) -> b -> StreamConsumer a m b
foldS b -> a -> b
f b
b0 (Stream s -> m (Step s a ())
step m s
ms0) =
    ((b, s) -> m (Step (b, s) o b)) -> m (b, s) -> Stream m o b
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream (b, s) -> m (Step (b, s) o b)
forall o. (b, s) -> m (Step (b, s) o b)
step' ((s -> (b, s)) -> m s -> m (b, s)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (b
b0, ) m s
ms0)
  where
    step' :: (b, s) -> m (Step (b, s) o b)
step' (!b
b, s
s) = do
        Step s a ()
res <- s -> m (Step s a ())
step s
s
        Step (b, s) o b -> m (Step (b, s) o b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (b, s) o b -> m (Step (b, s) o b))
-> Step (b, s) o b -> m (Step (b, s) o b)
forall a b. (a -> b) -> a -> b
$ case Step s a ()
res of
            Stop () -> b -> Step (b, s) o b
forall s o r. r -> Step s o r
Stop b
b
            Skip s
s' -> (b, s) -> Step (b, s) o b
forall s o r. s -> Step s o r
Skip (b
b, s
s')
            Emit s
s' a
a -> (b, s) -> Step (b, s) o b
forall s o r. s -> Step s o r
Skip (b -> a -> b
f b
b a
a, s
s')
{-# INLINE foldS #-}

foldMS :: Monad m => (b -> a -> m b) -> b -> StreamConsumer a m b
foldMS :: (b -> a -> m b) -> b -> StreamConsumer a m b
foldMS b -> a -> m b
f b
b0 (Stream s -> m (Step s a ())
step m s
ms0) =
    ((b, s) -> m (Step (b, s) o b)) -> m (b, s) -> Stream m o b
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream (b, s) -> m (Step (b, s) o b)
forall o. (b, s) -> m (Step (b, s) o b)
step' ((s -> (b, s)) -> m s -> m (b, s)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (b
b0, ) m s
ms0)
  where
    step' :: (b, s) -> m (Step (b, s) o b)
step' (!b
b, s
s) = do
        Step s a ()
res <- s -> m (Step s a ())
step s
s
        case Step s a ()
res of
            Stop () -> Step (b, s) o b -> m (Step (b, s) o b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (b, s) o b -> m (Step (b, s) o b))
-> Step (b, s) o b -> m (Step (b, s) o b)
forall a b. (a -> b) -> a -> b
$ b -> Step (b, s) o b
forall s o r. r -> Step s o r
Stop b
b
            Skip s
s' -> Step (b, s) o b -> m (Step (b, s) o b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (b, s) o b -> m (Step (b, s) o b))
-> Step (b, s) o b -> m (Step (b, s) o b)
forall a b. (a -> b) -> a -> b
$ (b, s) -> Step (b, s) o b
forall s o r. s -> Step s o r
Skip (b
b, s
s')
            Emit s
s' a
a -> do
                b
b' <- b -> a -> m b
f b
b a
a
                Step (b, s) o b -> m (Step (b, s) o b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (b, s) o b -> m (Step (b, s) o b))
-> Step (b, s) o b -> m (Step (b, s) o b)
forall a b. (a -> b) -> a -> b
$ (b, s) -> Step (b, s) o b
forall s o r. s -> Step s o r
Skip (b
b', s
s')
{-# INLINE foldMS #-}

mapM_S :: Monad m
       => (a -> m ())
       -> StreamConsumer a m ()
mapM_S :: (a -> m ()) -> StreamConsumer a m ()
mapM_S a -> m ()
f (Stream s -> m (Step s a ())
step m s
ms0) =
    (s -> m (Step s o ())) -> m s -> Stream m o ()
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream s -> m (Step s o ())
forall o. s -> m (Step s o ())
step' m s
ms0
  where
    step' :: s -> m (Step s o ())
step' s
s = do
        Step s a ()
res <- s -> m (Step s a ())
step s
s
        case Step s a ()
res of
          Stop () -> Step s o () -> m (Step s o ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s o () -> m (Step s o ())) -> Step s o () -> m (Step s o ())
forall a b. (a -> b) -> a -> b
$ () -> Step s o ()
forall s o r. r -> Step s o r
Stop ()
          Skip s
s' -> Step s o () -> m (Step s o ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s o () -> m (Step s o ())) -> Step s o () -> m (Step s o ())
forall a b. (a -> b) -> a -> b
$ s -> Step s o ()
forall s o r. s -> Step s o r
Skip s
s'
          Emit s
s' a
x -> a -> m ()
f a
x m () -> m (Step s o ()) -> m (Step s o ())
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Step s o () -> m (Step s o ())
forall (m :: * -> *) a. Monad m => a -> m a
return (s -> Step s o ()
forall s o r. s -> Step s o r
Skip s
s')
{-# INLINE [1] mapM_S #-}

dropS :: Monad m
      => Int
      -> StreamConsumer a m ()
dropS :: Int -> StreamConsumer a m ()
dropS Int
n0 (Stream s -> m (Step s a ())
step m s
ms0) =
    ((s, Int) -> m (Step (s, Int) o ())) -> m (s, Int) -> Stream m o ()
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream (s, Int) -> m (Step (s, Int) o ())
forall b o. (Ord b, Num b) => (s, b) -> m (Step (s, b) o ())
step' ((s -> (s, Int)) -> m s -> m (s, Int)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (, Int
n0) m s
ms0)
  where
    step' :: (s, b) -> m (Step (s, b) o ())
step' (s
_, b
n) | b
n b -> b -> Bool
forall a. Ord a => a -> a -> Bool
<= b
0 = Step (s, b) o () -> m (Step (s, b) o ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, b) o () -> m (Step (s, b) o ()))
-> Step (s, b) o () -> m (Step (s, b) o ())
forall a b. (a -> b) -> a -> b
$ () -> Step (s, b) o ()
forall s o r. r -> Step s o r
Stop ()
    step' (s
s, b
n) = do
        Step s a ()
res <- s -> m (Step s a ())
step s
s
        Step (s, b) o () -> m (Step (s, b) o ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, b) o () -> m (Step (s, b) o ()))
-> Step (s, b) o () -> m (Step (s, b) o ())
forall a b. (a -> b) -> a -> b
$ case Step s a ()
res of
            Stop () -> () -> Step (s, b) o ()
forall s o r. r -> Step s o r
Stop ()
            Skip s
s' -> (s, b) -> Step (s, b) o ()
forall s o r. s -> Step s o r
Skip (s
s', b
n)
            Emit s
s' a
_ -> (s, b) -> Step (s, b) o ()
forall s o r. s -> Step s o r
Skip (s
s', b
n b -> b -> b
forall a. Num a => a -> a -> a
- b
1)
{-# INLINE dropS #-}

takeS :: Monad m
      => Int
      -> StreamConsumer a m [a]
takeS :: Int -> StreamConsumer a m [a]
takeS Int
n0 (Stream s -> m (Step s a ())
step m s
s0) =
    (([a] -> [a], Int, s) -> m (Step ([a] -> [a], Int, s) o [a]))
-> m ([a] -> [a], Int, s) -> Stream m o [a]
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream ([a] -> [a], Int, s) -> m (Step ([a] -> [a], Int, s) o [a])
forall b c o.
(Ord b, Num b) =>
([a] -> c, b, s) -> m (Step ([a] -> c, b, s) o c)
step' ((s -> ([a] -> [a], Int, s)) -> m s -> m ([a] -> [a], Int, s)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM ([a] -> [a]
forall a. a -> a
id, Int
n0,) m s
s0)
  where
    step' :: ([a] -> c, b, s) -> m (Step ([a] -> c, b, s) o c)
step' ([a] -> c
output, b
n, s
_) | b
n b -> b -> Bool
forall a. Ord a => a -> a -> Bool
<= b
0 = Step ([a] -> c, b, s) o c -> m (Step ([a] -> c, b, s) o c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step ([a] -> c, b, s) o c -> m (Step ([a] -> c, b, s) o c))
-> Step ([a] -> c, b, s) o c -> m (Step ([a] -> c, b, s) o c)
forall a b. (a -> b) -> a -> b
$ c -> Step ([a] -> c, b, s) o c
forall s o r. r -> Step s o r
Stop ([a] -> c
output [])
    step' ([a] -> c
output, b
n, s
s) = do
        Step s a ()
res <- s -> m (Step s a ())
step s
s
        Step ([a] -> c, b, s) o c -> m (Step ([a] -> c, b, s) o c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step ([a] -> c, b, s) o c -> m (Step ([a] -> c, b, s) o c))
-> Step ([a] -> c, b, s) o c -> m (Step ([a] -> c, b, s) o c)
forall a b. (a -> b) -> a -> b
$ case Step s a ()
res of
            Stop () -> c -> Step ([a] -> c, b, s) o c
forall s o r. r -> Step s o r
Stop ([a] -> c
output [])
            Skip s
s' -> ([a] -> c, b, s) -> Step ([a] -> c, b, s) o c
forall s o r. s -> Step s o r
Skip ([a] -> c
output, b
n, s
s')
            Emit s
s' a
x -> ([a] -> c, b, s) -> Step ([a] -> c, b, s) o c
forall s o r. s -> Step s o r
Skip ([a] -> c
output ([a] -> c) -> ([a] -> [a]) -> [a] -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:), b
n b -> b -> b
forall a. Num a => a -> a -> a
- b
1, s
s')
{-# INLINE takeS #-}

headS :: Monad m => StreamConsumer a m (Maybe a)
headS :: StreamConsumer a m (Maybe a)
headS (Stream s -> m (Step s a ())
step m s
s0) =
    (s -> m (Step s o (Maybe a))) -> m s -> Stream m o (Maybe a)
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream s -> m (Step s o (Maybe a))
forall o. s -> m (Step s o (Maybe a))
step' m s
s0
  where
    step' :: s -> m (Step s o (Maybe a))
step' s
s = do
        Step s a ()
res <- s -> m (Step s a ())
step s
s
        Step s o (Maybe a) -> m (Step s o (Maybe a))
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s o (Maybe a) -> m (Step s o (Maybe a)))
-> Step s o (Maybe a) -> m (Step s o (Maybe a))
forall a b. (a -> b) -> a -> b
$ case Step s a ()
res of
            Stop () -> Maybe a -> Step s o (Maybe a)
forall s o r. r -> Step s o r
Stop Maybe a
forall a. Maybe a
Nothing
            Skip s
s' -> s -> Step s o (Maybe a)
forall s o r. s -> Step s o r
Skip s
s'
            Emit s
_ a
x -> Maybe a -> Step s o (Maybe a)
forall s o r. r -> Step s o r
Stop (a -> Maybe a
forall a. a -> Maybe a
Just a
x)
{-# INLINE headS #-}

mapS :: Monad m => (a -> b) -> StreamConduit a m b
mapS :: (a -> b) -> StreamConduit a m b
mapS a -> b
f (Stream s -> m (Step s a ())
step m s
ms0) =
    (s -> m (Step s b ())) -> m s -> Stream m b ()
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream s -> m (Step s b ())
step' m s
ms0
  where
    step' :: s -> m (Step s b ())
step' s
s = do
        Step s a ()
res <- s -> m (Step s a ())
step s
s
        Step s b () -> m (Step s b ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s b () -> m (Step s b ())) -> Step s b () -> m (Step s b ())
forall a b. (a -> b) -> a -> b
$ case Step s a ()
res of
            Stop ()
r -> () -> Step s b ()
forall s o r. r -> Step s o r
Stop ()
r
            Emit s
s' a
a -> s -> b -> Step s b ()
forall s o r. s -> o -> Step s o r
Emit s
s' (a -> b
f a
a)
            Skip s
s' -> s -> Step s b ()
forall s o r. s -> Step s o r
Skip s
s'
{-# INLINE mapS #-}

mapMS :: Monad m => (a -> m b) -> StreamConduit a m b
mapMS :: (a -> m b) -> StreamConduit a m b
mapMS a -> m b
f (Stream s -> m (Step s a ())
step m s
ms0) =
    (s -> m (Step s b ())) -> m s -> Stream m b ()
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream s -> m (Step s b ())
step' m s
ms0
  where
    step' :: s -> m (Step s b ())
step' s
s = do
        Step s a ()
res <- s -> m (Step s a ())
step s
s
        case Step s a ()
res of
            Stop ()
r -> Step s b () -> m (Step s b ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s b () -> m (Step s b ())) -> Step s b () -> m (Step s b ())
forall a b. (a -> b) -> a -> b
$ () -> Step s b ()
forall s o r. r -> Step s o r
Stop ()
r
            Emit s
s' a
a -> s -> b -> Step s b ()
forall s o r. s -> o -> Step s o r
Emit s
s' (b -> Step s b ()) -> m b -> m (Step s b ())
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` a -> m b
f a
a
            Skip s
s' -> Step s b () -> m (Step s b ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s b () -> m (Step s b ())) -> Step s b () -> m (Step s b ())
forall a b. (a -> b) -> a -> b
$ s -> Step s b ()
forall s o r. s -> Step s o r
Skip s
s'
{-# INLINE mapMS #-}

iterMS :: Monad m => (a -> m ()) -> StreamConduit a m a
iterMS :: (a -> m ()) -> StreamConduit a m a
iterMS a -> m ()
f (Stream s -> m (Step s a ())
step m s
ms0) =
    (s -> m (Step s a ())) -> m s -> Stream m a ()
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream s -> m (Step s a ())
step' m s
ms0
  where
    step' :: s -> m (Step s a ())
step' s
s = do
        Step s a ()
res <- s -> m (Step s a ())
step s
s
        case Step s a ()
res of
            Stop () -> Step s a () -> m (Step s a ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s a () -> m (Step s a ())) -> Step s a () -> m (Step s a ())
forall a b. (a -> b) -> a -> b
$ () -> Step s a ()
forall s o r. r -> Step s o r
Stop ()
            Skip s
s' -> Step s a () -> m (Step s a ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s a () -> m (Step s a ())) -> Step s a () -> m (Step s a ())
forall a b. (a -> b) -> a -> b
$ s -> Step s a ()
forall s o r. s -> Step s o r
Skip s
s'
            Emit s
s' a
x -> a -> m ()
f a
x m () -> m (Step s a ()) -> m (Step s a ())
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Step s a () -> m (Step s a ())
forall (m :: * -> *) a. Monad m => a -> m a
return (s -> a -> Step s a ()
forall s o r. s -> o -> Step s o r
Emit s
s' a
x)
{-# INLINE iterMS #-}

mapMaybeS :: Monad m => (a -> Maybe b) -> StreamConduit a m b
mapMaybeS :: (a -> Maybe b) -> StreamConduit a m b
mapMaybeS a -> Maybe b
f (Stream s -> m (Step s a ())
step m s
ms0) =
    (s -> m (Step s b ())) -> m s -> Stream m b ()
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream s -> m (Step s b ())
step' m s
ms0
  where
    step' :: s -> m (Step s b ())
step' s
s = do
        Step s a ()
res <- s -> m (Step s a ())
step s
s
        Step s b () -> m (Step s b ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s b () -> m (Step s b ())) -> Step s b () -> m (Step s b ())
forall a b. (a -> b) -> a -> b
$ case Step s a ()
res of
            Stop () -> () -> Step s b ()
forall s o r. r -> Step s o r
Stop ()
            Skip s
s' -> s -> Step s b ()
forall s o r. s -> Step s o r
Skip s
s'
            Emit s
s' a
x ->
                case a -> Maybe b
f a
x of
                    Just b
y -> s -> b -> Step s b ()
forall s o r. s -> o -> Step s o r
Emit s
s' b
y
                    Maybe b
Nothing -> s -> Step s b ()
forall s o r. s -> Step s o r
Skip s
s'
{-# INLINE mapMaybeS #-}

mapMaybeMS :: Monad m => (a -> m (Maybe b)) -> StreamConduit a m b
mapMaybeMS :: (a -> m (Maybe b)) -> StreamConduit a m b
mapMaybeMS a -> m (Maybe b)
f (Stream s -> m (Step s a ())
step m s
ms0) =
    (s -> m (Step s b ())) -> m s -> Stream m b ()
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream s -> m (Step s b ())
step' m s
ms0
  where
    step' :: s -> m (Step s b ())
step' s
s = do
        Step s a ()
res <- s -> m (Step s a ())
step s
s
        case Step s a ()
res of
            Stop () -> Step s b () -> m (Step s b ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s b () -> m (Step s b ())) -> Step s b () -> m (Step s b ())
forall a b. (a -> b) -> a -> b
$ () -> Step s b ()
forall s o r. r -> Step s o r
Stop ()
            Skip s
s' -> Step s b () -> m (Step s b ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s b () -> m (Step s b ())) -> Step s b () -> m (Step s b ())
forall a b. (a -> b) -> a -> b
$ s -> Step s b ()
forall s o r. s -> Step s o r
Skip s
s'
            Emit s
s' a
x -> do
                Maybe b
my <- a -> m (Maybe b)
f a
x
                case Maybe b
my of
                    Just b
y -> Step s b () -> m (Step s b ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s b () -> m (Step s b ())) -> Step s b () -> m (Step s b ())
forall a b. (a -> b) -> a -> b
$ s -> b -> Step s b ()
forall s o r. s -> o -> Step s o r
Emit s
s' b
y
                    Maybe b
Nothing -> Step s b () -> m (Step s b ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s b () -> m (Step s b ())) -> Step s b () -> m (Step s b ())
forall a b. (a -> b) -> a -> b
$ s -> Step s b ()
forall s o r. s -> Step s o r
Skip s
s'
{-# INLINE mapMaybeMS #-}

catMaybesS :: Monad m => StreamConduit (Maybe a) m a
catMaybesS :: StreamConduit (Maybe a) m a
catMaybesS (Stream s -> m (Step s (Maybe a) ())
step m s
ms0) =
    (s -> m (Step s a ())) -> m s -> Stream m a ()
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream s -> m (Step s a ())
step' m s
ms0
  where
    step' :: s -> m (Step s a ())
step' s
s = do
        Step s (Maybe a) ()
res <- s -> m (Step s (Maybe a) ())
step s
s
        Step s a () -> m (Step s a ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s a () -> m (Step s a ())) -> Step s a () -> m (Step s a ())
forall a b. (a -> b) -> a -> b
$ case Step s (Maybe a) ()
res of
            Stop () -> () -> Step s a ()
forall s o r. r -> Step s o r
Stop ()
            Skip s
s' -> s -> Step s a ()
forall s o r. s -> Step s o r
Skip s
s'
            Emit s
s' Maybe a
Nothing -> s -> Step s a ()
forall s o r. s -> Step s o r
Skip s
s'
            Emit s
s' (Just a
x) -> s -> a -> Step s a ()
forall s o r. s -> o -> Step s o r
Emit s
s' a
x
{-# INLINE catMaybesS #-}

concatS :: (Monad m, F.Foldable f) => StreamConduit (f a) m a
concatS :: StreamConduit (f a) m a
concatS (Stream s -> m (Step s (f a) ())
step m s
ms0) =
    (([a], s) -> m (Step ([a], s) a ())) -> m ([a], s) -> Stream m a ()
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream ([a], s) -> m (Step ([a], s) a ())
step' ((s -> ([a], s)) -> m s -> m ([a], s)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM ([], ) m s
ms0)
  where
    step' :: ([a], s) -> m (Step ([a], s) a ())
step' ([], s
s) = do
        Step s (f a) ()
res <- s -> m (Step s (f a) ())
step s
s
        Step ([a], s) a () -> m (Step ([a], s) a ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step ([a], s) a () -> m (Step ([a], s) a ()))
-> Step ([a], s) a () -> m (Step ([a], s) a ())
forall a b. (a -> b) -> a -> b
$ case Step s (f a) ()
res of
            Stop () -> () -> Step ([a], s) a ()
forall s o r. r -> Step s o r
Stop ()
            Skip s
s' -> ([a], s) -> Step ([a], s) a ()
forall s o r. s -> Step s o r
Skip ([], s
s')
            Emit s
s' f a
x -> ([a], s) -> Step ([a], s) a ()
forall s o r. s -> Step s o r
Skip (f a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList f a
x, s
s')
    step' ((a
x:[a]
xs), s
s) = Step ([a], s) a () -> m (Step ([a], s) a ())
forall (m :: * -> *) a. Monad m => a -> m a
return (([a], s) -> a -> Step ([a], s) a ()
forall s o r. s -> o -> Step s o r
Emit ([a]
xs, s
s) a
x)
{-# INLINE concatS #-}

concatMapS :: Monad m => (a -> [b]) -> StreamConduit a m b
concatMapS :: (a -> [b]) -> StreamConduit a m b
concatMapS a -> [b]
f (Stream s -> m (Step s a ())
step m s
ms0) =
    (([b], s) -> m (Step ([b], s) b ())) -> m ([b], s) -> Stream m b ()
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream ([b], s) -> m (Step ([b], s) b ())
step' ((s -> ([b], s)) -> m s -> m ([b], s)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM ([], ) m s
ms0)
  where
    step' :: ([b], s) -> m (Step ([b], s) b ())
step' ([], s
s) = do
        Step s a ()
res <- s -> m (Step s a ())
step s
s
        Step ([b], s) b () -> m (Step ([b], s) b ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step ([b], s) b () -> m (Step ([b], s) b ()))
-> Step ([b], s) b () -> m (Step ([b], s) b ())
forall a b. (a -> b) -> a -> b
$ case Step s a ()
res of
            Stop () -> () -> Step ([b], s) b ()
forall s o r. r -> Step s o r
Stop ()
            Skip s
s' -> ([b], s) -> Step ([b], s) b ()
forall s o r. s -> Step s o r
Skip ([], s
s')
            Emit s
s' a
x -> ([b], s) -> Step ([b], s) b ()
forall s o r. s -> Step s o r
Skip (a -> [b]
f a
x, s
s')
    step' ((b
x:[b]
xs), s
s) = Step ([b], s) b () -> m (Step ([b], s) b ())
forall (m :: * -> *) a. Monad m => a -> m a
return (([b], s) -> b -> Step ([b], s) b ()
forall s o r. s -> o -> Step s o r
Emit ([b]
xs, s
s) b
x)
{-# INLINE concatMapS #-}

concatMapMS :: Monad m => (a -> m [b]) -> StreamConduit a m b
concatMapMS :: (a -> m [b]) -> StreamConduit a m b
concatMapMS a -> m [b]
f (Stream s -> m (Step s a ())
step m s
ms0) =
    (([b], s) -> m (Step ([b], s) b ())) -> m ([b], s) -> Stream m b ()
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream ([b], s) -> m (Step ([b], s) b ())
step' ((s -> ([b], s)) -> m s -> m ([b], s)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM ([], ) m s
ms0)
  where
    step' :: ([b], s) -> m (Step ([b], s) b ())
step' ([], s
s) = do
        Step s a ()
res <- s -> m (Step s a ())
step s
s
        case Step s a ()
res of
            Stop () -> Step ([b], s) b () -> m (Step ([b], s) b ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step ([b], s) b () -> m (Step ([b], s) b ()))
-> Step ([b], s) b () -> m (Step ([b], s) b ())
forall a b. (a -> b) -> a -> b
$ () -> Step ([b], s) b ()
forall s o r. r -> Step s o r
Stop ()
            Skip s
s' -> Step ([b], s) b () -> m (Step ([b], s) b ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step ([b], s) b () -> m (Step ([b], s) b ()))
-> Step ([b], s) b () -> m (Step ([b], s) b ())
forall a b. (a -> b) -> a -> b
$ ([b], s) -> Step ([b], s) b ()
forall s o r. s -> Step s o r
Skip ([], s
s')
            Emit s
s' a
x -> do
                [b]
xs <- a -> m [b]
f a
x
                Step ([b], s) b () -> m (Step ([b], s) b ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step ([b], s) b () -> m (Step ([b], s) b ()))
-> Step ([b], s) b () -> m (Step ([b], s) b ())
forall a b. (a -> b) -> a -> b
$ ([b], s) -> Step ([b], s) b ()
forall s o r. s -> Step s o r
Skip ([b]
xs, s
s')
    step' ((b
x:[b]
xs), s
s) = Step ([b], s) b () -> m (Step ([b], s) b ())
forall (m :: * -> *) a. Monad m => a -> m a
return (([b], s) -> b -> Step ([b], s) b ()
forall s o r. s -> o -> Step s o r
Emit ([b]
xs, s
s) b
x)
{-# INLINE concatMapMS #-}

concatMapAccumS :: Monad m => (a -> accum -> (accum, [b])) -> accum -> StreamConduit a m b
concatMapAccumS :: (a -> accum -> (accum, [b])) -> accum -> StreamConduit a m b
concatMapAccumS a -> accum -> (accum, [b])
f  accum
initial (Stream s -> m (Step s a ())
step m s
ms0) =
    ((accum, [b], s) -> m (Step (accum, [b], s) b ()))
-> m (accum, [b], s) -> Stream m b ()
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream (accum, [b], s) -> m (Step (accum, [b], s) b ())
step' ((s -> (accum, [b], s)) -> m s -> m (accum, [b], s)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (accum
initial, [], ) m s
ms0)
  where
    step' :: (accum, [b], s) -> m (Step (accum, [b], s) b ())
step' (accum
accum, [], s
s) = do
        Step s a ()
res <- s -> m (Step s a ())
step s
s
        Step (accum, [b], s) b () -> m (Step (accum, [b], s) b ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (accum, [b], s) b () -> m (Step (accum, [b], s) b ()))
-> Step (accum, [b], s) b () -> m (Step (accum, [b], s) b ())
forall a b. (a -> b) -> a -> b
$ case Step s a ()
res of
            Stop () -> () -> Step (accum, [b], s) b ()
forall s o r. r -> Step s o r
Stop ()
            Skip s
s' -> (accum, [b], s) -> Step (accum, [b], s) b ()
forall s o r. s -> Step s o r
Skip (accum
accum, [], s
s')
            Emit s
s' a
x ->
                let (accum
accum', [b]
xs) = a -> accum -> (accum, [b])
f a
x accum
accum
                in (accum, [b], s) -> Step (accum, [b], s) b ()
forall s o r. s -> Step s o r
Skip (accum
accum', [b]
xs, s
s')
    step' (accum
accum, (b
x:[b]
xs), s
s) = Step (accum, [b], s) b () -> m (Step (accum, [b], s) b ())
forall (m :: * -> *) a. Monad m => a -> m a
return ((accum, [b], s) -> b -> Step (accum, [b], s) b ()
forall s o r. s -> o -> Step s o r
Emit (accum
accum, [b]
xs, s
s) b
x)
{-# INLINE concatMapAccumS #-}

mapAccumS :: Monad m => (a -> s -> (s, b)) -> s -> StreamConduitT a b m s
mapAccumS :: (a -> s -> (s, b)) -> s -> StreamConduitT a b m s
mapAccumS a -> s -> (s, b)
f s
initial (Stream s -> m (Step s a ())
step m s
ms0) =
    ((s, s) -> m (Step (s, s) b s)) -> m (s, s) -> Stream m b s
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream (s, s) -> m (Step (s, s) b s)
step' ((s -> (s, s)) -> m s -> m (s, s)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (s
initial, ) m s
ms0)
  where
    step' :: (s, s) -> m (Step (s, s) b s)
step' (s
accum, s
s) = do
        Step s a ()
res <- s -> m (Step s a ())
step s
s
        Step (s, s) b s -> m (Step (s, s) b s)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, s) b s -> m (Step (s, s) b s))
-> Step (s, s) b s -> m (Step (s, s) b s)
forall a b. (a -> b) -> a -> b
$ case Step s a ()
res of
            Stop () -> s -> Step (s, s) b s
forall s o r. r -> Step s o r
Stop s
accum
            Skip s
s' -> (s, s) -> Step (s, s) b s
forall s o r. s -> Step s o r
Skip (s
accum, s
s')
            Emit s
s' a
x ->
                let (s
accum', b
r) = a -> s -> (s, b)
f a
x s
accum
                in (s, s) -> b -> Step (s, s) b s
forall s o r. s -> o -> Step s o r
Emit (s
accum', s
s') b
r
{-# INLINE mapAccumS #-}

mapAccumMS :: Monad m => (a -> s -> m (s, b)) -> s -> StreamConduitT a b m s
mapAccumMS :: (a -> s -> m (s, b)) -> s -> StreamConduitT a b m s
mapAccumMS a -> s -> m (s, b)
f s
initial (Stream s -> m (Step s a ())
step m s
ms0) =
    ((s, s) -> m (Step (s, s) b s)) -> m (s, s) -> Stream m b s
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream (s, s) -> m (Step (s, s) b s)
step' ((s -> (s, s)) -> m s -> m (s, s)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (s
initial, ) m s
ms0)
  where
    step' :: (s, s) -> m (Step (s, s) b s)
step' (s
accum, s
s) = do
        Step s a ()
res <- s -> m (Step s a ())
step s
s
        case Step s a ()
res of
            Stop () -> Step (s, s) b s -> m (Step (s, s) b s)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, s) b s -> m (Step (s, s) b s))
-> Step (s, s) b s -> m (Step (s, s) b s)
forall a b. (a -> b) -> a -> b
$ s -> Step (s, s) b s
forall s o r. r -> Step s o r
Stop s
accum
            Skip s
s' -> Step (s, s) b s -> m (Step (s, s) b s)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, s) b s -> m (Step (s, s) b s))
-> Step (s, s) b s -> m (Step (s, s) b s)
forall a b. (a -> b) -> a -> b
$ (s, s) -> Step (s, s) b s
forall s o r. s -> Step s o r
Skip (s
accum, s
s')
            Emit s
s' a
x -> do
                (s
accum', b
r) <- a -> s -> m (s, b)
f a
x s
accum
                Step (s, s) b s -> m (Step (s, s) b s)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, s) b s -> m (Step (s, s) b s))
-> Step (s, s) b s -> m (Step (s, s) b s)
forall a b. (a -> b) -> a -> b
$ (s, s) -> b -> Step (s, s) b s
forall s o r. s -> o -> Step s o r
Emit (s
accum', s
s') b
r
{-# INLINE mapAccumMS #-}

concatMapAccumMS :: Monad m => (a -> accum -> m (accum, [b])) -> accum -> StreamConduit a m b
concatMapAccumMS :: (a -> accum -> m (accum, [b])) -> accum -> StreamConduit a m b
concatMapAccumMS a -> accum -> m (accum, [b])
f  accum
initial (Stream s -> m (Step s a ())
step m s
ms0) =
    ((accum, [b], s) -> m (Step (accum, [b], s) b ()))
-> m (accum, [b], s) -> Stream m b ()
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream (accum, [b], s) -> m (Step (accum, [b], s) b ())
step' ((s -> (accum, [b], s)) -> m s -> m (accum, [b], s)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (accum
initial, [], ) m s
ms0)
  where
    step' :: (accum, [b], s) -> m (Step (accum, [b], s) b ())
step' (accum
accum, [], s
s) = do
        Step s a ()
res <- s -> m (Step s a ())
step s
s
        case Step s a ()
res of
            Stop () -> Step (accum, [b], s) b () -> m (Step (accum, [b], s) b ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (accum, [b], s) b () -> m (Step (accum, [b], s) b ()))
-> Step (accum, [b], s) b () -> m (Step (accum, [b], s) b ())
forall a b. (a -> b) -> a -> b
$ () -> Step (accum, [b], s) b ()
forall s o r. r -> Step s o r
Stop ()
            Skip s
s' -> Step (accum, [b], s) b () -> m (Step (accum, [b], s) b ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (accum, [b], s) b () -> m (Step (accum, [b], s) b ()))
-> Step (accum, [b], s) b () -> m (Step (accum, [b], s) b ())
forall a b. (a -> b) -> a -> b
$ (accum, [b], s) -> Step (accum, [b], s) b ()
forall s o r. s -> Step s o r
Skip (accum
accum, [], s
s')
            Emit s
s' a
x -> do
                (accum
accum', [b]
xs) <- a -> accum -> m (accum, [b])
f a
x accum
accum
                Step (accum, [b], s) b () -> m (Step (accum, [b], s) b ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (accum, [b], s) b () -> m (Step (accum, [b], s) b ()))
-> Step (accum, [b], s) b () -> m (Step (accum, [b], s) b ())
forall a b. (a -> b) -> a -> b
$ (accum, [b], s) -> Step (accum, [b], s) b ()
forall s o r. s -> Step s o r
Skip (accum
accum', [b]
xs, s
s')
    step' (accum
accum, (b
x:[b]
xs), s
s) = Step (accum, [b], s) b () -> m (Step (accum, [b], s) b ())
forall (m :: * -> *) a. Monad m => a -> m a
return ((accum, [b], s) -> b -> Step (accum, [b], s) b ()
forall s o r. s -> o -> Step s o r
Emit (accum
accum, [b]
xs, s
s) b
x)
{-# INLINE concatMapAccumMS #-}

mapFoldableS :: (Monad m, F.Foldable f) => (a -> f b) -> StreamConduit a m b
mapFoldableS :: (a -> f b) -> StreamConduit a m b
mapFoldableS a -> f b
f (Stream s -> m (Step s a ())
step m s
ms0) =
    (([b], s) -> m (Step ([b], s) b ())) -> m ([b], s) -> Stream m b ()
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream ([b], s) -> m (Step ([b], s) b ())
step' ((s -> ([b], s)) -> m s -> m ([b], s)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM ([], ) m s
ms0)
  where
    step' :: ([b], s) -> m (Step ([b], s) b ())
step' ([], s
s) = do
        Step s a ()
res <- s -> m (Step s a ())
step s
s
        Step ([b], s) b () -> m (Step ([b], s) b ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step ([b], s) b () -> m (Step ([b], s) b ()))
-> Step ([b], s) b () -> m (Step ([b], s) b ())
forall a b. (a -> b) -> a -> b
$ case Step s a ()
res of
            Stop () -> () -> Step ([b], s) b ()
forall s o r. r -> Step s o r
Stop ()
            Skip s
s' -> ([b], s) -> Step ([b], s) b ()
forall s o r. s -> Step s o r
Skip ([], s
s')
            Emit s
s' a
x -> ([b], s) -> Step ([b], s) b ()
forall s o r. s -> Step s o r
Skip (f b -> [b]
forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList (a -> f b
f a
x), s
s')
    step' ((b
x:[b]
xs), s
s) = Step ([b], s) b () -> m (Step ([b], s) b ())
forall (m :: * -> *) a. Monad m => a -> m a
return (([b], s) -> b -> Step ([b], s) b ()
forall s o r. s -> o -> Step s o r
Emit ([b]
xs, s
s) b
x)
{-# INLINE mapFoldableS #-}

mapFoldableMS :: (Monad m, F.Foldable f) => (a -> m (f b)) -> StreamConduit a m b
mapFoldableMS :: (a -> m (f b)) -> StreamConduit a m b
mapFoldableMS a -> m (f b)
f (Stream s -> m (Step s a ())
step m s
ms0) =
    (([b], s) -> m (Step ([b], s) b ())) -> m ([b], s) -> Stream m b ()
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream ([b], s) -> m (Step ([b], s) b ())
step' ((s -> ([b], s)) -> m s -> m ([b], s)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM ([], ) m s
ms0)
  where
    step' :: ([b], s) -> m (Step ([b], s) b ())
step' ([], s
s) = do
        Step s a ()
res <- s -> m (Step s a ())
step s
s
        case Step s a ()
res of
            Stop () -> Step ([b], s) b () -> m (Step ([b], s) b ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step ([b], s) b () -> m (Step ([b], s) b ()))
-> Step ([b], s) b () -> m (Step ([b], s) b ())
forall a b. (a -> b) -> a -> b
$ () -> Step ([b], s) b ()
forall s o r. r -> Step s o r
Stop ()
            Skip s
s' -> Step ([b], s) b () -> m (Step ([b], s) b ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step ([b], s) b () -> m (Step ([b], s) b ()))
-> Step ([b], s) b () -> m (Step ([b], s) b ())
forall a b. (a -> b) -> a -> b
$ ([b], s) -> Step ([b], s) b ()
forall s o r. s -> Step s o r
Skip ([], s
s')
            Emit s
s' a
x -> do
                f b
y <- a -> m (f b)
f a
x
                Step ([b], s) b () -> m (Step ([b], s) b ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step ([b], s) b () -> m (Step ([b], s) b ()))
-> Step ([b], s) b () -> m (Step ([b], s) b ())
forall a b. (a -> b) -> a -> b
$ ([b], s) -> Step ([b], s) b ()
forall s o r. s -> Step s o r
Skip (f b -> [b]
forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList f b
y, s
s')
    step' ((b
x:[b]
xs), s
s) = Step ([b], s) b () -> m (Step ([b], s) b ())
forall (m :: * -> *) a. Monad m => a -> m a
return (([b], s) -> b -> Step ([b], s) b ()
forall s o r. s -> o -> Step s o r
Emit ([b]
xs, s
s) b
x)
{-# INLINE mapFoldableMS #-}

consumeS :: Monad m => StreamConsumer a m [a]
consumeS :: StreamConsumer a m [a]
consumeS (Stream s -> m (Step s a ())
step m s
ms0) =
    (([a] -> [a], s) -> m (Step ([a] -> [a], s) o [a]))
-> m ([a] -> [a], s) -> Stream m o [a]
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream ([a] -> [a], s) -> m (Step ([a] -> [a], s) o [a])
forall c o. ([a] -> c, s) -> m (Step ([a] -> c, s) o c)
step' ((s -> ([a] -> [a], s)) -> m s -> m ([a] -> [a], s)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM ([a] -> [a]
forall a. a -> a
id,) m s
ms0)
  where
    step' :: ([a] -> c, s) -> m (Step ([a] -> c, s) o c)
step' ([a] -> c
front, s
s) = do
        Step s a ()
res <- s -> m (Step s a ())
step s
s
        Step ([a] -> c, s) o c -> m (Step ([a] -> c, s) o c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step ([a] -> c, s) o c -> m (Step ([a] -> c, s) o c))
-> Step ([a] -> c, s) o c -> m (Step ([a] -> c, s) o c)
forall a b. (a -> b) -> a -> b
$ case Step s a ()
res of
            Stop () -> c -> Step ([a] -> c, s) o c
forall s o r. r -> Step s o r
Stop ([a] -> c
front [])
            Skip s
s' -> ([a] -> c, s) -> Step ([a] -> c, s) o c
forall s o r. s -> Step s o r
Skip ([a] -> c
front, s
s')
            Emit s
s' a
a -> ([a] -> c, s) -> Step ([a] -> c, s) o c
forall s o r. s -> Step s o r
Skip ([a] -> c
front ([a] -> c) -> ([a] -> [a]) -> [a] -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a
aa -> [a] -> [a]
forall a. a -> [a] -> [a]
:), s
s')
{-# INLINE consumeS #-}

groupByS :: Monad m => (a -> a -> Bool) -> StreamConduit a m [a]
groupByS :: (a -> a -> Bool) -> StreamConduit a m [a]
groupByS a -> a -> Bool
f = ((a, [a]) -> [a]) -> StreamConduit (a, [a]) m [a]
forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> StreamConduit a m b
mapS ((a -> [a] -> [a]) -> (a, [a]) -> [a]
forall a b c. (a -> b -> c) -> (a, b) -> c
Prelude.uncurry (:)) StreamConduit (a, [a]) m [a]
-> (Stream m a () -> Stream m (a, [a]) ()) -> StreamConduit a m [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a)
-> (a -> a -> Bool) -> Stream m a () -> Stream m (a, [a]) ()
forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> (b -> b -> Bool) -> StreamConduit a m (a, [a])
groupBy1S a -> a
forall a. a -> a
id a -> a -> Bool
f
{-# INLINE groupByS #-}

groupOn1S :: (Monad m, Eq b) => (a -> b) -> StreamConduit a m (a, [a])
groupOn1S :: (a -> b) -> StreamConduit a m (a, [a])
groupOn1S a -> b
f = (a -> b) -> (b -> b -> Bool) -> StreamConduit a m (a, [a])
forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> (b -> b -> Bool) -> StreamConduit a m (a, [a])
groupBy1S a -> b
f b -> b -> Bool
forall a. Eq a => a -> a -> Bool
(==)
{-# INLINE groupOn1S #-}

data GroupByState a b s
     = GBStart s
     | GBLoop ([a] -> [a]) a b s
     | GBDone

groupBy1S :: Monad m => (a -> b) -> (b -> b -> Bool) -> StreamConduit a m (a, [a])
groupBy1S :: (a -> b) -> (b -> b -> Bool) -> StreamConduit a m (a, [a])
groupBy1S a -> b
f b -> b -> Bool
eq (Stream s -> m (Step s a ())
step m s
ms0) =
    (GroupByState a b s -> m (Step (GroupByState a b s) (a, [a]) ()))
-> m (GroupByState a b s) -> Stream m (a, [a]) ()
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream GroupByState a b s -> m (Step (GroupByState a b s) (a, [a]) ())
step' ((s -> GroupByState a b s) -> m s -> m (GroupByState a b s)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM s -> GroupByState a b s
forall a b s. s -> GroupByState a b s
GBStart m s
ms0)
  where
    step' :: GroupByState a b s -> m (Step (GroupByState a b s) (a, [a]) ())
step' (GBStart s
s) = do
        Step s a ()
res <- s -> m (Step s a ())
step s
s
        Step (GroupByState a b s) (a, [a]) ()
-> m (Step (GroupByState a b s) (a, [a]) ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (GroupByState a b s) (a, [a]) ()
 -> m (Step (GroupByState a b s) (a, [a]) ()))
-> Step (GroupByState a b s) (a, [a]) ()
-> m (Step (GroupByState a b s) (a, [a]) ())
forall a b. (a -> b) -> a -> b
$ case Step s a ()
res of
            Stop () -> () -> Step (GroupByState a b s) (a, [a]) ()
forall s o r. r -> Step s o r
Stop ()
            Skip s
s' -> GroupByState a b s -> Step (GroupByState a b s) (a, [a]) ()
forall s o r. s -> Step s o r
Skip (s -> GroupByState a b s
forall a b s. s -> GroupByState a b s
GBStart s
s')
            Emit s
s' a
x0 -> GroupByState a b s -> Step (GroupByState a b s) (a, [a]) ()
forall s o r. s -> Step s o r
Skip (([a] -> [a]) -> a -> b -> s -> GroupByState a b s
forall a b s. ([a] -> [a]) -> a -> b -> s -> GroupByState a b s
GBLoop [a] -> [a]
forall a. a -> a
id a
x0 (a -> b
f a
x0) s
s')
    step' (GBLoop [a] -> [a]
rest a
x0 b
fx0 s
s) = do
        Step s a ()
res <- s -> m (Step s a ())
step s
s
        Step (GroupByState a b s) (a, [a]) ()
-> m (Step (GroupByState a b s) (a, [a]) ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (GroupByState a b s) (a, [a]) ()
 -> m (Step (GroupByState a b s) (a, [a]) ()))
-> Step (GroupByState a b s) (a, [a]) ()
-> m (Step (GroupByState a b s) (a, [a]) ())
forall a b. (a -> b) -> a -> b
$ case Step s a ()
res of
            Stop () -> GroupByState a b s
-> (a, [a]) -> Step (GroupByState a b s) (a, [a]) ()
forall s o r. s -> o -> Step s o r
Emit GroupByState a b s
forall a b s. GroupByState a b s
GBDone (a
x0, [a] -> [a]
rest [])
            Skip s
s' -> GroupByState a b s -> Step (GroupByState a b s) (a, [a]) ()
forall s o r. s -> Step s o r
Skip (([a] -> [a]) -> a -> b -> s -> GroupByState a b s
forall a b s. ([a] -> [a]) -> a -> b -> s -> GroupByState a b s
GBLoop [a] -> [a]
rest a
x0 b
fx0 s
s')
            Emit s
s' a
x
                | b
fx0 b -> b -> Bool
`eq` a -> b
f a
x -> GroupByState a b s -> Step (GroupByState a b s) (a, [a]) ()
forall s o r. s -> Step s o r
Skip (([a] -> [a]) -> a -> b -> s -> GroupByState a b s
forall a b s. ([a] -> [a]) -> a -> b -> s -> GroupByState a b s
GBLoop ([a] -> [a]
rest ([a] -> [a]) -> ([a] -> [a]) -> [a] -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:)) a
x0 b
fx0 s
s')
                | Bool
otherwise -> GroupByState a b s
-> (a, [a]) -> Step (GroupByState a b s) (a, [a]) ()
forall s o r. s -> o -> Step s o r
Emit (([a] -> [a]) -> a -> b -> s -> GroupByState a b s
forall a b s. ([a] -> [a]) -> a -> b -> s -> GroupByState a b s
GBLoop [a] -> [a]
forall a. a -> a
id a
x (a -> b
f a
x) s
s') (a
x0, [a] -> [a]
rest [])
    step' GroupByState a b s
GBDone = Step (GroupByState a b s) (a, [a]) ()
-> m (Step (GroupByState a b s) (a, [a]) ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (GroupByState a b s) (a, [a]) ()
 -> m (Step (GroupByState a b s) (a, [a]) ()))
-> Step (GroupByState a b s) (a, [a]) ()
-> m (Step (GroupByState a b s) (a, [a]) ())
forall a b. (a -> b) -> a -> b
$ () -> Step (GroupByState a b s) (a, [a]) ()
forall s o r. r -> Step s o r
Stop ()
{-# INLINE groupBy1S #-}

isolateS :: Monad m => Int -> StreamConduit a m a
isolateS :: Int -> StreamConduit a m a
isolateS Int
count (Stream s -> m (Step s a ())
step m s
ms0) =
    ((Int, s) -> m (Step (Int, s) a ())) -> m (Int, s) -> Stream m a ()
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream (Int, s) -> m (Step (Int, s) a ())
forall a. (Ord a, Num a) => (a, s) -> m (Step (a, s) a ())
step' ((s -> (Int, s)) -> m s -> m (Int, s)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (Int
count,) m s
ms0)
  where
    step' :: (a, s) -> m (Step (a, s) a ())
step' (a
n, s
_) | a
n a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
0 = Step (a, s) a () -> m (Step (a, s) a ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (a, s) a () -> m (Step (a, s) a ()))
-> Step (a, s) a () -> m (Step (a, s) a ())
forall a b. (a -> b) -> a -> b
$ () -> Step (a, s) a ()
forall s o r. r -> Step s o r
Stop ()
    step' (a
n, s
s) = do
        Step s a ()
res <- s -> m (Step s a ())
step s
s
        Step (a, s) a () -> m (Step (a, s) a ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (a, s) a () -> m (Step (a, s) a ()))
-> Step (a, s) a () -> m (Step (a, s) a ())
forall a b. (a -> b) -> a -> b
$ case Step s a ()
res of
            Stop () -> () -> Step (a, s) a ()
forall s o r. r -> Step s o r
Stop ()
            Skip s
s' -> (a, s) -> Step (a, s) a ()
forall s o r. s -> Step s o r
Skip (a
n, s
s')
            Emit s
s' a
x -> (a, s) -> a -> Step (a, s) a ()
forall s o r. s -> o -> Step s o r
Emit (a
n a -> a -> a
forall a. Num a => a -> a -> a
- a
1, s
s') a
x
{-# INLINE isolateS #-}

filterS :: Monad m => (a -> Bool) -> StreamConduit a m a
filterS :: (a -> Bool) -> StreamConduit a m a
filterS a -> Bool
f (Stream s -> m (Step s a ())
step m s
ms0) =
    (s -> m (Step s a ())) -> m s -> Stream m a ()
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream s -> m (Step s a ())
step' m s
ms0
  where
    step' :: s -> m (Step s a ())
step' s
s = do
        Step s a ()
res <- s -> m (Step s a ())
step s
s
        Step s a () -> m (Step s a ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s a () -> m (Step s a ())) -> Step s a () -> m (Step s a ())
forall a b. (a -> b) -> a -> b
$ case Step s a ()
res of
            Stop () -> () -> Step s a ()
forall s o r. r -> Step s o r
Stop ()
            Skip s
s' -> s -> Step s a ()
forall s o r. s -> Step s o r
Skip s
s'
            Emit s
s' a
x
                | a -> Bool
f a
x -> s -> a -> Step s a ()
forall s o r. s -> o -> Step s o r
Emit s
s' a
x
                | Bool
otherwise -> s -> Step s a ()
forall s o r. s -> Step s o r
Skip s
s'

sinkNullS :: Monad m => StreamConsumer a m ()
sinkNullS :: StreamConsumer a m ()
sinkNullS (Stream s -> m (Step s a ())
step m s
ms0) =
    (s -> m (Step s o ())) -> m s -> Stream m o ()
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream s -> m (Step s o ())
forall o. s -> m (Step s o ())
step' m s
ms0
  where
    step' :: s -> m (Step s o ())
step' s
s = do
        Step s a ()
res <- s -> m (Step s a ())
step s
s
        Step s o () -> m (Step s o ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s o () -> m (Step s o ())) -> Step s o () -> m (Step s o ())
forall a b. (a -> b) -> a -> b
$ case Step s a ()
res of
            Stop () -> () -> Step s o ()
forall s o r. r -> Step s o r
Stop ()
            Skip s
s' -> s -> Step s o ()
forall s o r. s -> Step s o r
Skip s
s'
            Emit s
s' a
_ -> s -> Step s o ()
forall s o r. s -> Step s o r
Skip s
s'
{-# INLINE sinkNullS #-}

sourceNullS :: Monad m => StreamProducer m a
sourceNullS :: StreamProducer m a
sourceNullS Stream m i ()
_ = (() -> m (Step () a ())) -> m () -> Stream m a ()
forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream (\()
_ -> Step () a () -> m (Step () a ())
forall (m :: * -> *) a. Monad m => a -> m a
return (() -> Step () a ()
forall s o r. r -> Step s o r
Stop ())) (() -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
{-# INLINE sourceNullS #-}