{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE Trustworthy #-}
-- | /NOTE/ It is recommended to start using "Data.Conduit.Combinators" instead
-- of this module.
--
-- Higher-level functions to interact with the elements of a stream. Most of
-- these are based on list functions.
--
-- For many purposes, it's recommended to use the conduit-combinators library,
-- which provides a more complete set of functions.
--
-- Note that these functions all deal with individual elements of a stream as a
-- sort of \"black box\", where there is no introspection of the contained
-- elements. Values such as @ByteString@ and @Text@ will likely need to be
-- treated specially to deal with their contents properly (@Word8@ and @Char@,
-- respectively). See the @Data.Conduit.Binary@ and @Data.Conduit.Text@
-- modules in the @conduit-extra@ package.
module Data.Conduit.List
    ( -- * Sources
      sourceList
    , sourceNull
    , unfold
    , unfoldEither
    , unfoldM
    , unfoldEitherM
    , enumFromTo
    , iterate
    , replicate
    , replicateM
      -- * Sinks
      -- ** Pure
    , fold
    , foldMap
    , uncons
    , unconsEither
    , take
    , drop
    , head
    , peek
    , consume
    , sinkNull
      -- ** Monadic
    , foldMapM
    , foldM
    , unconsM
    , unconsEitherM
    , mapM_
      -- * Conduits
      -- ** Pure
    , map
    , mapMaybe
    , mapFoldable
    , catMaybes
    , concat
    , concatMap
    , concatMapAccum
    , scanl
    , scan
    , mapAccum
    , chunksOf
    , groupBy
    , groupOn1
    , isolate
    , filter
      -- ** Monadic
    , mapM
    , iterM
    , scanlM
    , scanM
    , mapAccumM
    , mapMaybeM
    , mapFoldableM
    , concatMapM
    , concatMapAccumM
      -- * Misc
    , sequence
    ) where

import qualified Prelude
import Prelude
    ( ($), return, (==), (-), Int
    , (.), id, Maybe (..), Monad
    , Either (..)
    , Bool (..)
    , (>>)
    , (>>=)
    , seq
    , otherwise
    , Enum, Eq
    , maybe
    , (<=)
    , (>)
    , error
    , (++)
    , show
    )
import Data.Monoid (Monoid, mempty, mappend)
import qualified Data.Foldable as F
import Data.Conduit
import Data.Conduit.Internal.Conduit (unconsM, unconsEitherM)
import Data.Conduit.Internal.Fusion
import Data.Conduit.Internal.List.Stream
import qualified Data.Conduit.Internal as CI
import Data.Functor.Identity (Identity (runIdentity))
import Control.Monad (when, (<=<), liftM, void)
import Control.Monad.Trans.Class (lift)

-- Defines INLINE_RULE0, INLINE_RULE, STREAMING0, and STREAMING.
#include "fusion-macros.h"

-- | Generate a source from a seed value.
--
-- Subject to fusion
--
-- Since 0.4.2
unfold, unfoldC :: Monad m
                => (b -> Maybe (a, b))
                -> b
                -> ConduitT i a m ()
unfoldC :: (b -> Maybe (a, b)) -> b -> ConduitT i a m ()
unfoldC b -> Maybe (a, b)
f =
    b -> ConduitT i a m ()
forall (m :: * -> *) i. Monad m => b -> ConduitT i a m ()
go
  where
    go :: b -> ConduitT i a m ()
go b
seed =
        case b -> Maybe (a, b)
f b
seed of
            Just (a
a, b
seed') -> a -> ConduitT i a m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield a
a ConduitT i a m () -> ConduitT i a m () -> ConduitT i a m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> b -> ConduitT i a m ()
go b
seed'
            Maybe (a, b)
Nothing -> () -> ConduitT i a m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
{-# INLINE unfoldC #-}
STREAMING(unfold, unfoldC, unfoldS, f x)

-- | Generate a source from a seed value with a return value.
--
-- Subject to fusion
--
-- @since 1.2.11
unfoldEither, unfoldEitherC :: Monad m
                            => (b -> Either r (a, b))
                            -> b
                            -> ConduitT i a m r
unfoldEitherC :: (b -> Either r (a, b)) -> b -> ConduitT i a m r
unfoldEitherC b -> Either r (a, b)
f =
    b -> ConduitT i a m r
forall (m :: * -> *) i. Monad m => b -> ConduitT i a m r
go
  where
    go :: b -> ConduitT i a m r
go b
seed =
        case b -> Either r (a, b)
f b
seed of
            Right (a
a, b
seed') -> a -> ConduitT i a m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield a
a ConduitT i a m () -> ConduitT i a m r -> ConduitT i a m r
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> b -> ConduitT i a m r
go b
seed'
            Left r
r -> r -> ConduitT i a m r
forall (m :: * -> *) a. Monad m => a -> m a
return r
r
{-# INLINE unfoldEitherC #-}
STREAMING(unfoldEither, unfoldEitherC, unfoldEitherS, f x)

-- | A monadic unfold.
--
-- Subject to fusion
--
-- Since 1.1.2
unfoldM, unfoldMC :: Monad m
                  => (b -> m (Maybe (a, b)))
                  -> b
                  -> ConduitT i a m ()
unfoldMC :: (b -> m (Maybe (a, b))) -> b -> ConduitT i a m ()
unfoldMC b -> m (Maybe (a, b))
f =
    b -> ConduitT i a m ()
forall i. b -> ConduitT i a m ()
go
  where
    go :: b -> ConduitT i a m ()
go b
seed = do
        Maybe (a, b)
mres <- m (Maybe (a, b)) -> ConduitT i a m (Maybe (a, b))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Maybe (a, b)) -> ConduitT i a m (Maybe (a, b)))
-> m (Maybe (a, b)) -> ConduitT i a m (Maybe (a, b))
forall a b. (a -> b) -> a -> b
$ b -> m (Maybe (a, b))
f b
seed
        case Maybe (a, b)
mres of
            Just (a
a, b
seed') -> a -> ConduitT i a m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield a
a ConduitT i a m () -> ConduitT i a m () -> ConduitT i a m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> b -> ConduitT i a m ()
go b
seed'
            Maybe (a, b)
Nothing -> () -> ConduitT i a m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
STREAMING(unfoldM, unfoldMC, unfoldMS, f seed)

-- | A monadic unfoldEither.
--
-- Subject to fusion
--
-- @since 1.2.11
unfoldEitherM, unfoldEitherMC :: Monad m
                              => (b -> m (Either r (a, b)))
                              -> b
                              -> ConduitT i a m r
unfoldEitherMC :: (b -> m (Either r (a, b))) -> b -> ConduitT i a m r
unfoldEitherMC b -> m (Either r (a, b))
f =
    b -> ConduitT i a m r
forall i. b -> ConduitT i a m r
go
  where
    go :: b -> ConduitT i a m r
go b
seed = do
        Either r (a, b)
mres <- m (Either r (a, b)) -> ConduitT i a m (Either r (a, b))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either r (a, b)) -> ConduitT i a m (Either r (a, b)))
-> m (Either r (a, b)) -> ConduitT i a m (Either r (a, b))
forall a b. (a -> b) -> a -> b
$ b -> m (Either r (a, b))
f b
seed
        case Either r (a, b)
mres of
            Right (a
a, b
seed') -> a -> ConduitT i a m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield a
a ConduitT i a m () -> ConduitT i a m r -> ConduitT i a m r
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> b -> ConduitT i a m r
go b
seed'
            Left r
r -> r -> ConduitT i a m r
forall (m :: * -> *) a. Monad m => a -> m a
return r
r
STREAMING(unfoldEitherM, unfoldEitherMC, unfoldEitherMS, f seed)

-- | Split a pure conduit into head and tail.
-- This is equivalent to @runIdentity . unconsM@.
--
-- Note that you have to 'sealConduitT' it first.
--
-- Since 1.3.3
uncons :: SealedConduitT () o Identity ()
       -> Maybe (o, SealedConduitT () o Identity ())
uncons :: SealedConduitT () o Identity ()
-> Maybe (o, SealedConduitT () o Identity ())
uncons = Identity (Maybe (o, SealedConduitT () o Identity ()))
-> Maybe (o, SealedConduitT () o Identity ())
forall a. Identity a -> a
runIdentity (Identity (Maybe (o, SealedConduitT () o Identity ()))
 -> Maybe (o, SealedConduitT () o Identity ()))
-> (SealedConduitT () o Identity ()
    -> Identity (Maybe (o, SealedConduitT () o Identity ())))
-> SealedConduitT () o Identity ()
-> Maybe (o, SealedConduitT () o Identity ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SealedConduitT () o Identity ()
-> Identity (Maybe (o, SealedConduitT () o Identity ()))
forall (m :: * -> *) o.
Monad m =>
SealedConduitT () o m () -> m (Maybe (o, SealedConduitT () o m ()))
unconsM

-- | Split a pure conduit into head and tail or return its result if it is done.
-- This is equivalent to @runIdentity . unconsEitherM@.
--
-- Note that you have to 'sealConduitT' it first.
--
-- Since 1.3.3
unconsEither :: SealedConduitT () o Identity r
             -> Either r (o, SealedConduitT () o Identity r)
unconsEither :: SealedConduitT () o Identity r
-> Either r (o, SealedConduitT () o Identity r)
unconsEither = Identity (Either r (o, SealedConduitT () o Identity r))
-> Either r (o, SealedConduitT () o Identity r)
forall a. Identity a -> a
runIdentity (Identity (Either r (o, SealedConduitT () o Identity r))
 -> Either r (o, SealedConduitT () o Identity r))
-> (SealedConduitT () o Identity r
    -> Identity (Either r (o, SealedConduitT () o Identity r)))
-> SealedConduitT () o Identity r
-> Either r (o, SealedConduitT () o Identity r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SealedConduitT () o Identity r
-> Identity (Either r (o, SealedConduitT () o Identity r))
forall (m :: * -> *) o r.
Monad m =>
SealedConduitT () o m r
-> m (Either r (o, SealedConduitT () o m r))
unconsEitherM

-- | Yield the values from the list.
--
-- Subject to fusion
sourceList, sourceListC :: Monad m => [a] -> ConduitT i a m ()
sourceListC :: [a] -> ConduitT i a m ()
sourceListC = (a -> ConduitT i a m ()) -> [a] -> ConduitT i a m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
Prelude.mapM_ a -> ConduitT i a m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield
{-# INLINE sourceListC #-}
STREAMING(sourceList, sourceListC, sourceListS, xs)

-- | Enumerate from a value to a final value, inclusive, via 'succ'.
--
-- This is generally more efficient than using @Prelude@\'s @enumFromTo@ and
-- combining with @sourceList@ since this avoids any intermediate data
-- structures.
--
-- Subject to fusion
--
-- Since 0.4.2
enumFromTo, enumFromToC :: (Enum a, Prelude.Ord a, Monad m)
                        => a
                        -> a
                        -> ConduitT i a m ()
enumFromToC :: a -> a -> ConduitT i a m ()
enumFromToC a
x0 a
y =
    a -> ConduitT i a m ()
forall (m :: * -> *) i. Monad m => a -> ConduitT i a m ()
loop a
x0
  where
    loop :: a -> ConduitT i a m ()
loop a
x
        | a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
Prelude.> a
y = () -> ConduitT i a m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
        | Bool
otherwise = a -> ConduitT i a m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield a
x ConduitT i a m () -> ConduitT i a m () -> ConduitT i a m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> ConduitT i a m ()
loop (a -> a
forall a. Enum a => a -> a
Prelude.succ a
x)
{-# INLINE enumFromToC #-}
STREAMING(enumFromTo, enumFromToC, enumFromToS, x0 y)

-- | Produces an infinite stream of repeated applications of f to x.
--
-- Subject to fusion
--
iterate, iterateC :: Monad m => (a -> a) -> a -> ConduitT i a m ()
iterateC :: (a -> a) -> a -> ConduitT i a m ()
iterateC a -> a
f =
    a -> ConduitT i a m ()
forall (m :: * -> *) i b. Monad m => a -> ConduitT i a m b
go
  where
    go :: a -> ConduitT i a m b
go a
a = a -> ConduitT i a m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield a
a ConduitT i a m () -> ConduitT i a m b -> ConduitT i a m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> ConduitT i a m b
go (a -> a
f a
a)
{-# INLINE iterateC #-}
STREAMING(iterate, iterateC, iterateS, f a)

-- | Replicate a single value the given number of times.
--
-- Subject to fusion
--
-- Since 1.2.0
replicate, replicateC :: Monad m => Int -> a -> ConduitT i a m ()
replicateC :: Int -> a -> ConduitT i a m ()
replicateC Int
cnt0 a
a =
    Int -> ConduitT i a m ()
forall t (m :: * -> *) i.
(Ord t, Num t, Monad m) =>
t -> ConduitT i a m ()
loop Int
cnt0
  where
    loop :: t -> ConduitT i a m ()
loop t
i
        | t
i t -> t -> Bool
forall a. Ord a => a -> a -> Bool
<= t
0 = () -> ConduitT i a m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
        | Bool
otherwise = a -> ConduitT i a m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield a
a ConduitT i a m () -> ConduitT i a m () -> ConduitT i a m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> t -> ConduitT i a m ()
loop (t
i t -> t -> t
forall a. Num a => a -> a -> a
- t
1)
{-# INLINE replicateC #-}
STREAMING(replicate, replicateC, replicateS, cnt0 a)

-- | Replicate a monadic value the given number of times.
--
-- Subject to fusion
--
-- Since 1.2.0
replicateM, replicateMC :: Monad m => Int -> m a -> ConduitT i a m ()
replicateMC :: Int -> m a -> ConduitT i a m ()
replicateMC Int
cnt0 m a
ma =
    Int -> ConduitT i a m ()
forall t i. (Ord t, Num t) => t -> ConduitT i a m ()
loop Int
cnt0
  where
    loop :: t -> ConduitT i a m ()
loop t
i
        | t
i t -> t -> Bool
forall a. Ord a => a -> a -> Bool
<= t
0 = () -> ConduitT i a m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
        | Bool
otherwise = m a -> ConduitT i a m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m a
ma ConduitT i a m a -> (a -> ConduitT i a m ()) -> ConduitT i a m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> ConduitT i a m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield ConduitT i a m () -> ConduitT i a m () -> ConduitT i a m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> t -> ConduitT i a m ()
loop (t
i t -> t -> t
forall a. Num a => a -> a -> a
- t
1)
{-# INLINE replicateMC #-}
STREAMING(replicateM, replicateMC, replicateMS, cnt0 ma)

-- | A strict left fold.
--
-- Subject to fusion
--
-- Since 0.3.0
fold, foldC :: Monad m
            => (b -> a -> b)
            -> b
            -> ConduitT a o m b
foldC :: (b -> a -> b) -> b -> ConduitT a o m b
foldC b -> a -> b
f =
    b -> ConduitT a o m b
forall (m :: * -> *) o. Monad m => b -> ConduitT a o m b
loop
  where
    loop :: b -> ConduitT a o m b
loop !b
accum = ConduitT a o m (Maybe a)
forall (m :: * -> *) i. Monad m => Consumer i m (Maybe i)
await ConduitT a o m (Maybe a)
-> (Maybe a -> ConduitT a o m b) -> ConduitT a o m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ConduitT a o m b
-> (a -> ConduitT a o m b) -> Maybe a -> ConduitT a o m b
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (b -> ConduitT a o m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
accum) (b -> ConduitT a o m b
loop (b -> ConduitT a o m b) -> (a -> b) -> a -> ConduitT a o m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> a -> b
f b
accum)
{-# INLINE foldC #-}
STREAMING(fold, foldC, foldS, f accum)

-- | A monadic strict left fold.
--
-- Subject to fusion
--
-- Since 0.3.0
foldM, foldMC :: Monad m
              => (b -> a -> m b)
              -> b
              -> ConduitT a o m b
foldMC :: (b -> a -> m b) -> b -> ConduitT a o m b
foldMC b -> a -> m b
f =
    b -> ConduitT a o m b
forall o. b -> ConduitT a o m b
loop
  where
    loop :: b -> ConduitT a o m b
loop b
accum = do
        ConduitT a o m (Maybe a)
forall (m :: * -> *) i. Monad m => Consumer i m (Maybe i)
await ConduitT a o m (Maybe a)
-> (Maybe a -> ConduitT a o m b) -> ConduitT a o m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ConduitT a o m b
-> (a -> ConduitT a o m b) -> Maybe a -> ConduitT a o m b
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (b -> ConduitT a o m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
accum) a -> ConduitT a o m b
go
      where
        go :: a -> ConduitT a o m b
go a
a = do
            b
accum' <- m b -> ConduitT a o m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m b -> ConduitT a o m b) -> m b -> ConduitT a o m b
forall a b. (a -> b) -> a -> b
$ b -> a -> m b
f b
accum a
a
            b
accum' b -> ConduitT a o m b -> ConduitT a o m b
`seq` b -> ConduitT a o m b
loop b
accum'
{-# INLINE foldMC #-}
STREAMING(foldM, foldMC, foldMS, f accum)

-----------------------------------------------------------------
-- These are for cases where- for whatever reason- stream fusion cannot be
-- applied.
connectFold :: Monad m => ConduitT () a m () -> (b -> a -> b) -> b -> m b
connectFold :: ConduitT () a m () -> (b -> a -> b) -> b -> m b
connectFold (CI.ConduitT forall b. (() -> Pipe () () a () m b) -> Pipe () () a () m b
src0) b -> a -> b
f =
    Pipe () () a () m () -> b -> m b
forall (m :: * -> *) i. Monad m => Pipe () i a () m () -> b -> m b
go ((() -> Pipe () () a () m ()) -> Pipe () () a () m ()
forall b. (() -> Pipe () () a () m b) -> Pipe () () a () m b
src0 () -> Pipe () () a () m ()
forall l i o u (m :: * -> *) r. r -> Pipe l i o u m r
CI.Done)
  where
    go :: Pipe () i a () m () -> b -> m b
go (CI.Done ()) b
b = b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
b
    go (CI.HaveOutput Pipe () i a () m ()
src a
a) b
b = Pipe () i a () m () -> b -> m b
go Pipe () i a () m ()
src (b -> m b) -> b -> m b
forall a b. (a -> b) -> a -> b
Prelude.$! b -> a -> b
f b
b a
a
    go (CI.NeedInput i -> Pipe () i a () m ()
_ () -> Pipe () i a () m ()
c) b
b = Pipe () i a () m () -> b -> m b
go (() -> Pipe () i a () m ()
c ()) b
b
    go (CI.Leftover Pipe () i a () m ()
src ()) b
b = Pipe () i a () m () -> b -> m b
go Pipe () i a () m ()
src b
b
    go (CI.PipeM m (Pipe () i a () m ())
msrc) b
b = do
        Pipe () i a () m ()
src <- m (Pipe () i a () m ())
msrc
        Pipe () i a () m () -> b -> m b
go Pipe () i a () m ()
src b
b
{-# INLINE connectFold #-}
{-# RULES "conduit: $$ fold" forall src f b. runConduit (src .| fold f b) = connectFold src f b #-}

connectFoldM :: Monad m => ConduitT () a m () -> (b -> a -> m b) -> b -> m b
connectFoldM :: ConduitT () a m () -> (b -> a -> m b) -> b -> m b
connectFoldM (CI.ConduitT forall b. (() -> Pipe () () a () m b) -> Pipe () () a () m b
src0) b -> a -> m b
f =
    Pipe () () a () m () -> b -> m b
forall i. Pipe () i a () m () -> b -> m b
go ((() -> Pipe () () a () m ()) -> Pipe () () a () m ()
forall b. (() -> Pipe () () a () m b) -> Pipe () () a () m b
src0 () -> Pipe () () a () m ()
forall l i o u (m :: * -> *) r. r -> Pipe l i o u m r
CI.Done)
  where
    go :: Pipe () i a () m () -> b -> m b
go (CI.Done ()) b
b = b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
b
    go (CI.HaveOutput Pipe () i a () m ()
src a
a) b
b = do
        !b
b' <- b -> a -> m b
f b
b a
a
        Pipe () i a () m () -> b -> m b
go Pipe () i a () m ()
src b
b'
    go (CI.NeedInput i -> Pipe () i a () m ()
_ () -> Pipe () i a () m ()
c) b
b = Pipe () i a () m () -> b -> m b
go (() -> Pipe () i a () m ()
c ()) b
b
    go (CI.Leftover Pipe () i a () m ()
src ()) b
b = Pipe () i a () m () -> b -> m b
go Pipe () i a () m ()
src b
b
    go (CI.PipeM m (Pipe () i a () m ())
msrc) b
b = do
        Pipe () i a () m ()
src <- m (Pipe () i a () m ())
msrc
        Pipe () i a () m () -> b -> m b
go Pipe () i a () m ()
src b
b
{-# INLINE connectFoldM #-}
{-# RULES "conduit: $$ foldM" forall src f b. runConduit (src .| foldM f b) = connectFoldM src f b #-}
-----------------------------------------------------------------

-- | A monoidal strict left fold.
--
-- Subject to fusion
--
-- Since 0.5.3
foldMap :: (Monad m, Monoid b)
        => (a -> b)
        -> ConduitT a o m b
INLINE_RULE(foldMap, f, let combiner accum = mappend accum . f in fold combiner mempty)

-- | A monoidal strict left fold in a Monad.
--
-- Since 1.0.8
foldMapM :: (Monad m, Monoid b)
        => (a -> m b)
        -> ConduitT a o m b
INLINE_RULE(foldMapM, f, let combiner accum = liftM (mappend accum) . f in foldM combiner mempty)

-- | Apply the action to all values in the stream.
--
-- Subject to fusion
--
-- Since 0.3.0
mapM_, mapM_C :: Monad m
              => (a -> m ())
              -> ConduitT a o m ()
mapM_C :: (a -> m ()) -> ConduitT a o m ()
mapM_C a -> m ()
f = (a -> ConduitT a o m ()) -> ConduitT a o m ()
forall (m :: * -> *) i o r.
Monad m =>
(i -> ConduitT i o m r) -> ConduitT i o m ()
awaitForever ((a -> ConduitT a o m ()) -> ConduitT a o m ())
-> (a -> ConduitT a o m ()) -> ConduitT a o m ()
forall a b. (a -> b) -> a -> b
$ m () -> ConduitT a o m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> ConduitT a o m ())
-> (a -> m ()) -> a -> ConduitT a o m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m ()
f
{-# INLINE mapM_C #-}
STREAMING(mapM_, mapM_C, mapM_S, f)

srcMapM_ :: Monad m => ConduitT () a m () -> (a -> m ()) -> m ()
srcMapM_ :: ConduitT () a m () -> (a -> m ()) -> m ()
srcMapM_ (CI.ConduitT forall b. (() -> Pipe () () a () m b) -> Pipe () () a () m b
src) a -> m ()
f =
    Pipe () () a () m () -> m ()
forall i. Pipe () i a () m () -> m ()
go ((() -> Pipe () () a () m ()) -> Pipe () () a () m ()
forall b. (() -> Pipe () () a () m b) -> Pipe () () a () m b
src () -> Pipe () () a () m ()
forall l i o u (m :: * -> *) r. r -> Pipe l i o u m r
CI.Done)
  where
    go :: Pipe () i a () m () -> m ()
go (CI.Done ()) = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    go (CI.PipeM m (Pipe () i a () m ())
mp) = m (Pipe () i a () m ())
mp m (Pipe () i a () m ()) -> (Pipe () i a () m () -> m ()) -> m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Pipe () i a () m () -> m ()
go
    go (CI.Leftover Pipe () i a () m ()
p ()) = Pipe () i a () m () -> m ()
go Pipe () i a () m ()
p
    go (CI.HaveOutput Pipe () i a () m ()
p a
o) = a -> m ()
f a
o m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Pipe () i a () m () -> m ()
go Pipe () i a () m ()
p
    go (CI.NeedInput i -> Pipe () i a () m ()
_ () -> Pipe () i a () m ()
c) = Pipe () i a () m () -> m ()
go (() -> Pipe () i a () m ()
c ())
{-# INLINE srcMapM_ #-}
{-# RULES "conduit: connect to mapM_" [2] forall f src. runConduit (src .| mapM_ f) = srcMapM_ src f #-}

-- | Ignore a certain number of values in the stream. This function is
-- semantically equivalent to:
--
-- > drop i = take i >> return ()
--
-- However, @drop@ is more efficient as it does not need to hold values in
-- memory.
--
-- Subject to fusion
--
-- Since 0.3.0
drop, dropC :: Monad m
            => Int
            -> ConduitT a o m ()
dropC :: Int -> ConduitT a o m ()
dropC =
    Int -> ConduitT a o m ()
forall t (m :: * -> *) a o.
(Ord t, Num t, Monad m) =>
t -> ConduitT a o m ()
loop
  where
    loop :: t -> ConduitT a o m ()
loop t
i | t
i t -> t -> Bool
forall a. Ord a => a -> a -> Bool
<= t
0 = () -> ConduitT a o m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    loop t
count = ConduitT a o m (Maybe a)
forall (m :: * -> *) i. Monad m => Consumer i m (Maybe i)
await ConduitT a o m (Maybe a)
-> (Maybe a -> ConduitT a o m ()) -> ConduitT a o m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ConduitT a o m ()
-> (a -> ConduitT a o m ()) -> Maybe a -> ConduitT a o m ()
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (() -> ConduitT a o m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()) (\a
_ -> t -> ConduitT a o m ()
loop (t
count t -> t -> t
forall a. Num a => a -> a -> a
- t
1))
{-# INLINE dropC #-}
STREAMING(drop, dropC, dropS, i)

-- | Take some values from the stream and return as a list. If you want to
-- instead create a conduit that pipes data to another sink, see 'isolate'.
-- This function is semantically equivalent to:
--
-- > take i = isolate i =$ consume
--
-- Subject to fusion
--
-- Since 0.3.0
take, takeC :: Monad m
            => Int
            -> ConduitT a o m [a]
takeC :: Int -> ConduitT a o m [a]
takeC =
    ([a] -> [a]) -> Int -> ConduitT a o m [a]
forall t (m :: * -> *) a b o.
(Ord t, Num t, Monad m) =>
([a] -> b) -> t -> ConduitT a o m b
loop [a] -> [a]
forall a. a -> a
id
  where
    loop :: ([a] -> b) -> t -> ConduitT a o m b
loop [a] -> b
front t
count | t
count t -> t -> Bool
forall a. Ord a => a -> a -> Bool
<= t
0 = b -> ConduitT a o m b
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> ConduitT a o m b) -> b -> ConduitT a o m b
forall a b. (a -> b) -> a -> b
$ [a] -> b
front []
    loop [a] -> b
front t
count = ConduitT a o m (Maybe a)
forall (m :: * -> *) i. Monad m => Consumer i m (Maybe i)
await ConduitT a o m (Maybe a)
-> (Maybe a -> ConduitT a o m b) -> ConduitT a o m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ConduitT a o m b
-> (a -> ConduitT a o m b) -> Maybe a -> ConduitT a o m b
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
        (b -> ConduitT a o m b
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> ConduitT a o m b) -> b -> ConduitT a o m b
forall a b. (a -> b) -> a -> b
$ [a] -> b
front [])
        (\a
x -> ([a] -> b) -> t -> ConduitT a o m b
loop ([a] -> b
front ([a] -> b) -> ([a] -> [a]) -> [a] -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:)) (t
count t -> t -> t
forall a. Num a => a -> a -> a
- t
1))
{-# INLINE takeC #-}
STREAMING(take, takeC, takeS, i)

-- | Take a single value from the stream, if available.
--
-- Subject to fusion
--
-- Since 0.3.0
head, headC :: Monad m => ConduitT a o m (Maybe a)
headC :: ConduitT a o m (Maybe a)
headC = ConduitT a o m (Maybe a)
forall (m :: * -> *) i. Monad m => Consumer i m (Maybe i)
await
{-# INLINE headC #-}
STREAMING0(head, headC, headS)

-- | Look at the next value in the stream, if available. This function will not
-- change the state of the stream.
--
-- Since 0.3.0
peek :: Monad m => ConduitT a o m (Maybe a)
peek :: ConduitT a o m (Maybe a)
peek = ConduitT a o m (Maybe a)
forall (m :: * -> *) i. Monad m => Consumer i m (Maybe i)
await ConduitT a o m (Maybe a)
-> (Maybe a -> ConduitT a o m (Maybe a))
-> ConduitT a o m (Maybe a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ConduitT a o m (Maybe a)
-> (a -> ConduitT a o m (Maybe a))
-> Maybe a
-> ConduitT a o m (Maybe a)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Maybe a -> ConduitT a o m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing) (\a
x -> a -> ConduitT a o m ()
forall i o (m :: * -> *). i -> ConduitT i o m ()
leftover a
x ConduitT a o m ()
-> ConduitT a o m (Maybe a) -> ConduitT a o m (Maybe a)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Maybe a -> ConduitT a o m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
x))

-- | Apply a transformation to all values in a stream.
--
-- Subject to fusion
--
-- Since 0.3.0
map, mapC :: Monad m => (a -> b) -> ConduitT a b m ()
mapC :: (a -> b) -> ConduitT a b m ()
mapC a -> b
f = (a -> ConduitT a b m ()) -> ConduitT a b m ()
forall (m :: * -> *) i o r.
Monad m =>
(i -> ConduitT i o m r) -> ConduitT i o m ()
awaitForever ((a -> ConduitT a b m ()) -> ConduitT a b m ())
-> (a -> ConduitT a b m ()) -> ConduitT a b m ()
forall a b. (a -> b) -> a -> b
$ b -> ConduitT a b m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield (b -> ConduitT a b m ()) -> (a -> b) -> a -> ConduitT a b m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f
{-# INLINE mapC #-}
STREAMING(map, mapC, mapS, f)

-- Since a Source never has any leftovers, fusion rules on it are safe.
{-
{-# RULES "conduit: source/map fusion .|" forall f src. src .| map f = mapFuseRight src f #-}

mapFuseRight :: Monad m => Source m a -> (a -> b) -> Source m b
mapFuseRight src f = CIC.mapOutput f src
{-# INLINE mapFuseRight #-}
-}

{-

It might be nice to include these rewrite rules, but they may have subtle
differences based on leftovers.

{-# RULES "conduit: map-to-mapOutput pipeL" forall f src. pipeL src (map f) = mapOutput f src #-}
{-# RULES "conduit: map-to-mapOutput $=" forall f src. src $= (map f) = mapOutput f src #-}
{-# RULES "conduit: map-to-mapOutput pipe" forall f src. pipe src (map f) = mapOutput f src #-}
{-# RULES "conduit: map-to-mapOutput >+>" forall f src. src >+> (map f) = mapOutput f src #-}

{-# RULES "conduit: map-to-mapInput pipeL" forall f sink. pipeL (map f) sink = mapInput f (Prelude.const Prelude.Nothing) sink #-}
{-# RULES "conduit: map-to-mapInput =$" forall f sink. map f =$ sink = mapInput f (Prelude.const Prelude.Nothing) sink #-}
{-# RULES "conduit: map-to-mapInput pipe" forall f sink. pipe (map f) sink = mapInput f (Prelude.const Prelude.Nothing) sink #-}
{-# RULES "conduit: map-to-mapInput >+>" forall f sink. map f >+> sink = mapInput f (Prelude.const Prelude.Nothing) sink #-}

{-# RULES "conduit: map-to-mapOutput .|" forall f con. con .| map f = mapOutput f con #-}
{-# RULES "conduit: map-to-mapInput .|" forall f con. map f .| con = mapInput f (Prelude.const Prelude.Nothing) con #-}

{-# INLINE [1] map #-}

-}

-- | Apply a monadic transformation to all values in a stream.
--
-- If you do not need the transformed values, and instead just want the monadic
-- side-effects of running the action, see 'mapM_'.
--
-- Subject to fusion
--
-- Since 0.3.0
mapM, mapMC :: Monad m => (a -> m b) -> ConduitT a b m ()
mapMC :: (a -> m b) -> ConduitT a b m ()
mapMC a -> m b
f = (a -> ConduitT a b m ()) -> ConduitT a b m ()
forall (m :: * -> *) i o r.
Monad m =>
(i -> ConduitT i o m r) -> ConduitT i o m ()
awaitForever ((a -> ConduitT a b m ()) -> ConduitT a b m ())
-> (a -> ConduitT a b m ()) -> ConduitT a b m ()
forall a b. (a -> b) -> a -> b
$ \a
a -> m b -> ConduitT a b m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (a -> m b
f a
a) ConduitT a b m b -> (b -> ConduitT a b m ()) -> ConduitT a b m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= b -> ConduitT a b m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield
{-# INLINE mapMC #-}
STREAMING(mapM, mapMC, mapMS, f)

-- | Apply a monadic action on all values in a stream.
--
-- This @Conduit@ can be used to perform a monadic side-effect for every
-- value, whilst passing the value through the @Conduit@ as-is.
--
-- > iterM f = mapM (\a -> f a >>= \() -> return a)
--
-- Subject to fusion
--
-- Since 0.5.6
iterM, iterMC :: Monad m => (a -> m ()) -> ConduitT a a m ()
iterMC :: (a -> m ()) -> ConduitT a a m ()
iterMC a -> m ()
f = (a -> ConduitT a a m ()) -> ConduitT a a m ()
forall (m :: * -> *) i o r.
Monad m =>
(i -> ConduitT i o m r) -> ConduitT i o m ()
awaitForever ((a -> ConduitT a a m ()) -> ConduitT a a m ())
-> (a -> ConduitT a a m ()) -> ConduitT a a m ()
forall a b. (a -> b) -> a -> b
$ \a
a -> m () -> ConduitT a a m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (a -> m ()
f a
a) ConduitT a a m () -> ConduitT a a m () -> ConduitT a a m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> ConduitT a a m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield a
a
{-# INLINE iterMC #-}
STREAMING(iterM, iterMC, iterMS, f)

-- | Apply a transformation that may fail to all values in a stream, discarding
-- the failures.
--
-- Subject to fusion
--
-- Since 0.5.1
mapMaybe, mapMaybeC :: Monad m => (a -> Maybe b) -> ConduitT a b m ()
mapMaybeC :: (a -> Maybe b) -> ConduitT a b m ()
mapMaybeC a -> Maybe b
f = (a -> ConduitT a b m ()) -> ConduitT a b m ()
forall (m :: * -> *) i o r.
Monad m =>
(i -> ConduitT i o m r) -> ConduitT i o m ()
awaitForever ((a -> ConduitT a b m ()) -> ConduitT a b m ())
-> (a -> ConduitT a b m ()) -> ConduitT a b m ()
forall a b. (a -> b) -> a -> b
$ ConduitT a b m ()
-> (b -> ConduitT a b m ()) -> Maybe b -> ConduitT a b m ()
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (() -> ConduitT a b m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()) b -> ConduitT a b m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield (Maybe b -> ConduitT a b m ())
-> (a -> Maybe b) -> a -> ConduitT a b m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Maybe b
f
{-# INLINE mapMaybeC #-}
STREAMING(mapMaybe, mapMaybeC, mapMaybeS, f)

-- | Apply a monadic transformation that may fail to all values in a stream,
-- discarding the failures.
--
-- Subject to fusion
--
-- Since 0.5.1
mapMaybeM, mapMaybeMC :: Monad m => (a -> m (Maybe b)) -> ConduitT a b m ()
mapMaybeMC :: (a -> m (Maybe b)) -> ConduitT a b m ()
mapMaybeMC a -> m (Maybe b)
f = (a -> ConduitT a b m ()) -> ConduitT a b m ()
forall (m :: * -> *) i o r.
Monad m =>
(i -> ConduitT i o m r) -> ConduitT i o m ()
awaitForever ((a -> ConduitT a b m ()) -> ConduitT a b m ())
-> (a -> ConduitT a b m ()) -> ConduitT a b m ()
forall a b. (a -> b) -> a -> b
$ ConduitT a b m ()
-> (b -> ConduitT a b m ()) -> Maybe b -> ConduitT a b m ()
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (() -> ConduitT a b m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()) b -> ConduitT a b m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield (Maybe b -> ConduitT a b m ())
-> (a -> ConduitT a b m (Maybe b)) -> a -> ConduitT a b m ()
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< m (Maybe b) -> ConduitT a b m (Maybe b)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Maybe b) -> ConduitT a b m (Maybe b))
-> (a -> m (Maybe b)) -> a -> ConduitT a b m (Maybe b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m (Maybe b)
f
{-# INLINE mapMaybeMC #-}
STREAMING(mapMaybeM, mapMaybeMC, mapMaybeMS, f)

-- | Filter the @Just@ values from a stream, discarding the @Nothing@  values.
--
-- Subject to fusion
--
-- Since 0.5.1
catMaybes, catMaybesC :: Monad m => ConduitT (Maybe a) a m ()
catMaybesC :: ConduitT (Maybe a) a m ()
catMaybesC = (Maybe a -> ConduitT (Maybe a) a m ()) -> ConduitT (Maybe a) a m ()
forall (m :: * -> *) i o r.
Monad m =>
(i -> ConduitT i o m r) -> ConduitT i o m ()
awaitForever ((Maybe a -> ConduitT (Maybe a) a m ())
 -> ConduitT (Maybe a) a m ())
-> (Maybe a -> ConduitT (Maybe a) a m ())
-> ConduitT (Maybe a) a m ()
forall a b. (a -> b) -> a -> b
$ ConduitT (Maybe a) a m ()
-> (a -> ConduitT (Maybe a) a m ())
-> Maybe a
-> ConduitT (Maybe a) a m ()
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (() -> ConduitT (Maybe a) a m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()) a -> ConduitT (Maybe a) a m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield
{-# INLINE catMaybesC #-}
STREAMING0(catMaybes, catMaybesC, catMaybesS)

-- | Generalization of 'catMaybes'. It puts all values from
--   'F.Foldable' into stream.
--
-- Subject to fusion
--
-- Since 1.0.6
concat, concatC :: (Monad m, F.Foldable f) => ConduitT (f a) a m ()
concatC :: ConduitT (f a) a m ()
concatC = (f a -> ConduitT (f a) a m ()) -> ConduitT (f a) a m ()
forall (m :: * -> *) i o r.
Monad m =>
(i -> ConduitT i o m r) -> ConduitT i o m ()
awaitForever ((f a -> ConduitT (f a) a m ()) -> ConduitT (f a) a m ())
-> (f a -> ConduitT (f a) a m ()) -> ConduitT (f a) a m ()
forall a b. (a -> b) -> a -> b
$ (a -> ConduitT (f a) a m ()) -> f a -> ConduitT (f a) a m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
F.mapM_ a -> ConduitT (f a) a m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield
{-# INLINE concatC #-}
STREAMING0(concat, concatC, concatS)

-- | Apply a transformation to all values in a stream, concatenating the output
-- values.
--
-- Subject to fusion
--
-- Since 0.3.0
concatMap, concatMapC :: Monad m => (a -> [b]) -> ConduitT a b m ()
concatMapC :: (a -> [b]) -> ConduitT a b m ()
concatMapC a -> [b]
f = (a -> ConduitT a b m ()) -> ConduitT a b m ()
forall (m :: * -> *) i o r.
Monad m =>
(i -> ConduitT i o m r) -> ConduitT i o m ()
awaitForever ((a -> ConduitT a b m ()) -> ConduitT a b m ())
-> (a -> ConduitT a b m ()) -> ConduitT a b m ()
forall a b. (a -> b) -> a -> b
$ [b] -> ConduitT a b m ()
forall (m :: * -> *) a i. Monad m => [a] -> ConduitT i a m ()
sourceList ([b] -> ConduitT a b m ()) -> (a -> [b]) -> a -> ConduitT a b m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [b]
f
{-# INLINE concatMapC #-}
STREAMING(concatMap, concatMapC, concatMapS, f)

-- | Apply a monadic transformation to all values in a stream, concatenating
-- the output values.
--
-- Subject to fusion
--
-- Since 0.3.0
concatMapM, concatMapMC :: Monad m => (a -> m [b]) -> ConduitT a b m ()
concatMapMC :: (a -> m [b]) -> ConduitT a b m ()
concatMapMC a -> m [b]
f = (a -> ConduitT a b m ()) -> ConduitT a b m ()
forall (m :: * -> *) i o r.
Monad m =>
(i -> ConduitT i o m r) -> ConduitT i o m ()
awaitForever ((a -> ConduitT a b m ()) -> ConduitT a b m ())
-> (a -> ConduitT a b m ()) -> ConduitT a b m ()
forall a b. (a -> b) -> a -> b
$ [b] -> ConduitT a b m ()
forall (m :: * -> *) a i. Monad m => [a] -> ConduitT i a m ()
sourceList ([b] -> ConduitT a b m ())
-> (a -> ConduitT a b m [b]) -> a -> ConduitT a b m ()
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< m [b] -> ConduitT a b m [b]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m [b] -> ConduitT a b m [b])
-> (a -> m [b]) -> a -> ConduitT a b m [b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m [b]
f
{-# INLINE concatMapMC #-}
STREAMING(concatMapM, concatMapMC, concatMapMS, f)

-- | 'concatMap' with a strict accumulator.
--
-- Subject to fusion
--
-- Since 0.3.0
concatMapAccum, concatMapAccumC :: Monad m => (a -> accum -> (accum, [b])) -> accum -> ConduitT a b m ()
concatMapAccumC :: (a -> accum -> (accum, [b])) -> accum -> ConduitT a b m ()
concatMapAccumC a -> accum -> (accum, [b])
f accum
x0 = ConduitT a [b] m accum -> ConduitT a [b] m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void ((a -> accum -> (accum, [b])) -> accum -> ConduitT a [b] m accum
forall (m :: * -> *) a s b.
Monad m =>
(a -> s -> (s, b)) -> s -> ConduitT a b m s
mapAccum a -> accum -> (accum, [b])
f accum
x0) ConduitT a [b] m () -> ConduitM [b] b m () -> ConduitT a b m ()
forall (m :: * -> *) a b c r.
Monad m =>
ConduitM a b m () -> ConduitM b c m r -> ConduitM a c m r
.| ConduitM [b] b m ()
forall (m :: * -> *) (f :: * -> *) a.
(Monad m, Foldable f) =>
ConduitT (f a) a m ()
concat
{-# INLINE concatMapAccumC #-}
STREAMING(concatMapAccum, concatMapAccumC, concatMapAccumS, f x0)

-- | Deprecated synonym for @mapAccum@
--
-- Since 1.0.6
scanl :: Monad m => (a -> s -> (s, b)) -> s -> ConduitT a b m ()
scanl :: (a -> s -> (s, b)) -> s -> ConduitT a b m ()
scanl a -> s -> (s, b)
f s
s = ConduitT a b m s -> ConduitT a b m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ConduitT a b m s -> ConduitT a b m ())
-> ConduitT a b m s -> ConduitT a b m ()
forall a b. (a -> b) -> a -> b
$ (a -> s -> (s, b)) -> s -> ConduitT a b m s
forall (m :: * -> *) a s b.
Monad m =>
(a -> s -> (s, b)) -> s -> ConduitT a b m s
mapAccum a -> s -> (s, b)
f s
s
{-# DEPRECATED scanl "Use mapAccum instead" #-}

-- | Deprecated synonym for @mapAccumM@
--
-- Since 1.0.6
scanlM :: Monad m => (a -> s -> m (s, b)) -> s -> ConduitT a b m ()
scanlM :: (a -> s -> m (s, b)) -> s -> ConduitT a b m ()
scanlM a -> s -> m (s, b)
f s
s = ConduitT a b m s -> ConduitT a b m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ConduitT a b m s -> ConduitT a b m ())
-> ConduitT a b m s -> ConduitT a b m ()
forall a b. (a -> b) -> a -> b
$ (a -> s -> m (s, b)) -> s -> ConduitT a b m s
forall (m :: * -> *) a s b.
Monad m =>
(a -> s -> m (s, b)) -> s -> ConduitT a b m s
mapAccumM a -> s -> m (s, b)
f s
s
{-# DEPRECATED scanlM "Use mapAccumM instead" #-}

-- | Analog of @mapAccumL@ for lists. Note that in contrast to @mapAccumL@, the function argument
--   takes the accumulator as its second argument, not its first argument, and the accumulated value
--   is strict.
--
-- Subject to fusion
--
-- Since 1.1.1
mapAccum, mapAccumC :: Monad m => (a -> s -> (s, b)) -> s -> ConduitT a b m s
mapAccumC :: (a -> s -> (s, b)) -> s -> ConduitT a b m s
mapAccumC a -> s -> (s, b)
f =
    s -> ConduitT a b m s
forall (m :: * -> *). Monad m => s -> ConduitT a b m s
loop
  where
    loop :: s -> ConduitT a b m s
loop !s
s = ConduitT a b m (Maybe a)
forall (m :: * -> *) i. Monad m => Consumer i m (Maybe i)
await ConduitT a b m (Maybe a)
-> (Maybe a -> ConduitT a b m s) -> ConduitT a b m s
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ConduitT a b m s
-> (a -> ConduitT a b m s) -> Maybe a -> ConduitT a b m s
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (s -> ConduitT a b m s
forall (m :: * -> *) a. Monad m => a -> m a
return s
s) a -> ConduitT a b m s
go
      where
        go :: a -> ConduitT a b m s
go a
a = case a -> s -> (s, b)
f a
a s
s of
                 (s
s', b
b) -> b -> ConduitT a b m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield b
b ConduitT a b m () -> ConduitT a b m s -> ConduitT a b m s
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> s -> ConduitT a b m s
loop s
s'
STREAMING(mapAccum, mapAccumC, mapAccumS, f s)

-- | Monadic `mapAccum`.
--
-- Subject to fusion
--
-- Since 1.1.1
mapAccumM, mapAccumMC :: Monad m => (a -> s -> m (s, b)) -> s -> ConduitT a b m s
mapAccumMC :: (a -> s -> m (s, b)) -> s -> ConduitT a b m s
mapAccumMC a -> s -> m (s, b)
f =
    s -> ConduitT a b m s
loop
  where
    loop :: s -> ConduitT a b m s
loop !s
s = ConduitT a b m (Maybe a)
forall (m :: * -> *) i. Monad m => Consumer i m (Maybe i)
await ConduitT a b m (Maybe a)
-> (Maybe a -> ConduitT a b m s) -> ConduitT a b m s
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ConduitT a b m s
-> (a -> ConduitT a b m s) -> Maybe a -> ConduitT a b m s
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (s -> ConduitT a b m s
forall (m :: * -> *) a. Monad m => a -> m a
return s
s) a -> ConduitT a b m s
go
      where
        go :: a -> ConduitT a b m s
go a
a = do (s
s', b
b) <- m (s, b) -> ConduitT a b m (s, b)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (s, b) -> ConduitT a b m (s, b))
-> m (s, b) -> ConduitT a b m (s, b)
forall a b. (a -> b) -> a -> b
$ a -> s -> m (s, b)
f a
a s
s
                  b -> ConduitT a b m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield b
b
                  s -> ConduitT a b m s
loop s
s'
{-# INLINE mapAccumMC #-}
STREAMING(mapAccumM, mapAccumMC, mapAccumMS, f s)

-- | Analog of 'Prelude.scanl' for lists.
--
-- Subject to fusion
--
-- Since 1.1.1
scan :: Monad m => (a -> b -> b) -> b -> ConduitT a b m b
INLINE_RULE(scan, f, mapAccum (\a b -> let r = f a b in (r, r)))

-- | Monadic @scanl@.
--
-- Subject to fusion
--
-- Since 1.1.1
scanM :: Monad m => (a -> b -> m b) -> b -> ConduitT a b m b
INLINE_RULE(scanM, f, mapAccumM (\a b -> f a b >>= \r -> return (r, r)))

-- | 'concatMapM' with a strict accumulator.
--
-- Subject to fusion
--
-- Since 0.3.0
concatMapAccumM, concatMapAccumMC :: Monad m => (a -> accum -> m (accum, [b])) -> accum -> ConduitT a b m ()
concatMapAccumMC :: (a -> accum -> m (accum, [b])) -> accum -> ConduitT a b m ()
concatMapAccumMC a -> accum -> m (accum, [b])
f accum
x0 = ConduitT a [b] m accum -> ConduitT a [b] m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void ((a -> accum -> m (accum, [b])) -> accum -> ConduitT a [b] m accum
forall (m :: * -> *) a s b.
Monad m =>
(a -> s -> m (s, b)) -> s -> ConduitT a b m s
mapAccumM a -> accum -> m (accum, [b])
f accum
x0) ConduitT a [b] m () -> ConduitM [b] b m () -> ConduitT a b m ()
forall (m :: * -> *) a b c r.
Monad m =>
ConduitM a b m () -> ConduitM b c m r -> ConduitM a c m r
.| ConduitM [b] b m ()
forall (m :: * -> *) (f :: * -> *) a.
(Monad m, Foldable f) =>
ConduitT (f a) a m ()
concat
{-# INLINE concatMapAccumMC #-}
STREAMING(concatMapAccumM, concatMapAccumMC, concatMapAccumMS, f x0)

-- | Generalization of 'mapMaybe' and 'concatMap'. It applies function
-- to all values in a stream and send values inside resulting
-- 'Foldable' downstream.
--
-- Subject to fusion
--
-- Since 1.0.6
mapFoldable, mapFoldableC :: (Monad m, F.Foldable f) => (a -> f b) -> ConduitT a b m ()
mapFoldableC :: (a -> f b) -> ConduitT a b m ()
mapFoldableC a -> f b
f = (a -> ConduitT a b m ()) -> ConduitT a b m ()
forall (m :: * -> *) i o r.
Monad m =>
(i -> ConduitT i o m r) -> ConduitT i o m ()
awaitForever ((a -> ConduitT a b m ()) -> ConduitT a b m ())
-> (a -> ConduitT a b m ()) -> ConduitT a b m ()
forall a b. (a -> b) -> a -> b
$ (b -> ConduitT a b m ()) -> f b -> ConduitT a b m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
F.mapM_ b -> ConduitT a b m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield (f b -> ConduitT a b m ()) -> (a -> f b) -> a -> ConduitT a b m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> f b
f
{-# INLINE mapFoldableC #-}
STREAMING(mapFoldable, mapFoldableC, mapFoldableS, f)

-- | Monadic variant of 'mapFoldable'.
--
-- Subject to fusion
--
-- Since 1.0.6
mapFoldableM, mapFoldableMC :: (Monad m, F.Foldable f) => (a -> m (f b)) -> ConduitT a b m ()
mapFoldableMC :: (a -> m (f b)) -> ConduitT a b m ()
mapFoldableMC a -> m (f b)
f = (a -> ConduitT a b m ()) -> ConduitT a b m ()
forall (m :: * -> *) i o r.
Monad m =>
(i -> ConduitT i o m r) -> ConduitT i o m ()
awaitForever ((a -> ConduitT a b m ()) -> ConduitT a b m ())
-> (a -> ConduitT a b m ()) -> ConduitT a b m ()
forall a b. (a -> b) -> a -> b
$ (b -> ConduitT a b m ()) -> f b -> ConduitT a b m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
F.mapM_ b -> ConduitT a b m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield (f b -> ConduitT a b m ())
-> (a -> ConduitT a b m (f b)) -> a -> ConduitT a b m ()
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< m (f b) -> ConduitT a b m (f b)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (f b) -> ConduitT a b m (f b))
-> (a -> m (f b)) -> a -> ConduitT a b m (f b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m (f b)
f
{-# INLINE mapFoldableMC #-}
STREAMING(mapFoldableM, mapFoldableMC, mapFoldableMS, f)

-- | Consume all values from the stream and return as a list. Note that this
-- will pull all values into memory.
--
-- Subject to fusion
--
-- Since 0.3.0
consume, consumeC :: Monad m => ConduitT a o m [a]
consumeC :: ConduitT a o m [a]
consumeC =
    ([a] -> [a]) -> ConduitT a o m [a]
forall (m :: * -> *) a b o.
Monad m =>
([a] -> b) -> ConduitT a o m b
loop [a] -> [a]
forall a. a -> a
id
  where
    loop :: ([a] -> b) -> ConduitT a o m b
loop [a] -> b
front = ConduitT a o m (Maybe a)
forall (m :: * -> *) i. Monad m => Consumer i m (Maybe i)
await ConduitT a o m (Maybe a)
-> (Maybe a -> ConduitT a o m b) -> ConduitT a o m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ConduitT a o m b
-> (a -> ConduitT a o m b) -> Maybe a -> ConduitT a o m b
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (b -> ConduitT a o m b
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> ConduitT a o m b) -> b -> ConduitT a o m b
forall a b. (a -> b) -> a -> b
$ [a] -> b
front []) (\a
x -> ([a] -> b) -> ConduitT a o m b
loop (([a] -> b) -> ConduitT a o m b) -> ([a] -> b) -> ConduitT a o m b
forall a b. (a -> b) -> a -> b
$ [a] -> b
front ([a] -> b) -> ([a] -> [a]) -> [a] -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:))
{-# INLINE consumeC #-}
STREAMING0(consume, consumeC, consumeS)

-- | Group a stream into chunks of a given size. The last chunk may contain
-- fewer than n elements.
--
-- Subject to fusion
--
-- Since 1.2.9
chunksOf :: Monad m => Int -> ConduitT a [a] m ()
chunksOf :: Int -> ConduitT a [a] m ()
chunksOf Int
n = if Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 then Int -> ([a] -> [a]) -> ConduitT a [a] m ()
forall (m :: * -> *) a.
Monad m =>
Int -> ([a] -> [a]) -> ConduitT a [a] m ()
loop Int
n [a] -> [a]
forall a. a -> a
id else [Char] -> ConduitT a [a] m ()
forall a. HasCallStack => [Char] -> a
error ([Char] -> ConduitT a [a] m ()) -> [Char] -> ConduitT a [a] m ()
forall a b. (a -> b) -> a -> b
$ [Char]
"chunksOf size must be positive (given " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
n [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
")"
  where
    loop :: Int -> ([a] -> [a]) -> ConduitT a [a] m ()
loop Int
0 [a] -> [a]
rest = [a] -> ConduitT a [a] m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield ([a] -> [a]
rest []) ConduitT a [a] m () -> ConduitT a [a] m () -> ConduitT a [a] m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> ([a] -> [a]) -> ConduitT a [a] m ()
loop Int
n [a] -> [a]
forall a. a -> a
id
    loop Int
count [a] -> [a]
rest = ConduitT a [a] m (Maybe a)
forall (m :: * -> *) i. Monad m => Consumer i m (Maybe i)
await ConduitT a [a] m (Maybe a)
-> (Maybe a -> ConduitT a [a] m ()) -> ConduitT a [a] m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Maybe a
ma -> case Maybe a
ma of
      Maybe a
Nothing -> case [a] -> [a]
rest [] of
        [] -> () -> ConduitT a [a] m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
        [a]
nonempty -> [a] -> ConduitT a [a] m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield [a]
nonempty
      Just a
a -> Int -> ([a] -> [a]) -> ConduitT a [a] m ()
loop (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) ([a] -> [a]
rest ([a] -> [a]) -> ([a] -> [a]) -> [a] -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a
a a -> [a] -> [a]
forall a. a -> [a] -> [a]
:))

-- | Grouping input according to an equality function.
--
-- Subject to fusion
--
-- Since 0.3.0
groupBy, groupByC :: Monad m => (a -> a -> Bool) -> ConduitT a [a] m ()
groupByC :: (a -> a -> Bool) -> ConduitT a [a] m ()
groupByC a -> a -> Bool
f =
    ConduitT a [a] m ()
start
  where
    start :: ConduitT a [a] m ()
start = ConduitT a [a] m (Maybe a)
forall (m :: * -> *) i. Monad m => Consumer i m (Maybe i)
await ConduitT a [a] m (Maybe a)
-> (Maybe a -> ConduitT a [a] m ()) -> ConduitT a [a] m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ConduitT a [a] m ()
-> (a -> ConduitT a [a] m ()) -> Maybe a -> ConduitT a [a] m ()
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (() -> ConduitT a [a] m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()) (([a] -> [a]) -> a -> ConduitT a [a] m ()
forall (m :: * -> *).
Monad m =>
([a] -> [a]) -> a -> ConduitT a [a] m ()
loop [a] -> [a]
forall a. a -> a
id)

    loop :: ([a] -> [a]) -> a -> ConduitT a [a] m ()
loop [a] -> [a]
rest a
x =
        ConduitT a [a] m (Maybe a)
forall (m :: * -> *) i. Monad m => Consumer i m (Maybe i)
await ConduitT a [a] m (Maybe a)
-> (Maybe a -> ConduitT a [a] m ()) -> ConduitT a [a] m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ConduitT a [a] m ()
-> (a -> ConduitT a [a] m ()) -> Maybe a -> ConduitT a [a] m ()
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ([a] -> ConduitT a [a] m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield (a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a] -> [a]
rest [])) a -> ConduitT a [a] m ()
go
      where
        go :: a -> ConduitT a [a] m ()
go a
y
            | a -> a -> Bool
f a
x a
y     = ([a] -> [a]) -> a -> ConduitT a [a] m ()
loop ([a] -> [a]
rest ([a] -> [a]) -> ([a] -> [a]) -> [a] -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a
ya -> [a] -> [a]
forall a. a -> [a] -> [a]
:)) a
x
            | Bool
otherwise = [a] -> ConduitT a [a] m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield (a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a] -> [a]
rest []) ConduitT a [a] m () -> ConduitT a [a] m () -> ConduitT a [a] m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ([a] -> [a]) -> a -> ConduitT a [a] m ()
loop [a] -> [a]
forall a. a -> a
id a
y
STREAMING(groupBy, groupByC, groupByS, f)

-- | 'groupOn1' is similar to @groupBy id@
--
-- returns a pair, indicating there are always 1 or more items in the grouping.
-- This is designed to be converted into a NonEmpty structure
-- but it avoids a dependency on another package
--
-- > import Data.List.NonEmpty
-- >
-- > groupOn1 :: (Monad m, Eq b) => (a -> b) -> Conduit a m (NonEmpty a)
-- > groupOn1 f = CL.groupOn1 f .| CL.map (uncurry (:|))
--
-- Subject to fusion
--
-- Since 1.1.7
groupOn1, groupOn1C :: (Monad m, Eq b)
                     => (a -> b)
                     -> ConduitT a (a, [a]) m ()
groupOn1C :: (a -> b) -> ConduitT a (a, [a]) m ()
groupOn1C a -> b
f =
    ConduitT a (a, [a]) m ()
start
  where
    start :: ConduitT a (a, [a]) m ()
start = ConduitT a (a, [a]) m (Maybe a)
forall (m :: * -> *) i. Monad m => Consumer i m (Maybe i)
await ConduitT a (a, [a]) m (Maybe a)
-> (Maybe a -> ConduitT a (a, [a]) m ())
-> ConduitT a (a, [a]) m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ConduitT a (a, [a]) m ()
-> (a -> ConduitT a (a, [a]) m ())
-> Maybe a
-> ConduitT a (a, [a]) m ()
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (() -> ConduitT a (a, [a]) m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()) (([a] -> [a]) -> a -> ConduitT a (a, [a]) m ()
forall (m :: * -> *).
Monad m =>
([a] -> [a]) -> a -> ConduitT a (a, [a]) m ()
loop [a] -> [a]
forall a. a -> a
id)

    loop :: ([a] -> [a]) -> a -> ConduitT a (a, [a]) m ()
loop [a] -> [a]
rest a
x =
        ConduitT a (a, [a]) m (Maybe a)
forall (m :: * -> *) i. Monad m => Consumer i m (Maybe i)
await ConduitT a (a, [a]) m (Maybe a)
-> (Maybe a -> ConduitT a (a, [a]) m ())
-> ConduitT a (a, [a]) m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ConduitT a (a, [a]) m ()
-> (a -> ConduitT a (a, [a]) m ())
-> Maybe a
-> ConduitT a (a, [a]) m ()
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ((a, [a]) -> ConduitT a (a, [a]) m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield (a
x, [a] -> [a]
rest [])) a -> ConduitT a (a, [a]) m ()
go
      where
        go :: a -> ConduitT a (a, [a]) m ()
go a
y
            | a -> b
f a
x b -> b -> Bool
forall a. Eq a => a -> a -> Bool
== a -> b
f a
y = ([a] -> [a]) -> a -> ConduitT a (a, [a]) m ()
loop ([a] -> [a]
rest ([a] -> [a]) -> ([a] -> [a]) -> [a] -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a
ya -> [a] -> [a]
forall a. a -> [a] -> [a]
:)) a
x
            | Bool
otherwise  = (a, [a]) -> ConduitT a (a, [a]) m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield (a
x, [a] -> [a]
rest []) ConduitT a (a, [a]) m ()
-> ConduitT a (a, [a]) m () -> ConduitT a (a, [a]) m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ([a] -> [a]) -> a -> ConduitT a (a, [a]) m ()
loop [a] -> [a]
forall a. a -> a
id a
y
STREAMING(groupOn1, groupOn1C, groupOn1S, f)

-- | Ensure that the inner sink consumes no more than the given number of
-- values. Note this this does /not/ ensure that the sink consumes all of those
-- values. To get the latter behavior, combine with 'sinkNull', e.g.:
--
-- > src $$ do
-- >     x <- isolate count =$ do
-- >         x <- someSink
-- >         sinkNull
-- >         return x
-- >     someOtherSink
-- >     ...
--
-- Subject to fusion
--
-- Since 0.3.0
isolate, isolateC :: Monad m => Int -> ConduitT a a m ()
isolateC :: Int -> ConduitT a a m ()
isolateC =
    Int -> ConduitT a a m ()
forall t (m :: * -> *) o.
(Ord t, Num t, Monad m) =>
t -> ConduitT o o m ()
loop
  where
    loop :: t -> ConduitT o o m ()
loop t
count | t
count t -> t -> Bool
forall a. Ord a => a -> a -> Bool
<= t
0 = () -> ConduitT o o m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    loop t
count = ConduitT o o m (Maybe o)
forall (m :: * -> *) i. Monad m => Consumer i m (Maybe i)
await ConduitT o o m (Maybe o)
-> (Maybe o -> ConduitT o o m ()) -> ConduitT o o m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ConduitT o o m ()
-> (o -> ConduitT o o m ()) -> Maybe o -> ConduitT o o m ()
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (() -> ConduitT o o m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()) (\o
x -> o -> ConduitT o o m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield o
x ConduitT o o m () -> ConduitT o o m () -> ConduitT o o m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> t -> ConduitT o o m ()
loop (t
count t -> t -> t
forall a. Num a => a -> a -> a
- t
1))
STREAMING(isolate, isolateC, isolateS, count)

-- | Keep only values in the stream passing a given predicate.
--
-- Subject to fusion
--
-- Since 0.3.0
filter, filterC :: Monad m => (a -> Bool) -> ConduitT a a m ()
filterC :: (a -> Bool) -> ConduitT a a m ()
filterC a -> Bool
f = (a -> ConduitT a a m ()) -> ConduitT a a m ()
forall (m :: * -> *) i o r.
Monad m =>
(i -> ConduitT i o m r) -> ConduitT i o m ()
awaitForever ((a -> ConduitT a a m ()) -> ConduitT a a m ())
-> (a -> ConduitT a a m ()) -> ConduitT a a m ()
forall a b. (a -> b) -> a -> b
$ \a
i -> Bool -> ConduitT a a m () -> ConduitT a a m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (a -> Bool
f a
i) (a -> ConduitT a a m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield a
i)
STREAMING(filter, filterC, filterS, f)

filterFuseRight
  :: Monad m
  => ConduitT i o m ()
  -> (o -> Bool)
  -> ConduitT i o m ()
filterFuseRight :: ConduitT i o m () -> (o -> Bool) -> ConduitT i o m ()
filterFuseRight (CI.ConduitT forall b. (() -> Pipe i i o () m b) -> Pipe i i o () m b
src) o -> Bool
f = (forall b. (() -> Pipe i i o () m b) -> Pipe i i o () m b)
-> ConduitT i o m ()
forall i o (m :: * -> *) r.
(forall b. (r -> Pipe i i o () m b) -> Pipe i i o () m b)
-> ConduitT i o m r
CI.ConduitT ((forall b. (() -> Pipe i i o () m b) -> Pipe i i o () m b)
 -> ConduitT i o m ())
-> (forall b. (() -> Pipe i i o () m b) -> Pipe i i o () m b)
-> ConduitT i o m ()
forall a b. (a -> b) -> a -> b
$ \() -> Pipe i i o () m b
rest -> let
    go :: Pipe i i o () m () -> Pipe i i o () m b
go (CI.Done ()) = () -> Pipe i i o () m b
rest ()
    go (CI.PipeM m (Pipe i i o () m ())
mp) = m (Pipe i i o () m b) -> Pipe i i o () m b
forall l i o u (m :: * -> *) r.
m (Pipe l i o u m r) -> Pipe l i o u m r
CI.PipeM ((Pipe i i o () m () -> Pipe i i o () m b)
-> m (Pipe i i o () m ()) -> m (Pipe i i o () m b)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM Pipe i i o () m () -> Pipe i i o () m b
go m (Pipe i i o () m ())
mp)
    go (CI.Leftover Pipe i i o () m ()
p i
i) = Pipe i i o () m b -> i -> Pipe i i o () m b
forall l i o u (m :: * -> *) r.
Pipe l i o u m r -> l -> Pipe l i o u m r
CI.Leftover (Pipe i i o () m () -> Pipe i i o () m b
go Pipe i i o () m ()
p) i
i
    go (CI.HaveOutput Pipe i i o () m ()
p o
o)
        | o -> Bool
f o
o = Pipe i i o () m b -> o -> Pipe i i o () m b
forall l i o u (m :: * -> *) r.
Pipe l i o u m r -> o -> Pipe l i o u m r
CI.HaveOutput (Pipe i i o () m () -> Pipe i i o () m b
go Pipe i i o () m ()
p) o
o
        | Bool
otherwise = Pipe i i o () m () -> Pipe i i o () m b
go Pipe i i o () m ()
p
    go (CI.NeedInput i -> Pipe i i o () m ()
p () -> Pipe i i o () m ()
c) = (i -> Pipe i i o () m b)
-> (() -> Pipe i i o () m b) -> Pipe i i o () m b
forall l i o u (m :: * -> *) r.
(i -> Pipe l i o u m r)
-> (u -> Pipe l i o u m r) -> Pipe l i o u m r
CI.NeedInput (Pipe i i o () m () -> Pipe i i o () m b
go (Pipe i i o () m () -> Pipe i i o () m b)
-> (i -> Pipe i i o () m ()) -> i -> Pipe i i o () m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. i -> Pipe i i o () m ()
p) (Pipe i i o () m () -> Pipe i i o () m b
go (Pipe i i o () m () -> Pipe i i o () m b)
-> (() -> Pipe i i o () m ()) -> () -> Pipe i i o () m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. () -> Pipe i i o () m ()
c)
    in Pipe i i o () m () -> Pipe i i o () m b
go ((() -> Pipe i i o () m ()) -> Pipe i i o () m ()
forall b. (() -> Pipe i i o () m b) -> Pipe i i o () m b
src () -> Pipe i i o () m ()
forall l i o u (m :: * -> *) r. r -> Pipe l i o u m r
CI.Done)
-- Intermediate finalizers are dropped, but this is acceptable: the next
-- yielded value would be demanded by downstream in any event, and that new
-- finalizer will always override the existing finalizer.
{-# RULES "conduit: source/filter fusion .|" forall f src. src .| filter f = filterFuseRight src f #-}
{-# INLINE filterFuseRight #-}

-- | Ignore the remainder of values in the source. Particularly useful when
-- combined with 'isolate'.
--
-- Subject to fusion
--
-- Since 0.3.0
sinkNull, sinkNullC :: Monad m => ConduitT i o m ()
sinkNullC :: ConduitT i o m ()
sinkNullC = (i -> ConduitT i o m ()) -> ConduitT i o m ()
forall (m :: * -> *) i o r.
Monad m =>
(i -> ConduitT i o m r) -> ConduitT i o m ()
awaitForever ((i -> ConduitT i o m ()) -> ConduitT i o m ())
-> (i -> ConduitT i o m ()) -> ConduitT i o m ()
forall a b. (a -> b) -> a -> b
$ \i
_ -> () -> ConduitT i o m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
{-# INLINE sinkNullC #-}
STREAMING0(sinkNull, sinkNullC, sinkNullS)

srcSinkNull :: Monad m => ConduitT () o m () -> m ()
srcSinkNull :: ConduitT () o m () -> m ()
srcSinkNull (CI.ConduitT forall b. (() -> Pipe () () o () m b) -> Pipe () () o () m b
src) =
    Pipe () () o () m () -> m ()
forall (m :: * -> *) i o. Monad m => Pipe () i o () m () -> m ()
go ((() -> Pipe () () o () m ()) -> Pipe () () o () m ()
forall b. (() -> Pipe () () o () m b) -> Pipe () () o () m b
src () -> Pipe () () o () m ()
forall l i o u (m :: * -> *) r. r -> Pipe l i o u m r
CI.Done)
  where
    go :: Pipe () i o () m () -> m ()
go (CI.Done ()) = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    go (CI.PipeM m (Pipe () i o () m ())
mp) = m (Pipe () i o () m ())
mp m (Pipe () i o () m ()) -> (Pipe () i o () m () -> m ()) -> m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Pipe () i o () m () -> m ()
go
    go (CI.Leftover Pipe () i o () m ()
p ()) = Pipe () i o () m () -> m ()
go Pipe () i o () m ()
p
    go (CI.HaveOutput Pipe () i o () m ()
p o
_) = Pipe () i o () m () -> m ()
go Pipe () i o () m ()
p
    go (CI.NeedInput i -> Pipe () i o () m ()
_ () -> Pipe () i o () m ()
c) = Pipe () i o () m () -> m ()
go (() -> Pipe () i o () m ()
c ())
{-# INLINE srcSinkNull #-}
{-# RULES "conduit: connect to sinkNull" forall src. runConduit (src .| sinkNull) = srcSinkNull src #-}

-- | A source that outputs no values. Note that this is just a type-restricted
-- synonym for 'mempty'.
--
-- Subject to fusion
--
-- Since 0.3.0
sourceNull, sourceNullC :: Monad m => ConduitT i o m ()
sourceNullC :: ConduitT i o m ()
sourceNullC = () -> ConduitT i o m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
{-# INLINE sourceNullC #-}
STREAMING0(sourceNull, sourceNullC, sourceNullS)

-- | Run a @Pipe@ repeatedly, and output its result value downstream. Stops
-- when no more input is available from upstream.
--
-- Since 0.5.0
sequence :: Monad m
         => ConduitT i o m o -- ^ @Pipe@ to run repeatedly
         -> ConduitT i o m ()
sequence :: ConduitT i o m o -> ConduitT i o m ()
sequence ConduitT i o m o
sink =
    ConduitT i o m ()
self
  where
    self :: ConduitT i o m ()
self = (i -> ConduitT i o m ()) -> ConduitT i o m ()
forall (m :: * -> *) i o r.
Monad m =>
(i -> ConduitT i o m r) -> ConduitT i o m ()
awaitForever ((i -> ConduitT i o m ()) -> ConduitT i o m ())
-> (i -> ConduitT i o m ()) -> ConduitT i o m ()
forall a b. (a -> b) -> a -> b
$ \i
i -> i -> ConduitT i o m ()
forall i o (m :: * -> *). i -> ConduitT i o m ()
leftover i
i ConduitT i o m () -> ConduitT i o m o -> ConduitT i o m o
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ConduitT i o m o
sink ConduitT i o m o -> (o -> ConduitT i o m ()) -> ConduitT i o m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= o -> ConduitT i o m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield