{-# 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 :: forall (m :: * -> *) b a.
Monad m =>
(b -> Maybe (a, b)) -> b -> StreamProducer m a
unfoldS b -> Maybe (a, b)
f b
s0 Stream m i ()
_ =
    forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream forall {m :: * -> *}. Monad m => b -> m (Step b a ())
step (forall (m :: * -> *) a. Monad m => a -> m a
return b
s0)
  where
    step :: b -> m (Step b a ())
step b
s = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
        case b -> Maybe (a, b)
f b
s of
            Maybe (a, b)
Nothing -> forall s o r. r -> Step s o r
Stop ()
            Just (a
x, b
s') -> 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 :: forall (m :: * -> *) b r a i.
Monad m =>
(b -> Either r (a, b)) -> b -> StreamConduitT i a m r
unfoldEitherS b -> Either r (a, b)
f b
s0 Stream m i ()
_ =
    forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream forall {m :: * -> *}. Monad m => b -> m (Step b a r)
step (forall (m :: * -> *) a. Monad m => a -> m a
return b
s0)
  where
    step :: b -> m (Step b a r)
step b
s = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
        case b -> Either r (a, b)
f b
s of
            Left r
r        -> forall s o r. r -> Step s o r
Stop r
r
            Right (a
x, b
s') -> 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 :: forall (m :: * -> *) b a.
Monad m =>
(b -> m (Maybe (a, b))) -> b -> StreamProducer m a
unfoldMS b -> m (Maybe (a, b))
f b
s0 Stream m i ()
_ =
    forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream b -> m (Step b a ())
step (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
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Maybe (a, b)
ms' of
            Maybe (a, b)
Nothing -> forall s o r. r -> Step s o r
Stop ()
            Just (a
x, b
s') -> 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 :: forall (m :: * -> *) b r a i.
Monad m =>
(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 ()
_ =
    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 (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
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Either r (a, b)
ms' of
            Left r
r        -> forall s o r. r -> Step s o r
Stop r
r
            Right (a
x, b
s') -> 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 :: forall (m :: * -> *) a. Monad m => [a] -> StreamProducer m a
sourceListS [a]
xs0 Stream m i ()
_ =
    forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {o}. [o] -> Step [o] o ()
step) (forall (m :: * -> *) a. Monad m => a -> m a
return [a]
xs0)
  where
    step :: [o] -> Step [o] o ()
step [] = forall s o r. r -> Step s o r
Stop ()
    step (o
x:[o]
xs) = 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 :: forall a (m :: * -> *).
(Enum a, Ord a, Monad m) =>
a -> a -> StreamProducer m a
enumFromToS a
x0 a
y Stream m i ()
_ =
    forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream forall {m :: * -> *}. Monad m => a -> m (Step a a ())
step (forall (m :: * -> *) a. Monad m => a -> m a
return a
x0)
  where
    step :: a -> m (Step a a ())
step a
x = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ if a
x forall a. Ord a => a -> a -> Bool
Prelude.> a
y
        then forall s o r. r -> Step s o r
Stop ()
        else forall s o r. s -> o -> Step s o r
Emit (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 :: forall a (m :: * -> *).
(Integral a, Monad m) =>
a -> a -> StreamProducer m a
enumFromToS_int a
x0 a
y Stream m i ()
_ = a
x0 seq :: forall a b. a -> b -> b
`seq` a
y seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream forall {m :: * -> *}. Monad m => a -> m (Step a a ())
step (forall (m :: * -> *) a. Monad m => a -> m a
return a
x0)
  where
    step :: a -> m (Step a a ())
step a
x | a
x forall a. Ord a => a -> a -> Bool
<= a
y    = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s o r. s -> o -> Step s o r
Emit (a
x forall a. Num a => a -> a -> a
Prelude.+ a
1) a
x
           | Bool
otherwise = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ 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 :: forall (m :: * -> *) a.
Monad m =>
(a -> a) -> a -> StreamProducer m a
iterateS a -> a
f a
x0 Stream m i ()
_ =
    forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {r}. a -> Step a a r
step) (forall (m :: * -> *) a. Monad m => a -> m a
return a
x0)
  where
    step :: a -> Step a a r
step a
x = 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 :: forall (m :: * -> *) a. Monad m => Int -> a -> StreamProducer m a
replicateS Int
cnt0 a
a Stream m i ()
_ =
    forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream forall {s} {m :: * -> *}.
(Ord s, Num s, Monad m) =>
s -> m (Step s a ())
step (forall (m :: * -> *) a. Monad m => a -> m a
return Int
cnt0)
  where
    step :: s -> m (Step s a ())
step s
cnt
        | s
cnt forall a. Ord a => a -> a -> Bool
<= s
0  = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s o r. r -> Step s o r
Stop ()
        | Bool
otherwise = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s o r. s -> o -> Step s o r
Emit (s
cnt forall a. Num a => a -> a -> a
- s
1) a
a
{-# INLINE replicateS #-}

replicateMS :: Monad m => Int -> m a -> StreamProducer m a
replicateMS :: forall (m :: * -> *) a. Monad m => Int -> m a -> StreamProducer m a
replicateMS Int
cnt0 m a
ma Stream m i ()
_ =
    forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream forall {s}. (Ord s, Num s) => s -> m (Step s a ())
step (forall (m :: * -> *) a. Monad m => a -> m a
return Int
cnt0)
  where
    step :: s -> m (Step s a ())
step s
cnt
        | s
cnt forall a. Ord a => a -> a -> Bool
<= s
0  = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s o r. r -> Step s o r
Stop ()
        | Bool
otherwise = forall s o r. s -> o -> Step s o r
Emit (s
cnt forall a. Num a => a -> a -> a
- s
1) 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 :: forall (m :: * -> *) b a.
Monad m =>
(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) =
    forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream forall {o}. (b, s) -> m (Step (b, s) o b)
step' (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
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Step s a ()
res of
            Stop () -> forall s o r. r -> Step s o r
Stop b
b
            Skip s
s' -> forall s o r. s -> Step s o r
Skip (b
b, s
s')
            Emit s
s' a
a -> 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 :: forall (m :: * -> *) b a.
Monad m =>
(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) =
    forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream forall {o}. (b, s) -> m (Step (b, s) o b)
step' (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 () -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s o r. r -> Step s o r
Stop b
b
            Skip s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> 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
                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> 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 :: forall (m :: * -> *) a.
Monad m =>
(a -> m ()) -> StreamConsumer a m ()
mapM_S a -> m ()
f (Stream s -> m (Step s a ())
step m s
ms0) =
    forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream 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 () -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s o r. r -> Step s o r
Stop ()
          Skip s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s o r. s -> Step s o r
Skip s
s'
          Emit s
s' a
x -> a -> m ()
f a
x forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a. Monad m => a -> m a
return (forall s o r. s -> Step s o r
Skip s
s')
{-# INLINE [1] mapM_S #-}

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

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

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

mapS :: Monad m => (a -> b) -> StreamConduit a m b
mapS :: forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> StreamConduit a m b
mapS a -> b
f (Stream s -> m (Step s a ())
step m s
ms0) =
    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
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Step s a ()
res of
            Stop ()
r -> forall s o r. r -> Step s o r
Stop ()
r
            Emit s
s' a
a -> forall s o r. s -> o -> Step s o r
Emit s
s' (a -> b
f a
a)
            Skip s
s' -> 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 :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> StreamConduit a m b
mapMS a -> m b
f (Stream s -> m (Step s a ())
step m s
ms0) =
    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 -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s o r. r -> Step s o r
Stop ()
r
            Emit s
s' a
a -> forall s o r. s -> o -> Step s o r
Emit s
s' forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` a -> m b
f a
a
            Skip s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> 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 :: forall (m :: * -> *) a.
Monad m =>
(a -> m ()) -> StreamConduit a m a
iterMS a -> m ()
f (Stream s -> m (Step s a ())
step m s
ms0) =
    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 () -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s o r. r -> Step s o r
Stop ()
            Skip s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s o r. s -> Step s o r
Skip s
s'
            Emit s
s' a
x -> a -> m ()
f a
x forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a. Monad m => a -> m a
return (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 :: forall (m :: * -> *) a b.
Monad m =>
(a -> Maybe b) -> StreamConduit a m b
mapMaybeS a -> Maybe b
f (Stream s -> m (Step s a ())
step m s
ms0) =
    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
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Step s a ()
res of
            Stop () -> forall s o r. r -> Step s o r
Stop ()
            Skip s
s' -> 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 -> forall s o r. s -> o -> Step s o r
Emit s
s' b
y
                    Maybe b
Nothing -> 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 :: forall (m :: * -> *) a b.
Monad m =>
(a -> m (Maybe b)) -> StreamConduit a m b
mapMaybeMS a -> m (Maybe b)
f (Stream s -> m (Step s a ())
step m s
ms0) =
    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 () -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s o r. r -> Step s o r
Stop ()
            Skip s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> 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 -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s o r. s -> o -> Step s o r
Emit s
s' b
y
                    Maybe b
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s o r. s -> Step s o r
Skip s
s'
{-# INLINE mapMaybeMS #-}

catMaybesS :: Monad m => StreamConduit (Maybe a) m a
catMaybesS :: forall (m :: * -> *) a. Monad m => StreamConduit (Maybe a) m a
catMaybesS (Stream s -> m (Step s (Maybe a) ())
step m s
ms0) =
    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
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Step s (Maybe a) ()
res of
            Stop () -> forall s o r. r -> Step s o r
Stop ()
            Skip s
s' -> forall s o r. s -> Step s o r
Skip s
s'
            Emit s
s' Maybe a
Nothing -> forall s o r. s -> Step s o r
Skip s
s'
            Emit s
s' (Just a
x) -> 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 :: forall (m :: * -> *) (f :: * -> *) a.
(Monad m, Foldable f) =>
StreamConduit (f a) m a
concatS (Stream s -> m (Step s (f a) ())
step m s
ms0) =
    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' (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
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Step s (f a) ()
res of
            Stop () -> forall s o r. r -> Step s o r
Stop ()
            Skip s
s' -> forall s o r. s -> Step s o r
Skip ([], s
s')
            Emit s
s' f a
x -> forall s o r. s -> Step s o r
Skip (forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList f a
x, s
s')
    step' ((a
x:[a]
xs), s
s) = forall (m :: * -> *) a. Monad m => a -> m a
return (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 :: forall (m :: * -> *) a b.
Monad m =>
(a -> [b]) -> StreamConduit a m b
concatMapS a -> [b]
f (Stream s -> m (Step s a ())
step m s
ms0) =
    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' (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
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Step s a ()
res of
            Stop () -> forall s o r. r -> Step s o r
Stop ()
            Skip s
s' -> forall s o r. s -> Step s o r
Skip ([], s
s')
            Emit s
s' a
x -> forall s o r. s -> Step s o r
Skip (a -> [b]
f a
x, s
s')
    step' ((b
x:[b]
xs), s
s) = forall (m :: * -> *) a. Monad m => a -> m a
return (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 :: forall (m :: * -> *) a b.
Monad m =>
(a -> m [b]) -> StreamConduit a m b
concatMapMS a -> m [b]
f (Stream s -> m (Step s a ())
step m s
ms0) =
    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' (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 () -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s o r. r -> Step s o r
Stop ()
            Skip s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> 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
                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s o r. s -> Step s o r
Skip ([b]
xs, s
s')
    step' ((b
x:[b]
xs), s
s) = forall (m :: * -> *) a. Monad m => a -> m a
return (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 :: forall (m :: * -> *) a accum b.
Monad m =>
(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) =
    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' (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
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Step s a ()
res of
            Stop () -> forall s o r. r -> Step s o r
Stop ()
            Skip s
s' -> 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 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) = forall (m :: * -> *) a. Monad m => a -> m a
return (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 :: forall (m :: * -> *) a s b.
Monad m =>
(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) =
    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' (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
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Step s a ()
res of
            Stop () -> forall s o r. r -> Step s o r
Stop s
accum
            Skip s
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 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 :: forall (m :: * -> *) a s b.
Monad m =>
(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) =
    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' (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 () -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s o r. r -> Step s o r
Stop s
accum
            Skip s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ 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
                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ 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 :: forall (m :: * -> *) a accum b.
Monad m =>
(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) =
    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' (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 () -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s o r. r -> Step s o r
Stop ()
            Skip s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> 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
                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> 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) = forall (m :: * -> *) a. Monad m => a -> m a
return (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 :: forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, Foldable f) =>
(a -> f b) -> StreamConduit a m b
mapFoldableS a -> f b
f (Stream s -> m (Step s a ())
step m s
ms0) =
    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' (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
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Step s a ()
res of
            Stop () -> forall s o r. r -> Step s o r
Stop ()
            Skip s
s' -> forall s o r. s -> Step s o r
Skip ([], s
s')
            Emit s
s' a
x -> forall s o r. s -> Step s o r
Skip (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) = forall (m :: * -> *) a. Monad m => a -> m a
return (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 :: forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, Foldable f) =>
(a -> m (f b)) -> StreamConduit a m b
mapFoldableMS a -> m (f b)
f (Stream s -> m (Step s a ())
step m s
ms0) =
    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' (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 () -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s o r. r -> Step s o r
Stop ()
            Skip s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> 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
                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s o r. s -> Step s o r
Skip (forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList f b
y, s
s')
    step' ((b
x:[b]
xs), s
s) = forall (m :: * -> *) a. Monad m => a -> m a
return (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 :: forall (m :: * -> *) a. Monad m => StreamConsumer a m [a]
consumeS (Stream s -> m (Step s a ())
step m s
ms0) =
    forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream forall {r} {o}. ([a] -> r, s) -> m (Step ([a] -> r, s) o r)
step' (forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (forall a. a -> a
id,) m s
ms0)
  where
    step' :: ([a] -> r, s) -> m (Step ([a] -> r, s) o r)
step' ([a] -> r
front, s
s) = do
        Step s a ()
res <- s -> m (Step s a ())
step s
s
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Step s a ()
res of
            Stop () -> forall s o r. r -> Step s o r
Stop ([a] -> r
front [])
            Skip s
s' -> forall s o r. s -> Step s o r
Skip ([a] -> r
front, s
s')
            Emit s
s' a
a -> forall s o r. s -> Step s o r
Skip ([a] -> r
front forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a
aforall a. a -> [a] -> [a]
:), s
s')
{-# INLINE consumeS #-}

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

groupOn1S :: (Monad m, Eq b) => (a -> b) -> StreamConduit a m (a, [a])
groupOn1S :: forall (m :: * -> *) b a.
(Monad m, Eq b) =>
(a -> b) -> StreamConduit a m (a, [a])
groupOn1S a -> b
f = forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> (b -> b -> Bool) -> StreamConduit a m (a, [a])
groupBy1S a -> b
f 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 :: forall (m :: * -> *) a b.
Monad m =>
(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) =
    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' (forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM 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
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Step s a ()
res of
            Stop () -> forall s o r. r -> Step s o r
Stop ()
            Skip s
s' -> forall s o r. s -> Step s o r
Skip (forall a b s. s -> GroupByState a b s
GBStart s
s')
            Emit s
s' a
x0 -> forall s o r. s -> Step s o r
Skip (forall a b s. ([a] -> [a]) -> a -> b -> s -> GroupByState a b s
GBLoop 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
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Step s a ()
res of
            Stop () -> forall s o r. s -> o -> Step s o r
Emit forall a b s. GroupByState a b s
GBDone (a
x0, [a] -> [a]
rest [])
            Skip s
s' -> forall s o r. s -> Step s o r
Skip (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 -> forall s o r. s -> Step s o r
Skip (forall a b s. ([a] -> [a]) -> a -> b -> s -> GroupByState a b s
GBLoop ([a] -> [a]
rest forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a
xforall a. a -> [a] -> [a]
:)) a
x0 b
fx0 s
s')
                | Bool
otherwise -> forall s o r. s -> o -> Step s o r
Emit (forall a b s. ([a] -> [a]) -> a -> b -> s -> GroupByState a b s
GBLoop 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 = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s o r. r -> Step s o r
Stop ()
{-# INLINE groupBy1S #-}

isolateS :: Monad m => Int -> StreamConduit a m a
isolateS :: forall (m :: * -> *) a. Monad m => Int -> StreamConduit a m a
isolateS Int
count (Stream s -> m (Step s a ())
step m s
ms0) =
    forall (m :: * -> *) o r s.
(s -> m (Step s o r)) -> m s -> Stream m o r
Stream forall {a}. (Ord a, Num a) => (a, s) -> m (Step (a, s) a ())
step' (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 forall a. Ord a => a -> a -> Bool
<= a
0 = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ 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
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Step s a ()
res of
            Stop () -> forall s o r. r -> Step s o r
Stop ()
            Skip s
s' -> forall s o r. s -> Step s o r
Skip (a
n, s
s')
            Emit s
s' a
x -> forall s o r. s -> o -> Step s o r
Emit (a
n 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 :: forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> StreamConduit a m a
filterS a -> Bool
f (Stream s -> m (Step s a ())
step m s
ms0) =
    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
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Step s a ()
res of
            Stop () -> forall s o r. r -> Step s o r
Stop ()
            Skip s
s' -> forall s o r. s -> Step s o r
Skip s
s'
            Emit s
s' a
x
                | a -> Bool
f a
x -> forall s o r. s -> o -> Step s o r
Emit s
s' a
x
                | Bool
otherwise -> forall s o r. s -> Step s o r
Skip s
s'

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

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