{-# LANGUAGE UndecidableInstances #-}

#include "inline.hs"

-- |
-- Module      : Streamly.Internal.Data.Stream.StreamK
-- Copyright   : (c) 2017 Composewell Technologies
--
-- License     : BSD3
-- Maintainer  : streamly@composewell.com
-- Stability   : experimental
-- Portability : GHC
--
--
-- Continuation passing style (CPS) stream implementation. The symbol 'K' below
-- denotes a function as well as a Kontinuation.
--
-- @
-- import qualified Streamly.Internal.Data.Stream.StreamK as K
-- @
--
module Streamly.Internal.Data.Stream.StreamK
    (
    -- * A class for streams
      IsStream (..)
    , adapt

    -- * The stream type
    , Stream(..)

    -- * Construction Primitives
    , mkStream
    , nil
    , nilM
    , cons
    , (.:)

    -- * Elimination Primitives
    , foldStream
    , foldStreamShared

    -- * Transformation Primitives
    , unShare

    -- * Deconstruction
    , uncons

    -- * Generation
    -- ** Unfolds
    , unfoldr
    , unfoldrM

    -- ** Specialized Generation
    , repeat
    , repeatM
    , replicate
    , replicateM
    , fromIndices
    , fromIndicesM
    , iterate
    , iterateM

    -- ** Conversions
    , fromPure
    , fromEffect
    , fromFoldable
    , fromList
    , fromStreamK

    -- * foldr/build
    , foldrS
    , foldrSM
    , buildS
    , buildM
    , augmentS
    , augmentSM

    -- * Elimination
    -- ** General Folds
    , foldr
    , foldr1
    , foldrM
    , foldrT

    , foldl'
    , foldlM'
    , foldlS
    , foldlT
    , foldlx'
    , foldlMx'
    , fold

    -- ** Specialized Folds
    , drain
    , null
    , head
    , tail
    , init
    , elem
    , notElem
    , all
    , any
    , last
    , minimum
    , minimumBy
    , maximum
    , maximumBy
    , findIndices
    , lookup
    , findM
    , find
    , (!!)

    -- ** Map and Fold
    , mapM_

    -- ** Conversions
    , toList
    , toStreamK
    , hoist

    -- * Transformation
    -- ** By folding (scans)
    , scanl'
    , scanlx'

    -- ** Filtering
    , filter
    , take
    , takeWhile
    , drop
    , dropWhile

    -- ** Mapping
    , map
    , mapM
    , mapMSerial
    , sequence

    -- ** Inserting
    , intersperseM
    , intersperse
    , insertBy

    -- ** Deleting
    , deleteBy

    -- ** Reordering
    , reverse

    -- ** Map and Filter
    , mapMaybe

    -- ** Zipping
    , zipWith
    , zipWithM

    -- ** Merging
    , mergeBy
    , mergeByM

    -- ** Nesting
    , concatMapBy
    , concatMap
    , bindWith
    , concatPairsWith
    , apWith
    , apSerial
    , apSerialDiscardFst
    , apSerialDiscardSnd

    -- ** Transformation comprehensions
    , the

    -- * Semigroup Style Composition
    , serial

    -- * Utilities
    , consMStream
    , withLocal
    , mfix

    -- * Deprecated
    , Streaming -- deprecated
    , once      -- deprecated
    )
where

import Control.Monad.Trans.Class (MonadTrans(lift))
import Control.Monad (void, join)
import Control.Monad.Reader.Class  (MonadReader(..))
import Data.Function (fix)
import Prelude
       hiding (foldl, foldr, last, map, mapM, mapM_, repeat, sequence,
               take, filter, all, any, takeWhile, drop, dropWhile, minimum,
               maximum, elem, notElem, null, head, tail, init, zipWith, lookup,
               foldr1, (!!), replicate, reverse, concatMap, iterate)
import qualified Prelude

import Streamly.Internal.Data.SVar
import Streamly.Internal.Data.Stream.StreamK.Type

import qualified Streamly.Internal.Data.Fold.Type as FL

-- $setup
-- >>> :m

-------------------------------------------------------------------------------
-- Deconstruction
-------------------------------------------------------------------------------

{-# INLINE uncons #-}
uncons :: (IsStream t, Monad m) => t m a -> m (Maybe (a, t m a))
uncons :: t m a -> m (Maybe (a, t m a))
uncons t m a
m =
    let stop :: m (Maybe a)
stop = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
        single :: a -> m (Maybe (a, t m a))
single a
a = Maybe (a, t m a) -> m (Maybe (a, t m a))
forall (m :: * -> *) a. Monad m => a -> m a
return ((a, t m a) -> Maybe (a, t m a)
forall a. a -> Maybe a
Just (a
a, t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
t m a
nil))
        yieldk :: a -> b -> m (Maybe (a, b))
yieldk a
a b
r = Maybe (a, b) -> m (Maybe (a, b))
forall (m :: * -> *) a. Monad m => a -> m a
return ((a, b) -> Maybe (a, b)
forall a. a -> Maybe a
Just (a
a, b
r))
    in State Stream m a
-> (a -> t m a -> m (Maybe (a, t m a)))
-> (a -> m (Maybe (a, t m a)))
-> m (Maybe (a, t m a))
-> t m a
-> m (Maybe (a, t m a))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> t m a -> m (Maybe (a, t m a))
forall (m :: * -> *) a b. Monad m => a -> b -> m (Maybe (a, b))
yieldk a -> m (Maybe (a, t m a))
forall (m :: * -> *) (t :: (* -> *) -> * -> *) a (m :: * -> *) a.
(Monad m, IsStream t) =>
a -> m (Maybe (a, t m a))
single m (Maybe (a, t m a))
forall a. m (Maybe a)
stop t m a
m

-------------------------------------------------------------------------------
-- Generation
-------------------------------------------------------------------------------

{-# INLINE unfoldr #-}
unfoldr :: IsStream t => (b -> Maybe (a, b)) -> b -> t m a
unfoldr :: (b -> Maybe (a, b)) -> b -> t m a
unfoldr b -> Maybe (a, b)
next b
s0 = (forall b. (a -> b -> b) -> b -> b) -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
(forall b. (a -> b -> b) -> b -> b) -> t m a
build ((forall b. (a -> b -> b) -> b -> b) -> t m a)
-> (forall b. (a -> b -> b) -> b -> b) -> t m a
forall a b. (a -> b) -> a -> b
$ \a -> b -> b
yld b
stp ->
    let go :: b -> b
go b
s =
            case b -> Maybe (a, b)
next b
s of
                Just (a
a, b
b) -> a -> b -> b
yld a
a (b -> b
go b
b)
                Maybe (a, b)
Nothing -> b
stp
    in b -> b
go b
s0

{-# INLINE unfoldrM #-}
unfoldrM :: (IsStream t, MonadAsync m) => (b -> m (Maybe (a, b))) -> b -> t m a
unfoldrM :: (b -> m (Maybe (a, b))) -> b -> t m a
unfoldrM b -> m (Maybe (a, b))
step = b -> t m a
forall (t :: (* -> *) -> * -> *). IsStream t => b -> t m a
go
    where
    go :: b -> t m a
go b
s = (forall r. (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, MonadAsync m) =>
(forall r. (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
sharedM ((forall r. (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
 -> t m a)
-> (forall r. (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall a b. (a -> b) -> a -> b
$ \a -> t m a -> m r
yld a -> m r
_ m r
stp -> do
                Maybe (a, b)
r <- b -> m (Maybe (a, b))
step b
s
                case Maybe (a, b)
r of
                    Just (a
a, b
b) -> a -> t m a -> m r
yld a
a (b -> t m a
go b
b)
                    Maybe (a, b)
Nothing -> m r
stp

{-
-- Generalization of concurrent streams/SVar via unfoldr.
--
-- Unfold a value into monadic actions and then run the resulting monadic
-- actions to generate a stream. Since the step of generating the monadic
-- action and running them are decoupled we can run the monadic actions
-- cooncurrently. For example, the seed could be a list of monadic actions or a
-- pure stream of monadic actions.
--
-- We can have different flavors of this depending on the stream type t. The
-- concurrent version could be async or ahead etc. Depending on how we queue
-- back the feedback portion b, it could be DFS or BFS style.
--
unfoldrA :: (IsStream t, MonadAsync m) => (b -> Maybe (m a, b)) -> b -> t m a
unfoldrA = undefined
-}

-------------------------------------------------------------------------------
-- Special generation
-------------------------------------------------------------------------------

-- | Same as fromEffect
--
-- @since 0.2.0
{-# DEPRECATED once "Please use fromEffect instead." #-}
{-# INLINE once #-}
once :: (Monad m, IsStream t) => m a -> t m a
once :: m a -> t m a
once = m a -> t m a
forall (m :: * -> *) (t :: (* -> *) -> * -> *) a.
(Monad m, IsStream t) =>
m a -> t m a
fromEffect

-- |
-- @
-- repeatM = fix . cons
-- repeatM = cycle1 . fromPure
-- @
--
-- Generate an infinite stream by repeating a monadic value.
--
-- /Pre-release/
repeatM :: (IsStream t, MonadAsync m) => m a -> t m a
repeatM :: m a -> t m a
repeatM = m a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, MonadIO m, MonadBaseControl IO m, MonadThrow m) =>
m a -> t m a
go
    where go :: m a -> t m a
go m a
m = m a
m m a -> t m a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, MonadAsync m) =>
m a -> t m a -> t m a
|: m a -> t m a
go m a
m

-- Generate an infinite stream by repeating a pure value.
--
-- /Pre-release/
{-# INLINE repeat #-}
repeat :: IsStream t => a -> t m a
repeat :: a -> t m a
repeat a
a = let x :: t m a
x = a -> t m a -> t m a
forall (t :: (* -> *) -> * -> *) a (m :: * -> *).
IsStream t =>
a -> t m a -> t m a
cons a
a t m a
x in t m a
forall (m :: * -> *). t m a
x

{-# INLINE replicateM #-}
replicateM :: (IsStream t, MonadAsync m) => Int -> m a -> t m a
replicateM :: Int -> m a -> t m a
replicateM Int
n m a
m = Int -> t m a
forall t (t :: (* -> *) -> * -> *).
(Ord t, Num t, IsStream t) =>
t -> t m a
go Int
n
    where
    go :: t -> t m a
go t
cnt = if t
cnt t -> t -> Bool
forall a. Ord a => a -> a -> Bool
<= t
0 then t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
t m a
nil else m a
m m a -> t m a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, MonadAsync m) =>
m a -> t m a -> t m a
|: t -> t m a
go (t
cnt t -> t -> t
forall a. Num a => a -> a -> a
- t
1)

{-# INLINE replicate #-}
replicate :: IsStream t => Int -> a -> t m a
replicate :: Int -> a -> t m a
replicate Int
n a
a = Int -> t m a
forall t (t :: (* -> *) -> * -> *) (m :: * -> *).
(Ord t, Num t, IsStream t) =>
t -> t m a
go Int
n
    where
    go :: t -> t m a
go t
cnt = if t
cnt t -> t -> Bool
forall a. Ord a => a -> a -> Bool
<= t
0 then t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
t m a
nil else a
a a -> t m a -> t m a
forall (t :: (* -> *) -> * -> *) a (m :: * -> *).
IsStream t =>
a -> t m a -> t m a
`cons` t -> t m a
go (t
cnt t -> t -> t
forall a. Num a => a -> a -> a
- t
1)

{-# INLINE fromIndicesM #-}
fromIndicesM :: (IsStream t, MonadAsync m) => (Int -> m a) -> t m a
fromIndicesM :: (Int -> m a) -> t m a
fromIndicesM Int -> m a
gen = Int -> t m a
forall (t :: (* -> *) -> * -> *). IsStream t => Int -> t m a
go Int
0
  where
    go :: Int -> t m a
go Int
i = (forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
(forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
mkStream ((forall r.
  State Stream m a
  -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
 -> t m a)
-> (forall r.
    State Stream m a
    -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> t m a -> m r
stp a -> m r
sng m r
yld -> do
        State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStreamShared State Stream m a
st a -> t m a -> m r
stp a -> m r
sng m r
yld (Int -> m a
gen Int
i m a -> t m a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, MonadAsync m) =>
m a -> t m a -> t m a
|: Int -> t m a
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1))

{-# INLINE fromIndices #-}
fromIndices :: IsStream t => (Int -> a) -> t m a
fromIndices :: (Int -> a) -> t m a
fromIndices Int -> a
gen = Int -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *).
IsStream t =>
Int -> t m a
go Int
0
  where
    go :: Int -> t m a
go Int
n = (Int -> a
gen Int
n) a -> t m a -> t m a
forall (t :: (* -> *) -> * -> *) a (m :: * -> *).
IsStream t =>
a -> t m a -> t m a
`cons` Int -> t m a
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)

{-# INLINE iterate #-}
iterate :: IsStream t => (a -> a) -> a -> t m a
iterate :: (a -> a) -> a -> t m a
iterate a -> a
step = a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *).
IsStream t =>
a -> t m a
go
    where
        go :: a -> t m a
go !a
s = a -> t m a -> t m a
forall (t :: (* -> *) -> * -> *) a (m :: * -> *).
IsStream t =>
a -> t m a -> t m a
cons a
s (a -> t m a
go (a -> a
step a
s))

{-# INLINE iterateM #-}
iterateM :: (IsStream t, MonadAsync m) => (a -> m a) -> m a -> t m a
iterateM :: (a -> m a) -> m a -> t m a
iterateM a -> m a
step = m a -> t m a
forall (t :: (* -> *) -> * -> *). IsStream t => m a -> t m a
go
    where
    go :: m a -> t m a
go m a
s = (forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
(forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
mkStream ((forall r.
  State Stream m a
  -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
 -> t m a)
-> (forall r.
    State Stream m a
    -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> t m a -> m r
stp a -> m r
sng m r
yld -> do
        !a
next <- m a
s
        State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStreamShared State Stream m a
st a -> t m a -> m r
stp a -> m r
sng m r
yld (a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
next m a -> t m a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, MonadAsync m) =>
m a -> t m a -> t m a
|: m a -> t m a
go (a -> m a
step a
next))

-------------------------------------------------------------------------------
-- Conversions
-------------------------------------------------------------------------------

-- |
-- @
-- fromFoldable = 'Prelude.foldr' 'cons' 'nil'
-- @
--
-- Construct a stream from a 'Foldable' containing pure values:
--
-- @since 0.2.0
{-# INLINE fromFoldable #-}
fromFoldable :: (IsStream t, Foldable f) => f a -> t m a
fromFoldable :: f a -> t m a
fromFoldable = (a -> t m a -> t m a) -> t m a -> f a -> t m a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Prelude.foldr a -> t m a -> t m a
forall (t :: (* -> *) -> * -> *) a (m :: * -> *).
IsStream t =>
a -> t m a -> t m a
cons t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
t m a
nil

{-# INLINE fromList #-}
fromList :: IsStream t => [a] -> t m a
fromList :: [a] -> t m a
fromList = [a] -> t m a
forall (t :: (* -> *) -> * -> *) (f :: * -> *) a (m :: * -> *).
(IsStream t, Foldable f) =>
f a -> t m a
fromFoldable

{-# INLINE fromStreamK #-}
fromStreamK :: IsStream t => Stream m a -> t m a
fromStreamK :: Stream m a -> t m a
fromStreamK = Stream m a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
Stream m a -> t m a
fromStream

-------------------------------------------------------------------------------
-- Elimination by Folding
-------------------------------------------------------------------------------

-- | Lazy right associative fold.
{-# INLINE foldr #-}
foldr :: (IsStream t, Monad m) => (a -> b -> b) -> b -> t m a -> m b
foldr :: (a -> b -> b) -> b -> t m a -> m b
foldr a -> b -> b
step b
acc = (a -> m b -> m b) -> m b -> t m a -> m b
forall (t :: (* -> *) -> * -> *) a (m :: * -> *) b.
IsStream t =>
(a -> m b -> m b) -> m b -> t m a -> m b
foldrM (\a
x m b
xs -> m b
xs m b -> (b -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \b
b -> b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> b
step a
x b
b)) (b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
acc)

-- | Right associative fold to an arbitrary transformer monad.
{-# INLINE foldrT #-}
foldrT :: (IsStream t, Monad m, Monad (s m), MonadTrans s)
    => (a -> s m b -> s m b) -> s m b -> t m a -> s m b
foldrT :: (a -> s m b -> s m b) -> s m b -> t m a -> s m b
foldrT a -> s m b -> s m b
step s m b
final t m a
m = t m a -> s m b
forall (t :: (* -> *) -> * -> *). IsStream t => t m a -> s m b
go t m a
m
  where
    go :: t m a -> s m b
go t m a
m1 = do
        Maybe (a, t m a)
res <- m (Maybe (a, t m a)) -> s m (Maybe (a, t m a))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Maybe (a, t m a)) -> s m (Maybe (a, t m a)))
-> m (Maybe (a, t m a)) -> s m (Maybe (a, t m a))
forall a b. (a -> b) -> a -> b
$ t m a -> m (Maybe (a, t m a))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> m (Maybe (a, t m a))
uncons t m a
m1
        case Maybe (a, t m a)
res of
            Just (a
h, t m a
t) -> a -> s m b -> s m b
step a
h (t m a -> s m b
go t m a
t)
            Maybe (a, t m a)
Nothing -> s m b
final

{-# INLINE foldr1 #-}
foldr1 :: (IsStream t, Monad m) => (a -> a -> a) -> t m a -> m (Maybe a)
foldr1 :: (a -> a -> a) -> t m a -> m (Maybe a)
foldr1 a -> a -> a
step t m a
m = do
    Maybe (a, t m a)
r <- t m a -> m (Maybe (a, t m a))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> m (Maybe (a, t m a))
uncons t m a
m
    case Maybe (a, t m a)
r of
        Maybe (a, t m a)
Nothing -> Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
        Just (a
h, t m a
t) -> (a -> Maybe a) -> m a -> m (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Maybe a
forall a. a -> Maybe a
Just (a -> t m a -> m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(IsStream t, Monad m) =>
a -> t m a -> m a
go a
h t m a
t)
    where
    go :: a -> t m a -> m a
go a
p t m a
m1 =
        let stp :: m a
stp = a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
p
            single :: a -> m a
single a
a = a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> m a) -> a -> m a
forall a b. (a -> b) -> a -> b
$ a -> a -> a
step a
a a
p
            yieldk :: a -> t m a -> m a
yieldk a
a t m a
r = (a -> a) -> m a -> m a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a -> a -> a
step a
p) (a -> t m a -> m a
go a
a t m a
r)
         in State Stream m a
-> (a -> t m a -> m a) -> (a -> m a) -> m a -> t m a -> m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> t m a -> m a
yieldk a -> m a
forall (m :: * -> *). Monad m => a -> m a
single m a
stp t m a
m1

-- XXX replace the recursive "go" with explicit continuations.
-- | Like 'foldx', but with a monadic step function.
{-# INLINABLE foldlMx' #-}
foldlMx' :: (IsStream t, Monad m)
    => (x -> a -> m x) -> m x -> (x -> m b) -> t m a -> m b
foldlMx' :: (x -> a -> m x) -> m x -> (x -> m b) -> t m a -> m b
foldlMx' x -> a -> m x
step m x
begin x -> m b
done t m a
m = m x -> t m a -> m b
forall (t :: (* -> *) -> * -> *). IsStream t => m x -> t m a -> m b
go m x
begin t m a
m
    where
    go :: m x -> t m a -> m b
go !m x
acc t m a
m1 =
        let stop :: m b
stop = m x
acc m x -> (x -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= x -> m b
done
            single :: a -> m b
single a
a = m x
acc m x -> (x -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \x
b -> x -> a -> m x
step x
b a
a m x -> (x -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= x -> m b
done
            yieldk :: a -> t m a -> m b
yieldk a
a t m a
r = m x
acc m x -> (x -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \x
b -> x -> a -> m x
step x
b a
a m x -> (x -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \x
x -> m x -> t m a -> m b
go (x -> m x
forall (m :: * -> *) a. Monad m => a -> m a
return x
x) t m a
r
         in State Stream m a
-> (a -> t m a -> m b) -> (a -> m b) -> m b -> t m a -> m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> t m a -> m b
yieldk a -> m b
single m b
stop t m a
m1

{-# INLINABLE fold #-}
fold :: (IsStream t, Monad m) => FL.Fold m a b -> t m a -> m b
fold :: Fold m a b -> t m a -> m b
fold (FL.Fold s -> a -> m (Step s b)
step m (Step s b)
begin s -> m b
done) t m a
m = do
    Step s b
res <- m (Step s b)
begin
    case Step s b
res of
        FL.Partial s
fs -> s -> t m a -> m b
forall (t :: (* -> *) -> * -> *). IsStream t => s -> t m a -> m b
go s
fs t m a
m
        FL.Done b
fb -> b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
fb

    where
    go :: s -> t m a -> m b
go !s
acc t m a
m1 =
        let stop :: m b
stop = s -> m b
done s
acc
            single :: a -> m b
single a
a = s -> a -> m (Step s b)
step s
acc a
a
              m (Step s b) -> (Step s b -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                        FL.Partial s
s -> s -> m b
done s
s
                        FL.Done b
b1 -> b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
b1
            yieldk :: a -> t m a -> m b
yieldk a
a t m a
r = s -> a -> m (Step s b)
step s
acc a
a
              m (Step s b) -> (Step s b -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                        FL.Partial s
s -> s -> t m a -> m b
go s
s t m a
r
                        FL.Done b
b1 -> b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
b1
         in State Stream m a
-> (a -> t m a -> m b) -> (a -> m b) -> m b -> t m a -> m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> t m a -> m b
yieldk a -> m b
single m b
stop t m a
m1

-- | Like 'foldl'' but with a monadic step function.
{-# INLINE foldlM' #-}
foldlM' :: (IsStream t, Monad m) => (b -> a -> m b) -> m b -> t m a -> m b
foldlM' :: (b -> a -> m b) -> m b -> t m a -> m b
foldlM' b -> a -> m b
step m b
begin = (b -> a -> m b) -> m b -> (b -> m b) -> t m a -> m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) x a b.
(IsStream t, Monad m) =>
(x -> a -> m x) -> m x -> (x -> m b) -> t m a -> m b
foldlMx' b -> a -> m b
step m b
begin b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return

-- | Lazy left fold to a stream.
{-# INLINE foldlS #-}
foldlS :: IsStream t => (t m b -> a -> t m b) -> t m b -> t m a -> t m b
foldlS :: (t m b -> a -> t m b) -> t m b -> t m a -> t m b
foldlS t m b -> a -> t m b
step t m b
begin t m a
m = t m b -> t m a -> t m b
forall (t :: (* -> *) -> * -> *).
IsStream t =>
t m b -> t m a -> t m b
go t m b
begin t m a
m
    where
    go :: t m b -> t m a -> t m b
go t m b
acc t m a
rest = (forall r.
 State Stream m b
 -> (b -> t m b -> m r) -> (b -> m r) -> m r -> m r)
-> t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
(forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
mkStream ((forall r.
  State Stream m b
  -> (b -> t m b -> m r) -> (b -> m r) -> m r -> m r)
 -> t m b)
-> (forall r.
    State Stream m b
    -> (b -> t m b -> m r) -> (b -> m r) -> m r -> m r)
-> t m b
forall a b. (a -> b) -> a -> b
$ \State Stream m b
st b -> t m b -> m r
yld b -> m r
sng m r
stp ->
        let run :: t m b -> m r
run t m b
x = State Stream m b
-> (b -> t m b -> m r) -> (b -> m r) -> m r -> t m b -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m b
st b -> t m b -> m r
yld b -> m r
sng m r
stp t m b
x
            stop :: m r
stop = t m b -> m r
run t m b
acc
            single :: a -> m r
single a
a = t m b -> m r
run (t m b -> m r) -> t m b -> m r
forall a b. (a -> b) -> a -> b
$ t m b -> a -> t m b
step t m b
acc a
a
            yieldk :: a -> t m a -> m r
yieldk a
a t m a
r = t m b -> m r
run (t m b -> m r) -> t m b -> m r
forall a b. (a -> b) -> a -> b
$ t m b -> t m a -> t m b
go (t m b -> a -> t m b
step t m b
acc a
a) t m a
r
         in State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream (State Stream m b -> State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m b
st) a -> t m a -> m r
yieldk a -> m r
single m r
stop t m a
rest

-- | Lazy left fold to an arbitrary transformer monad.
{-# INLINE foldlT #-}
foldlT :: (IsStream t, Monad m, Monad (s m), MonadTrans s)
    => (s m b -> a -> s m b) -> s m b -> t m a -> s m b
foldlT :: (s m b -> a -> s m b) -> s m b -> t m a -> s m b
foldlT s m b -> a -> s m b
step s m b
begin t m a
m = s m b -> t m a -> s m b
forall (t :: (* -> *) -> * -> *).
IsStream t =>
s m b -> t m a -> s m b
go s m b
begin t m a
m
  where
    go :: s m b -> t m a -> s m b
go s m b
acc t m a
m1 = do
        Maybe (a, t m a)
res <- m (Maybe (a, t m a)) -> s m (Maybe (a, t m a))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Maybe (a, t m a)) -> s m (Maybe (a, t m a)))
-> m (Maybe (a, t m a)) -> s m (Maybe (a, t m a))
forall a b. (a -> b) -> a -> b
$ t m a -> m (Maybe (a, t m a))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> m (Maybe (a, t m a))
uncons t m a
m1
        case Maybe (a, t m a)
res of
            Just (a
h, t m a
t) -> s m b -> t m a -> s m b
go (s m b -> a -> s m b
step s m b
acc a
h) t m a
t
            Maybe (a, t m a)
Nothing -> s m b
acc

------------------------------------------------------------------------------
-- Specialized folds
------------------------------------------------------------------------------

-- XXX use foldrM to implement folds where possible
-- XXX This (commented) definition of drain and mapM_ perform much better on
-- some benchmarks but worse on others. Need to investigate why, may there is
-- an optimization opportunity that we can exploit.
-- drain = foldrM (\_ xs -> return () >> xs) (return ())

-- |
-- > drain = foldl' (\_ _ -> ()) ()
-- > drain = mapM_ (\_ -> return ())
{-# INLINE drain #-}
drain :: (Monad m, IsStream t) => t m a -> m ()
drain :: t m a -> m ()
drain = (a -> m () -> m ()) -> m () -> t m a -> m ()
forall (t :: (* -> *) -> * -> *) a (m :: * -> *) b.
IsStream t =>
(a -> m b -> m b) -> m b -> t m a -> m b
foldrM (\a
_ m ()
xs -> m ()
xs) (() -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
{-
drain = go
    where
    go m1 =
        let stop = return ()
            single _ = return ()
            yieldk _ r = go r
         in foldStream defState yieldk single stop m1
-}

{-# INLINE null #-}
null :: (IsStream t, Monad m) => t m a -> m Bool
-- null = foldrM (\_ _ -> return True) (return False)
null :: t m a -> m Bool
null t m a
m =
    let stop :: m Bool
stop      = Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
        single :: p -> m Bool
single p
_  = Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
        yieldk :: p -> p -> m Bool
yieldk p
_ p
_ = Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
    in State Stream m a
-> (a -> t m a -> m Bool)
-> (a -> m Bool)
-> m Bool
-> t m a
-> m Bool
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> t m a -> m Bool
forall (m :: * -> *) p p. Monad m => p -> p -> m Bool
yieldk a -> m Bool
forall (m :: * -> *) p. Monad m => p -> m Bool
single m Bool
stop t m a
m

{-# INLINE head #-}
head :: (IsStream t, Monad m) => t m a -> m (Maybe a)
-- head = foldrM (\x _ -> return $ Just x) (return Nothing)
head :: t m a -> m (Maybe a)
head t m a
m =
    let stop :: m (Maybe a)
stop      = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
        single :: a -> m (Maybe a)
single a
a  = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
        yieldk :: a -> p -> m (Maybe a)
yieldk a
a p
_ = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
    in State Stream m a
-> (a -> t m a -> m (Maybe a))
-> (a -> m (Maybe a))
-> m (Maybe a)
-> t m a
-> m (Maybe a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> t m a -> m (Maybe a)
forall (m :: * -> *) a p. Monad m => a -> p -> m (Maybe a)
yieldk a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m (Maybe a)
single m (Maybe a)
forall a. m (Maybe a)
stop t m a
m

{-# INLINE tail #-}
tail :: (IsStream t, Monad m) => t m a -> m (Maybe (t m a))
tail :: t m a -> m (Maybe (t m a))
tail t m a
m =
    let stop :: m (Maybe a)
stop      = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
        single :: p -> m (Maybe (t m a))
single p
_  = Maybe (t m a) -> m (Maybe (t m a))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (t m a) -> m (Maybe (t m a)))
-> Maybe (t m a) -> m (Maybe (t m a))
forall a b. (a -> b) -> a -> b
$ t m a -> Maybe (t m a)
forall a. a -> Maybe a
Just t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
t m a
nil
        yieldk :: p -> a -> m (Maybe a)
yieldk p
_ a
r = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe a -> m (Maybe a)) -> Maybe a -> m (Maybe a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a
forall a. a -> Maybe a
Just a
r
    in State Stream m a
-> (a -> t m a -> m (Maybe (t m a)))
-> (a -> m (Maybe (t m a)))
-> m (Maybe (t m a))
-> t m a
-> m (Maybe (t m a))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> t m a -> m (Maybe (t m a))
forall (m :: * -> *) p a. Monad m => p -> a -> m (Maybe a)
yieldk a -> m (Maybe (t m a))
forall (m :: * -> *) (t :: (* -> *) -> * -> *) p (m :: * -> *) a.
(Monad m, IsStream t) =>
p -> m (Maybe (t m a))
single m (Maybe (t m a))
forall a. m (Maybe a)
stop t m a
m

{-# INLINE headPartial #-}
headPartial :: (IsStream t, Monad m) => t m a -> m a
headPartial :: t m a -> m a
headPartial = (a -> m a -> m a) -> m a -> t m a -> m a
forall (t :: (* -> *) -> * -> *) a (m :: * -> *) b.
IsStream t =>
(a -> m b -> m b) -> m b -> t m a -> m b
foldrM (\a
x m a
_ -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x) ([Char] -> m a
forall a. HasCallStack => [Char] -> a
error [Char]
"head of nil")

{-# INLINE tailPartial #-}
tailPartial :: IsStream t => t m a -> t m a
tailPartial :: t m a -> t m a
tailPartial t m a
m = (forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
(forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
mkStream ((forall r.
  State Stream m a
  -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
 -> t m a)
-> (forall r.
    State Stream m a
    -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> t m a -> m r
yld a -> m r
sng m r
stp ->
    let stop :: a
stop      = [Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"tail of nil"
        single :: p -> m r
single p
_  = m r
stp
        yieldk :: p -> t m a -> m r
yieldk p
_ t m a
r = State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
st a -> t m a -> m r
yld a -> m r
sng m r
stp t m a
r
    in State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
st a -> t m a -> m r
forall p. p -> t m a -> m r
yieldk a -> m r
forall p. p -> m r
single m r
forall a. a
stop t m a
m

-- | We can define cyclic structures using @let@:
--
-- >>> let (a, b) = ([1, b], head a) in (a, b)
-- ([1,1],1)
--
-- The function @fix@ defined as:
--
-- > fix f = let x = f x in x
--
-- ensures that the argument of a function and its output refer to the same
-- lazy value @x@ i.e.  the same location in memory.  Thus @x@ can be defined
-- in terms of itself, creating structures with cyclic references.
--
-- >>> import Data.Function (fix)
-- >>> f ~(a, b) = ([1, b], head a)
-- >>> fix f
-- ([1,1],1)
--
-- 'Control.Monad.mfix' is essentially the same as @fix@ but for monadic
-- values.
--
-- Using 'mfix' for streams we can construct a stream in which each element of
-- the stream is defined in a cyclic fashion. The argument of the function
-- being fixed represents the current element of the stream which is being
-- returned by the stream monad. Thus, we can use the argument to construct
-- itself.
--
-- In the following example, the argument @action@ of the function @f@
-- represents the tuple @(x,y)@ returned by it in a given iteration. We define
-- the first element of the tuple in terms of the second.
--
-- @
-- import Streamly.Internal.Data.Stream.IsStream as Stream
-- import System.IO.Unsafe (unsafeInterleaveIO)
--
-- main = do
--     Stream.mapM_ print $ Stream.mfix f
--
--     where
--
--     f action = do
--         let incr n act = fmap ((+n) . snd) $ unsafeInterleaveIO act
--         x <- Stream.fromListM [incr 1 action, incr 2 action]
--         y <- Stream.fromList [4,5]
--         return (x, y)
-- @
--
-- Note: you cannot achieve this by just changing the order of the monad
-- statements because that would change the order in which the stream elements
-- are generated.
--
-- Note that the function @f@ must be lazy in its argument, that's why we use
-- 'unsafeInterleaveIO' on @action@ because IO monad is strict.
--
-- /Pre-release/

mfix :: (IsStream t, Monad m) => (m a -> t m a) -> t m a
mfix :: (m a -> t m a) -> t m a
mfix m a -> t m a
f = (forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
(forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
mkStream ((forall r.
  State Stream m a
  -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
 -> t m a)
-> (forall r.
    State Stream m a
    -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> t m a -> m r
yld a -> m r
sng m r
stp ->
    let single :: a -> m r
single a
a  = State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
st a -> t m a -> m r
yld a -> m r
sng m r
stp (t m a -> m r) -> t m a -> m r
forall a b. (a -> b) -> a -> b
$ a
a a -> t m a -> t m a
forall (t :: (* -> *) -> * -> *) a (m :: * -> *).
IsStream t =>
a -> t m a -> t m a
`cons` t m a
ys
        yieldk :: a -> p -> m r
yieldk a
a p
_ = State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
st a -> t m a -> m r
yld a -> m r
sng m r
stp (t m a -> m r) -> t m a -> m r
forall a b. (a -> b) -> a -> b
$ a
a a -> t m a -> t m a
forall (t :: (* -> *) -> * -> *) a (m :: * -> *).
IsStream t =>
a -> t m a -> t m a
`cons` t m a
ys
    in State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
st a -> t m a -> m r
forall p. a -> p -> m r
yieldk a -> m r
single m r
stp t m a
xs

    where

    -- fix the head element of the stream
    xs :: t m a
xs = (t m a -> t m a) -> t m a
forall a. (a -> a) -> a
fix  (m a -> t m a
f (m a -> t m a) -> (t m a -> m a) -> t m a -> t m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t m a -> m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> m a
headPartial)

    -- now fix the tail recursively
    ys :: t m a
ys = (m a -> t m a) -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
(m a -> t m a) -> t m a
mfix (t m a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
t m a -> t m a
tailPartial (t m a -> t m a) -> (m a -> t m a) -> m a -> t m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m a -> t m a
f)

{-# INLINE init #-}
init :: (IsStream t, Monad m) => t m a -> m (Maybe (t m a))
init :: t m a -> m (Maybe (t m a))
init t m a
m = t m a -> m (Maybe (t m a))
forall (m :: * -> *) (t :: (* -> *) -> * -> *)
       (t :: (* -> *) -> * -> *) p.
(Monad m, IsStream t, IsStream t) =>
t m p -> m (Maybe (t m p))
go1 t m a
m
    where
    go1 :: t m p -> m (Maybe (t m p))
go1 t m p
m1 = do
        Maybe (p, t m p)
r <- t m p -> m (Maybe (p, t m p))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> m (Maybe (a, t m a))
uncons t m p
m1
        case Maybe (p, t m p)
r of
            Maybe (p, t m p)
Nothing -> Maybe (t m p) -> m (Maybe (t m p))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (t m p)
forall a. Maybe a
Nothing
            Just (p
h, t m p
t) -> Maybe (t m p) -> m (Maybe (t m p))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (t m p) -> m (Maybe (t m p)))
-> (t m p -> Maybe (t m p)) -> t m p -> m (Maybe (t m p))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t m p -> Maybe (t m p)
forall a. a -> Maybe a
Just (t m p -> m (Maybe (t m p))) -> t m p -> m (Maybe (t m p))
forall a b. (a -> b) -> a -> b
$ p -> t m p -> t m p
forall (t :: (* -> *) -> * -> *) (t :: (* -> *) -> * -> *) p
       (m :: * -> *).
(IsStream t, IsStream t) =>
p -> t m p -> t m p
go p
h t m p
t
    go :: p -> t m p -> t m p
go p
p t m p
m1 = (forall r.
 State Stream m p
 -> (p -> t m p -> m r) -> (p -> m r) -> m r -> m r)
-> t m p
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
(forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
mkStream ((forall r.
  State Stream m p
  -> (p -> t m p -> m r) -> (p -> m r) -> m r -> m r)
 -> t m p)
-> (forall r.
    State Stream m p
    -> (p -> t m p -> m r) -> (p -> m r) -> m r -> m r)
-> t m p
forall a b. (a -> b) -> a -> b
$ \State Stream m p
_ p -> t m p -> m r
yld p -> m r
sng m r
stp ->
        let single :: p -> m r
single p
_ = p -> m r
sng p
p
            yieldk :: p -> t m p -> m r
yieldk p
a t m p
x = p -> t m p -> m r
yld p
p (t m p -> m r) -> t m p -> m r
forall a b. (a -> b) -> a -> b
$ p -> t m p -> t m p
go p
a t m p
x
         in State Stream m p
-> (p -> t m p -> m r) -> (p -> m r) -> m r -> t m p -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m p
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState p -> t m p -> m r
yieldk p -> m r
forall p. p -> m r
single m r
stp t m p
m1

{-# INLINE elem #-}
elem :: (IsStream t, Monad m, Eq a) => a -> t m a -> m Bool
elem :: a -> t m a -> m Bool
elem a
e t m a
m = t m a -> m Bool
forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(IsStream t, Monad m) =>
t m a -> m Bool
go t m a
m
    where
    go :: t m a -> m Bool
go t m a
m1 =
        let stop :: m Bool
stop      = Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
            single :: a -> m Bool
single a
a  = Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
e)
            yieldk :: a -> t m a -> m Bool
yieldk a
a t m a
r = if a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
e then Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True else t m a -> m Bool
go t m a
r
        in State Stream m a
-> (a -> t m a -> m Bool)
-> (a -> m Bool)
-> m Bool
-> t m a
-> m Bool
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> t m a -> m Bool
yieldk a -> m Bool
forall (m :: * -> *). Monad m => a -> m Bool
single m Bool
stop t m a
m1

{-# INLINE notElem #-}
notElem :: (IsStream t, Monad m, Eq a) => a -> t m a -> m Bool
notElem :: a -> t m a -> m Bool
notElem a
e t m a
m = t m a -> m Bool
forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(IsStream t, Monad m) =>
t m a -> m Bool
go t m a
m
    where
    go :: t m a -> m Bool
go t m a
m1 =
        let stop :: m Bool
stop      = Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
            single :: a -> m Bool
single a
a  = Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/= a
e)
            yieldk :: a -> t m a -> m Bool
yieldk a
a t m a
r = if a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
e then Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False else t m a -> m Bool
go t m a
r
        in State Stream m a
-> (a -> t m a -> m Bool)
-> (a -> m Bool)
-> m Bool
-> t m a
-> m Bool
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> t m a -> m Bool
yieldk a -> m Bool
forall (m :: * -> *). Monad m => a -> m Bool
single m Bool
stop t m a
m1

{-# INLINABLE all #-}
all :: (IsStream t, Monad m) => (a -> Bool) -> t m a -> m Bool
all :: (a -> Bool) -> t m a -> m Bool
all a -> Bool
p t m a
m = t m a -> m Bool
forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(IsStream t, Monad m) =>
t m a -> m Bool
go t m a
m
    where
    go :: t m a -> m Bool
go t m a
m1 =
        let single :: a -> m Bool
single a
a   | a -> Bool
p a
a       = Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
                       | Bool
otherwise = Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
            yieldk :: a -> t m a -> m Bool
yieldk a
a t m a
r | a -> Bool
p a
a       = t m a -> m Bool
go t m a
r
                       | Bool
otherwise = Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
         in State Stream m a
-> (a -> t m a -> m Bool)
-> (a -> m Bool)
-> m Bool
-> t m a
-> m Bool
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> t m a -> m Bool
yieldk a -> m Bool
forall (m :: * -> *). Monad m => a -> m Bool
single (Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True) t m a
m1

{-# INLINABLE any #-}
any :: (IsStream t, Monad m) => (a -> Bool) -> t m a -> m Bool
any :: (a -> Bool) -> t m a -> m Bool
any a -> Bool
p t m a
m = t m a -> m Bool
forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(IsStream t, Monad m) =>
t m a -> m Bool
go t m a
m
    where
    go :: t m a -> m Bool
go t m a
m1 =
        let single :: a -> m Bool
single a
a   | a -> Bool
p a
a       = Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
                       | Bool
otherwise = Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
            yieldk :: a -> t m a -> m Bool
yieldk a
a t m a
r | a -> Bool
p a
a       = Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
                       | Bool
otherwise = t m a -> m Bool
go t m a
r
         in State Stream m a
-> (a -> t m a -> m Bool)
-> (a -> m Bool)
-> m Bool
-> t m a
-> m Bool
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> t m a -> m Bool
yieldk a -> m Bool
forall (m :: * -> *). Monad m => a -> m Bool
single (Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False) t m a
m1

-- | Extract the last element of the stream, if any.
{-# INLINE last #-}
last :: (IsStream t, Monad m) => t m a -> m (Maybe a)
last :: t m a -> m (Maybe a)
last = (Maybe a -> a -> Maybe a)
-> Maybe a -> (Maybe a -> Maybe a) -> t m a -> m (Maybe a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b x.
(IsStream t, Monad m) =>
(x -> a -> x) -> x -> (x -> b) -> t m a -> m b
foldlx' (\Maybe a
_ a
y -> a -> Maybe a
forall a. a -> Maybe a
Just a
y) Maybe a
forall a. Maybe a
Nothing Maybe a -> Maybe a
forall a. a -> a
id

{-# INLINE minimum #-}
minimum :: (IsStream t, Monad m, Ord a) => t m a -> m (Maybe a)
minimum :: t m a -> m (Maybe a)
minimum t m a
m = Maybe a -> t m a -> m (Maybe a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Ord a, Monad m) =>
Maybe a -> t m a -> m (Maybe a)
go Maybe a
forall a. Maybe a
Nothing t m a
m
    where
    go :: Maybe a -> t m a -> m (Maybe a)
go Maybe a
Nothing t m a
m1 =
        let stop :: m (Maybe a)
stop      = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
            single :: a -> m (Maybe a)
single a
a  = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
            yieldk :: a -> t m a -> m (Maybe a)
yieldk a
a t m a
r = Maybe a -> t m a -> m (Maybe a)
go (a -> Maybe a
forall a. a -> Maybe a
Just a
a) t m a
r
        in State Stream m a
-> (a -> t m a -> m (Maybe a))
-> (a -> m (Maybe a))
-> m (Maybe a)
-> t m a
-> m (Maybe a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> t m a -> m (Maybe a)
yieldk a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m (Maybe a)
single m (Maybe a)
forall a. m (Maybe a)
stop t m a
m1

    go (Just a
res) t m a
m1 =
        let stop :: m (Maybe a)
stop      = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
res)
            single :: a -> m (Maybe a)
single a
a  =
                if a
res a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
a
                then Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
res)
                else Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
            yieldk :: a -> t m a -> m (Maybe a)
yieldk a
a t m a
r =
                if a
res a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
a
                then Maybe a -> t m a -> m (Maybe a)
go (a -> Maybe a
forall a. a -> Maybe a
Just a
res) t m a
r
                else Maybe a -> t m a -> m (Maybe a)
go (a -> Maybe a
forall a. a -> Maybe a
Just a
a) t m a
r
        in State Stream m a
-> (a -> t m a -> m (Maybe a))
-> (a -> m (Maybe a))
-> m (Maybe a)
-> t m a
-> m (Maybe a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> t m a -> m (Maybe a)
yieldk a -> m (Maybe a)
forall (m :: * -> *). Monad m => a -> m (Maybe a)
single m (Maybe a)
stop t m a
m1

{-# INLINE minimumBy #-}
minimumBy
    :: (IsStream t, Monad m)
    => (a -> a -> Ordering) -> t m a -> m (Maybe a)
minimumBy :: (a -> a -> Ordering) -> t m a -> m (Maybe a)
minimumBy a -> a -> Ordering
cmp t m a
m = Maybe a -> t m a -> m (Maybe a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(IsStream t, Monad m) =>
Maybe a -> t m a -> m (Maybe a)
go Maybe a
forall a. Maybe a
Nothing t m a
m
    where
    go :: Maybe a -> t m a -> m (Maybe a)
go Maybe a
Nothing t m a
m1 =
        let stop :: m (Maybe a)
stop      = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
            single :: a -> m (Maybe a)
single a
a  = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
            yieldk :: a -> t m a -> m (Maybe a)
yieldk a
a t m a
r = Maybe a -> t m a -> m (Maybe a)
go (a -> Maybe a
forall a. a -> Maybe a
Just a
a) t m a
r
        in State Stream m a
-> (a -> t m a -> m (Maybe a))
-> (a -> m (Maybe a))
-> m (Maybe a)
-> t m a
-> m (Maybe a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> t m a -> m (Maybe a)
yieldk a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m (Maybe a)
single m (Maybe a)
forall a. m (Maybe a)
stop t m a
m1

    go (Just a
res) t m a
m1 =
        let stop :: m (Maybe a)
stop      = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
res)
            single :: a -> m (Maybe a)
single a
a  = case a -> a -> Ordering
cmp a
res a
a of
                Ordering
GT -> Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
                Ordering
_  -> Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
res)
            yieldk :: a -> t m a -> m (Maybe a)
yieldk a
a t m a
r = case a -> a -> Ordering
cmp a
res a
a of
                Ordering
GT -> Maybe a -> t m a -> m (Maybe a)
go (a -> Maybe a
forall a. a -> Maybe a
Just a
a) t m a
r
                Ordering
_  -> Maybe a -> t m a -> m (Maybe a)
go (a -> Maybe a
forall a. a -> Maybe a
Just a
res) t m a
r
        in State Stream m a
-> (a -> t m a -> m (Maybe a))
-> (a -> m (Maybe a))
-> m (Maybe a)
-> t m a
-> m (Maybe a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> t m a -> m (Maybe a)
yieldk a -> m (Maybe a)
forall (m :: * -> *). Monad m => a -> m (Maybe a)
single m (Maybe a)
stop t m a
m1

{-# INLINE maximum #-}
maximum :: (IsStream t, Monad m, Ord a) => t m a -> m (Maybe a)
maximum :: t m a -> m (Maybe a)
maximum t m a
m = Maybe a -> t m a -> m (Maybe a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Ord a, Monad m) =>
Maybe a -> t m a -> m (Maybe a)
go Maybe a
forall a. Maybe a
Nothing t m a
m
    where
    go :: Maybe a -> t m a -> m (Maybe a)
go Maybe a
Nothing t m a
m1 =
        let stop :: m (Maybe a)
stop      = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
            single :: a -> m (Maybe a)
single a
a  = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
            yieldk :: a -> t m a -> m (Maybe a)
yieldk a
a t m a
r = Maybe a -> t m a -> m (Maybe a)
go (a -> Maybe a
forall a. a -> Maybe a
Just a
a) t m a
r
        in State Stream m a
-> (a -> t m a -> m (Maybe a))
-> (a -> m (Maybe a))
-> m (Maybe a)
-> t m a
-> m (Maybe a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> t m a -> m (Maybe a)
yieldk a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m (Maybe a)
single m (Maybe a)
forall a. m (Maybe a)
stop t m a
m1

    go (Just a
res) t m a
m1 =
        let stop :: m (Maybe a)
stop      = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
res)
            single :: a -> m (Maybe a)
single a
a  =
                if a
res a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
a
                then Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
                else Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
res)
            yieldk :: a -> t m a -> m (Maybe a)
yieldk a
a t m a
r =
                if a
res a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
a
                then Maybe a -> t m a -> m (Maybe a)
go (a -> Maybe a
forall a. a -> Maybe a
Just a
a) t m a
r
                else Maybe a -> t m a -> m (Maybe a)
go (a -> Maybe a
forall a. a -> Maybe a
Just a
res) t m a
r
        in State Stream m a
-> (a -> t m a -> m (Maybe a))
-> (a -> m (Maybe a))
-> m (Maybe a)
-> t m a
-> m (Maybe a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> t m a -> m (Maybe a)
yieldk a -> m (Maybe a)
forall (m :: * -> *). Monad m => a -> m (Maybe a)
single m (Maybe a)
stop t m a
m1

{-# INLINE maximumBy #-}
maximumBy :: (IsStream t, Monad m) => (a -> a -> Ordering) -> t m a -> m (Maybe a)
maximumBy :: (a -> a -> Ordering) -> t m a -> m (Maybe a)
maximumBy a -> a -> Ordering
cmp t m a
m = Maybe a -> t m a -> m (Maybe a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(IsStream t, Monad m) =>
Maybe a -> t m a -> m (Maybe a)
go Maybe a
forall a. Maybe a
Nothing t m a
m
    where
    go :: Maybe a -> t m a -> m (Maybe a)
go Maybe a
Nothing t m a
m1 =
        let stop :: m (Maybe a)
stop      = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
            single :: a -> m (Maybe a)
single a
a  = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
            yieldk :: a -> t m a -> m (Maybe a)
yieldk a
a t m a
r = Maybe a -> t m a -> m (Maybe a)
go (a -> Maybe a
forall a. a -> Maybe a
Just a
a) t m a
r
        in State Stream m a
-> (a -> t m a -> m (Maybe a))
-> (a -> m (Maybe a))
-> m (Maybe a)
-> t m a
-> m (Maybe a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> t m a -> m (Maybe a)
yieldk a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m (Maybe a)
single m (Maybe a)
forall a. m (Maybe a)
stop t m a
m1

    go (Just a
res) t m a
m1 =
        let stop :: m (Maybe a)
stop      = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
res)
            single :: a -> m (Maybe a)
single a
a  = case a -> a -> Ordering
cmp a
res a
a of
                Ordering
GT -> Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
res)
                Ordering
_  -> Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
            yieldk :: a -> t m a -> m (Maybe a)
yieldk a
a t m a
r = case a -> a -> Ordering
cmp a
res a
a of
                Ordering
GT -> Maybe a -> t m a -> m (Maybe a)
go (a -> Maybe a
forall a. a -> Maybe a
Just a
res) t m a
r
                Ordering
_  -> Maybe a -> t m a -> m (Maybe a)
go (a -> Maybe a
forall a. a -> Maybe a
Just a
a) t m a
r
        in State Stream m a
-> (a -> t m a -> m (Maybe a))
-> (a -> m (Maybe a))
-> m (Maybe a)
-> t m a
-> m (Maybe a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> t m a -> m (Maybe a)
yieldk a -> m (Maybe a)
forall (m :: * -> *). Monad m => a -> m (Maybe a)
single m (Maybe a)
stop t m a
m1

{-# INLINE (!!) #-}
(!!) :: (IsStream t, Monad m) => t m a -> Int -> m (Maybe a)
t m a
m !! :: t m a -> Int -> m (Maybe a)
!! Int
i = Int -> t m a -> m (Maybe a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a a.
(IsStream t, Ord a, Monad m, Num a) =>
a -> t m a -> m (Maybe a)
go Int
i t m a
m
    where
    go :: a -> t m a -> m (Maybe a)
go a
n t m a
m1 =
      let single :: a -> m (Maybe a)
single a
a | a
n a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0 = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe a -> m (Maybe a)) -> Maybe a -> m (Maybe a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a
forall a. a -> Maybe a
Just a
a
                   | Bool
otherwise = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
          yieldk :: a -> t m a -> m (Maybe a)
yieldk a
a t m a
x | a
n a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0 = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
                     | a
n a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0 = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe a -> m (Maybe a)) -> Maybe a -> m (Maybe a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a
forall a. a -> Maybe a
Just a
a
                     | Bool
otherwise = a -> t m a -> m (Maybe a)
go (a
n a -> a -> a
forall a. Num a => a -> a -> a
- a
1) t m a
x
      in State Stream m a
-> (a -> t m a -> m (Maybe a))
-> (a -> m (Maybe a))
-> m (Maybe a)
-> t m a
-> m (Maybe a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> t m a -> m (Maybe a)
yieldk a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m (Maybe a)
single (Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing) t m a
m1

{-# INLINE lookup #-}
lookup :: (IsStream t, Monad m, Eq a) => a -> t m (a, b) -> m (Maybe b)
lookup :: a -> t m (a, b) -> m (Maybe b)
lookup a
e t m (a, b)
m = t m (a, b) -> m (Maybe b)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m (a, a) -> m (Maybe a)
go t m (a, b)
m
    where
    go :: t m (a, a) -> m (Maybe a)
go t m (a, a)
m1 =
        let single :: (a, a) -> m (Maybe a)
single (a
a, a
b) | a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
e = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe a -> m (Maybe a)) -> Maybe a -> m (Maybe a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a
forall a. a -> Maybe a
Just a
b
                          | Bool
otherwise = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
            yieldk :: (a, a) -> t m (a, a) -> m (Maybe a)
yieldk (a
a, a
b) t m (a, a)
x | a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
e = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe a -> m (Maybe a)) -> Maybe a -> m (Maybe a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a
forall a. a -> Maybe a
Just a
b
                            | Bool
otherwise = t m (a, a) -> m (Maybe a)
go t m (a, a)
x
        in State Stream m (a, a)
-> ((a, a) -> t m (a, a) -> m (Maybe a))
-> ((a, a) -> m (Maybe a))
-> m (Maybe a)
-> t m (a, a)
-> m (Maybe a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m (a, a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState (a, a) -> t m (a, a) -> m (Maybe a)
yieldk (a, a) -> m (Maybe a)
forall (m :: * -> *) a. Monad m => (a, a) -> m (Maybe a)
single (Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing) t m (a, a)
m1

{-# INLINE findM #-}
findM :: (IsStream t, Monad m) => (a -> m Bool) -> t m a -> m (Maybe a)
findM :: (a -> m Bool) -> t m a -> m (Maybe a)
findM a -> m Bool
p t m a
m = t m a -> m (Maybe a)
forall (t :: (* -> *) -> * -> *).
IsStream t =>
t m a -> m (Maybe a)
go t m a
m
    where
    go :: t m a -> m (Maybe a)
go t m a
m1 =
        let single :: a -> m (Maybe a)
single a
a = do
                Bool
b <- a -> m Bool
p a
a
                if Bool
b then Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe a -> m (Maybe a)) -> Maybe a -> m (Maybe a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a
forall a. a -> Maybe a
Just a
a else Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
            yieldk :: a -> t m a -> m (Maybe a)
yieldk a
a t m a
x = do
                Bool
b <- a -> m Bool
p a
a
                if Bool
b then Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe a -> m (Maybe a)) -> Maybe a -> m (Maybe a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a
forall a. a -> Maybe a
Just a
a else t m a -> m (Maybe a)
go t m a
x
        in State Stream m a
-> (a -> t m a -> m (Maybe a))
-> (a -> m (Maybe a))
-> m (Maybe a)
-> t m a
-> m (Maybe a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> t m a -> m (Maybe a)
yieldk a -> m (Maybe a)
single (Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing) t m a
m1

{-# INLINE find #-}
find :: (IsStream t, Monad m) => (a -> Bool) -> t m a -> m (Maybe a)
find :: (a -> Bool) -> t m a -> m (Maybe a)
find a -> Bool
p = (a -> m Bool) -> t m a -> m (Maybe a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
(a -> m Bool) -> t m a -> m (Maybe a)
findM (Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> m Bool) -> (a -> Bool) -> a -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
p)

{-# INLINE findIndices #-}
findIndices :: IsStream t => (a -> Bool) -> t m a -> t m Int
findIndices :: (a -> Bool) -> t m a -> t m Int
findIndices a -> Bool
p = Int -> t m a -> t m Int
forall (t :: (* -> *) -> * -> *) (t :: (* -> *) -> * -> *) a
       (m :: * -> *).
(IsStream t, IsStream t, Num a) =>
a -> t m a -> t m a
go Int
0
    where
    go :: a -> t m a -> t m a
go a
offset t m a
m1 = (forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
(forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
mkStream ((forall r.
  State Stream m a
  -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
 -> t m a)
-> (forall r.
    State Stream m a
    -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> t m a -> m r
yld a -> m r
sng m r
stp ->
        let single :: a -> m r
single a
a | a -> Bool
p a
a = a -> m r
sng a
offset
                     | Bool
otherwise = m r
stp
            yieldk :: a -> t m a -> m r
yieldk a
a t m a
x | a -> Bool
p a
a = a -> t m a -> m r
yld a
offset (t m a -> m r) -> t m a -> m r
forall a b. (a -> b) -> a -> b
$ a -> t m a -> t m a
go (a
offset a -> a -> a
forall a. Num a => a -> a -> a
+ a
1) t m a
x
                       | Bool
otherwise = State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream (State Stream m a -> State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m a
st) a -> t m a -> m r
yld a -> m r
sng m r
stp (t m a -> m r) -> t m a -> m r
forall a b. (a -> b) -> a -> b
$
                            a -> t m a -> t m a
go (a
offset a -> a -> a
forall a. Num a => a -> a -> a
+ a
1) t m a
x
        in State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream (State Stream m a -> State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m a
st) a -> t m a -> m r
yieldk a -> m r
single m r
stp t m a
m1

------------------------------------------------------------------------------
-- Map and Fold
------------------------------------------------------------------------------

-- | Apply a monadic action to each element of the stream and discard the
-- output of the action.
{-# INLINE mapM_ #-}
mapM_ :: (IsStream t, Monad m) => (a -> m b) -> t m a -> m ()
mapM_ :: (a -> m b) -> t m a -> m ()
mapM_ a -> m b
f t m a
m = t m a -> m ()
forall (t :: (* -> *) -> * -> *). IsStream t => t m a -> m ()
go t m a
m
    where
    go :: t m a -> m ()
go t m a
m1 =
        let stop :: m ()
stop = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
            single :: a -> m ()
single a
a = m b -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (a -> m b
f a
a)
            yieldk :: a -> t m a -> m ()
yieldk a
a t m a
r = a -> m b
f a
a m b -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> t m a -> m ()
go t m a
r
         in State Stream m a
-> (a -> t m a -> m ()) -> (a -> m ()) -> m () -> t m a -> m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> t m a -> m ()
yieldk a -> m ()
single m ()
stop t m a
m1

------------------------------------------------------------------------------
-- Converting folds
------------------------------------------------------------------------------

{-# INLINABLE toList #-}
toList :: (IsStream t, Monad m) => t m a -> m [a]
toList :: t m a -> m [a]
toList = (a -> [a] -> [a]) -> [a] -> t m a -> m [a]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(IsStream t, Monad m) =>
(a -> b -> b) -> b -> t m a -> m b
foldr (:) []

{-# INLINE toStreamK #-}
toStreamK :: Stream m a -> Stream m a
toStreamK :: Stream m a -> Stream m a
toStreamK = Stream m a -> Stream m a
forall a. a -> a
id

-- Based on suggestions by David Feuer and Pranay Sashank
{-# INLINE hoist #-}
hoist :: (IsStream t, Monad m, Monad n)
    => (forall x. m x -> n x) -> t m a -> t n a
hoist :: (forall x. m x -> n x) -> t m a -> t n a
hoist forall x. m x -> n x
f t m a
str =
    (forall r.
 State Stream n a
 -> (a -> t n a -> n r) -> (a -> n r) -> n r -> n r)
-> t n a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
(forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
mkStream ((forall r.
  State Stream n a
  -> (a -> t n a -> n r) -> (a -> n r) -> n r -> n r)
 -> t n a)
-> (forall r.
    State Stream n a
    -> (a -> t n a -> n r) -> (a -> n r) -> n r -> n r)
-> t n a
forall a b. (a -> b) -> a -> b
$ \State Stream n a
st a -> t n a -> n r
yld a -> n r
sng n r
stp ->
            let single :: a -> m (n r)
single = n r -> m (n r)
forall (m :: * -> *) a. Monad m => a -> m a
return (n r -> m (n r)) -> (a -> n r) -> a -> m (n r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> n r
sng
                yieldk :: a -> t m a -> m (n r)
yieldk a
a t m a
s = n r -> m (n r)
forall (m :: * -> *) a. Monad m => a -> m a
return (n r -> m (n r)) -> n r -> m (n r)
forall a b. (a -> b) -> a -> b
$ a -> t n a -> n r
yld a
a ((forall x. m x -> n x) -> t m a -> t n a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) (n :: * -> *) a.
(IsStream t, Monad m, Monad n) =>
(forall x. m x -> n x) -> t m a -> t n a
hoist forall x. m x -> n x
f t m a
s)
                stop :: m (n r)
stop = n r -> m (n r)
forall (m :: * -> *) a. Monad m => a -> m a
return n r
stp
                state :: State Stream n b
state = State Stream n a -> State Stream n b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream n a
st
             in n (n r) -> n r
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (n (n r) -> n r) -> (m (n r) -> n (n r)) -> m (n r) -> n r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m (n r) -> n (n r)
forall x. m x -> n x
f (m (n r) -> n r) -> m (n r) -> n r
forall a b. (a -> b) -> a -> b
$ State Stream m a
-> (a -> t m a -> m (n r))
-> (a -> m (n r))
-> m (n r)
-> t m a
-> m (n r)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStreamShared State Stream m a
forall (n :: * -> *) b. State Stream n b
state a -> t m a -> m (n r)
forall (m :: * -> *). Monad m => a -> t m a -> m (n r)
yieldk a -> m (n r)
single m (n r)
stop t m a
str

-------------------------------------------------------------------------------
-- Transformation by folding (Scans)
-------------------------------------------------------------------------------

{-# INLINE scanlx' #-}
scanlx' :: IsStream t => (x -> a -> x) -> x -> (x -> b) -> t m a -> t m b
scanlx' :: (x -> a -> x) -> x -> (x -> b) -> t m a -> t m b
scanlx' x -> a -> x
step x
begin x -> b
done t m a
m =
    b -> t m b -> t m b
forall (t :: (* -> *) -> * -> *) a (m :: * -> *).
IsStream t =>
a -> t m a -> t m a
cons (x -> b
done x
begin) (t m b -> t m b) -> t m b -> t m b
forall a b. (a -> b) -> a -> b
$ t m a -> x -> t m b
forall (t :: (* -> *) -> * -> *) (t :: (* -> *) -> * -> *)
       (m :: * -> *).
(IsStream t, IsStream t) =>
t m a -> x -> t m b
go t m a
m x
begin
    where
    go :: t m a -> x -> t m b
go t m a
m1 !x
acc = (forall r.
 State Stream m b
 -> (b -> t m b -> m r) -> (b -> m r) -> m r -> m r)
-> t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
(forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
mkStream ((forall r.
  State Stream m b
  -> (b -> t m b -> m r) -> (b -> m r) -> m r -> m r)
 -> t m b)
-> (forall r.
    State Stream m b
    -> (b -> t m b -> m r) -> (b -> m r) -> m r -> m r)
-> t m b
forall a b. (a -> b) -> a -> b
$ \State Stream m b
st b -> t m b -> m r
yld b -> m r
sng m r
stp ->
        let single :: a -> m r
single a
a = b -> m r
sng (x -> b
done (x -> b) -> x -> b
forall a b. (a -> b) -> a -> b
$ x -> a -> x
step x
acc a
a)
            yieldk :: a -> t m a -> m r
yieldk a
a t m a
r =
                let s :: x
s = x -> a -> x
step x
acc a
a
                in b -> t m b -> m r
yld (x -> b
done x
s) (t m a -> x -> t m b
go t m a
r x
s)
        in State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream (State Stream m b -> State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m b
st) a -> t m a -> m r
yieldk a -> m r
single m r
stp t m a
m1

{-# INLINE scanl' #-}
scanl' :: IsStream t => (b -> a -> b) -> b -> t m a -> t m b
scanl' :: (b -> a -> b) -> b -> t m a -> t m b
scanl' b -> a -> b
step b
begin = (b -> a -> b) -> b -> (b -> b) -> t m a -> t m b
forall (t :: (* -> *) -> * -> *) x a b (m :: * -> *).
IsStream t =>
(x -> a -> x) -> x -> (x -> b) -> t m a -> t m b
scanlx' b -> a -> b
step b
begin b -> b
forall a. a -> a
id

-------------------------------------------------------------------------------
-- Filtering
-------------------------------------------------------------------------------

{-# INLINE filter #-}
filter :: IsStream t => (a -> Bool) -> t m a -> t m a
filter :: (a -> Bool) -> t m a -> t m a
filter a -> Bool
p t m a
m = t m a -> t m a
forall (t :: (* -> *) -> * -> *) (t :: (* -> *) -> * -> *)
       (m :: * -> *).
(IsStream t, IsStream t) =>
t m a -> t m a
go t m a
m
    where
    go :: t m a -> t m a
go t m a
m1 = (forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
(forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
mkStream ((forall r.
  State Stream m a
  -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
 -> t m a)
-> (forall r.
    State Stream m a
    -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> t m a -> m r
yld a -> m r
sng m r
stp ->
        let single :: a -> m r
single a
a   | a -> Bool
p a
a       = a -> m r
sng a
a
                       | Bool
otherwise = m r
stp
            yieldk :: a -> t m a -> m r
yieldk a
a t m a
r | a -> Bool
p a
a       = a -> t m a -> m r
yld a
a (t m a -> t m a
go t m a
r)
                       | Bool
otherwise = State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
st a -> t m a -> m r
yieldk a -> m r
single m r
stp t m a
r
         in State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
st a -> t m a -> m r
yieldk a -> m r
single m r
stp t m a
m1

{-# INLINE take #-}
take :: IsStream t => Int -> t m a -> t m a
take :: Int -> t m a -> t m a
take Int
n t m a
m = Int -> t m a -> t m a
forall (t :: (* -> *) -> * -> *) p (t :: (* -> *) -> * -> *)
       (m :: * -> *) a.
(IsStream t, Ord p, IsStream t, Num p) =>
p -> t m a -> t m a
go Int
n t m a
m
    where
    go :: p -> t m a -> t m a
go p
n1 t m a
m1 = (forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
(forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
mkStream ((forall r.
  State Stream m a
  -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
 -> t m a)
-> (forall r.
    State Stream m a
    -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> t m a -> m r
yld a -> m r
sng m r
stp ->
        let yieldk :: a -> t m a -> m r
yieldk a
a t m a
r = a -> t m a -> m r
yld a
a (p -> t m a -> t m a
go (p
n1 p -> p -> p
forall a. Num a => a -> a -> a
- p
1) t m a
r)
        in if p
n1 p -> p -> Bool
forall a. Ord a => a -> a -> Bool
<= p
0
           then m r
stp
           else State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
st a -> t m a -> m r
yieldk a -> m r
sng m r
stp t m a
m1

{-# INLINE takeWhile #-}
takeWhile :: IsStream t => (a -> Bool) -> t m a -> t m a
takeWhile :: (a -> Bool) -> t m a -> t m a
takeWhile a -> Bool
p t m a
m = t m a -> t m a
forall (t :: (* -> *) -> * -> *) (t :: (* -> *) -> * -> *)
       (m :: * -> *).
(IsStream t, IsStream t) =>
t m a -> t m a
go t m a
m
    where
    go :: t m a -> t m a
go t m a
m1 = (forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
(forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
mkStream ((forall r.
  State Stream m a
  -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
 -> t m a)
-> (forall r.
    State Stream m a
    -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> t m a -> m r
yld a -> m r
sng m r
stp ->
        let single :: a -> m r
single a
a   | a -> Bool
p a
a       = a -> m r
sng a
a
                       | Bool
otherwise = m r
stp
            yieldk :: a -> t m a -> m r
yieldk a
a t m a
r | a -> Bool
p a
a       = a -> t m a -> m r
yld a
a (t m a -> t m a
go t m a
r)
                       | Bool
otherwise = m r
stp
         in State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
st a -> t m a -> m r
yieldk a -> m r
single m r
stp t m a
m1

{-# INLINE drop #-}
drop :: IsStream t => Int -> t m a -> t m a
drop :: Int -> t m a -> t m a
drop Int
n t m a
m = Stream m a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
Stream m a -> t m a
fromStream (Stream m a -> t m a) -> Stream m a -> t m a
forall a b. (a -> b) -> a -> b
$ Stream m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
t m a -> t m a
unShare (Int -> Stream m a -> Stream m a
forall (t :: (* -> *) -> * -> *) p (m :: * -> *) a.
(Ord p, IsStream t, Num p) =>
p -> t m a -> t m a
go Int
n (t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
t m a -> Stream m a
toStream t m a
m))
    where
    go :: p -> t m a -> t m a
go p
n1 t m a
m1 = (forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
(forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
mkStream ((forall r.
  State Stream m a
  -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
 -> t m a)
-> (forall r.
    State Stream m a
    -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> t m a -> m r
yld a -> m r
sng m r
stp ->
        let single :: p -> m r
single p
_ = m r
stp
            yieldk :: p -> t m a -> m r
yieldk p
_ t m a
r = State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStreamShared State Stream m a
st a -> t m a -> m r
yld a -> m r
sng m r
stp (t m a -> m r) -> t m a -> m r
forall a b. (a -> b) -> a -> b
$ p -> t m a -> t m a
go (p
n1 p -> p -> p
forall a. Num a => a -> a -> a
- p
1) t m a
r
        -- Somehow "<=" check performs better than a ">"
        in if p
n1 p -> p -> Bool
forall a. Ord a => a -> a -> Bool
<= p
0
           then State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStreamShared State Stream m a
st a -> t m a -> m r
yld a -> m r
sng m r
stp t m a
m1
           else State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStreamShared State Stream m a
st a -> t m a -> m r
forall p. p -> t m a -> m r
yieldk a -> m r
forall p. p -> m r
single m r
stp t m a
m1

{-# INLINE dropWhile #-}
dropWhile :: IsStream t => (a -> Bool) -> t m a -> t m a
dropWhile :: (a -> Bool) -> t m a -> t m a
dropWhile a -> Bool
p t m a
m = t m a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *).
IsStream t =>
t m a -> t m a
go t m a
m
    where
    go :: t m a -> t m a
go t m a
m1 = (forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
(forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
mkStream ((forall r.
  State Stream m a
  -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
 -> t m a)
-> (forall r.
    State Stream m a
    -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> t m a -> m r
yld a -> m r
sng m r
stp ->
        let single :: a -> m r
single a
a   | a -> Bool
p a
a       = m r
stp
                       | Bool
otherwise = a -> m r
sng a
a
            yieldk :: a -> t m a -> m r
yieldk a
a t m a
r | a -> Bool
p a
a = State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
st a -> t m a -> m r
yieldk a -> m r
single m r
stp t m a
r
                       | Bool
otherwise = a -> t m a -> m r
yld a
a t m a
r
         in State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
st a -> t m a -> m r
yieldk a -> m r
single m r
stp t m a
m1

-------------------------------------------------------------------------------
-- Mapping
-------------------------------------------------------------------------------

-- Be careful when modifying this, this uses a consM (|:) deliberately to allow
-- other stream types to overload it.
{-# INLINE sequence #-}
sequence :: (IsStream t, MonadAsync m) => t m (m a) -> t m a
sequence :: t m (m a) -> t m a
sequence t m (m a)
m = t m (m a) -> t m a
forall (t :: (* -> *) -> * -> *) (t :: (* -> *) -> * -> *)
       (m :: * -> *) a.
(IsStream t, IsStream t, MonadIO m, MonadBaseControl IO m,
 MonadThrow m) =>
t m (m a) -> t m a
go t m (m a)
m
    where
    go :: t m (m a) -> t m a
go t m (m a)
m1 = (forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
(forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
mkStream ((forall r.
  State Stream m a
  -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
 -> t m a)
-> (forall r.
    State Stream m a
    -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> t m a -> m r
yld a -> m r
sng m r
stp ->
        let single :: m a -> m r
single m a
ma = m a
ma m a -> (a -> m r) -> m r
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> m r
sng
            yieldk :: m a -> t m (m a) -> m r
yieldk m a
ma t m (m a)
r = State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStreamShared State Stream m a
st a -> t m a -> m r
yld a -> m r
sng m r
stp (t m a -> m r) -> t m a -> m r
forall a b. (a -> b) -> a -> b
$ m a
ma m a -> t m a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, MonadAsync m) =>
m a -> t m a -> t m a
|: t m (m a) -> t m a
go t m (m a)
r
         in State Stream m (m a)
-> (m a -> t m (m a) -> m r)
-> (m a -> m r)
-> m r
-> t m (m a)
-> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream (State Stream m a -> State Stream m (m a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m a
st) m a -> t m (m a) -> m r
yieldk m a -> m r
single m r
stp t m (m a)
m1

-------------------------------------------------------------------------------
-- Inserting
-------------------------------------------------------------------------------

{-# INLINE intersperseM #-}
intersperseM :: (IsStream t, MonadAsync m) => m a -> t m a -> t m a
intersperseM :: m a -> t m a -> t m a
intersperseM m a
a t m a
m = t m a -> t m a
forall (t :: (* -> *) -> * -> *) (t :: (* -> *) -> * -> *).
(IsStream t, IsStream t) =>
t m a -> t m a
prependingStart t m a
m
    where
    prependingStart :: t m a -> t m a
prependingStart t m a
m1 = (forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
(forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
mkStream ((forall r.
  State Stream m a
  -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
 -> t m a)
-> (forall r.
    State Stream m a
    -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> t m a -> m r
yld a -> m r
sng m r
stp ->
        let yieldk :: a -> t m a -> m r
yieldk a
i t m a
x = State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStreamShared State Stream m a
st a -> t m a -> m r
yld a -> m r
sng m r
stp (t m a -> m r) -> t m a -> m r
forall a b. (a -> b) -> a -> b
$ a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
i m a -> t m a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, MonadAsync m) =>
m a -> t m a -> t m a
|: t m a -> t m a
forall (t :: (* -> *) -> * -> *) (t :: (* -> *) -> * -> *).
(IsStream t, IsStream t) =>
t m a -> t m a
go t m a
x
         in State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
st a -> t m a -> m r
forall (t :: (* -> *) -> * -> *). IsStream t => a -> t m a -> m r
yieldk a -> m r
sng m r
stp t m a
m1
    go :: t m a -> t m a
go t m a
m2 = (forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
(forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
mkStream ((forall r.
  State Stream m a
  -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
 -> t m a)
-> (forall r.
    State Stream m a
    -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> t m a -> m r
yld a -> m r
sng m r
stp ->
        let single :: a -> m r
single a
i = State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStreamShared State Stream m a
st a -> t m a -> m r
yld a -> m r
sng m r
stp (t m a -> m r) -> t m a -> m r
forall a b. (a -> b) -> a -> b
$ m a
a m a -> t m a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, MonadAsync m) =>
m a -> t m a -> t m a
|: a -> t m a
forall (t :: (* -> *) -> * -> *) a (m :: * -> *).
IsStream t =>
a -> t m a
fromPure a
i
            yieldk :: a -> t m a -> m r
yieldk a
i t m a
x = State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStreamShared State Stream m a
st a -> t m a -> m r
yld a -> m r
sng m r
stp (t m a -> m r) -> t m a -> m r
forall a b. (a -> b) -> a -> b
$ m a
a m a -> t m a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, MonadAsync m) =>
m a -> t m a -> t m a
|: a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
i m a -> t m a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, MonadAsync m) =>
m a -> t m a -> t m a
|: t m a -> t m a
go t m a
x
         in State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
st a -> t m a -> m r
yieldk a -> m r
single m r
stp t m a
m2

{-# INLINE intersperse #-}
intersperse :: (IsStream t, MonadAsync m) => a -> t m a -> t m a
intersperse :: a -> t m a -> t m a
intersperse a
a = m a -> t m a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, MonadAsync m) =>
m a -> t m a -> t m a
intersperseM (a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a)

{-# INLINE insertBy #-}
insertBy :: IsStream t => (a -> a -> Ordering) -> a -> t m a -> t m a
insertBy :: (a -> a -> Ordering) -> a -> t m a -> t m a
insertBy a -> a -> Ordering
cmp a
x t m a
m = t m a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *).
IsStream t =>
t m a -> t m a
go t m a
m
  where
    go :: t m a -> t m a
go t m a
m1 = (forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
(forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
mkStream ((forall r.
  State Stream m a
  -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
 -> t m a)
-> (forall r.
    State Stream m a
    -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> t m a -> m r
yld a -> m r
_ m r
_ ->
        let single :: a -> m r
single a
a = case a -> a -> Ordering
cmp a
x a
a of
                Ordering
GT -> a -> t m a -> m r
yld a
a (a -> t m a
forall (t :: (* -> *) -> * -> *) a (m :: * -> *).
IsStream t =>
a -> t m a
fromPure a
x)
                Ordering
_  -> a -> t m a -> m r
yld a
x (a -> t m a
forall (t :: (* -> *) -> * -> *) a (m :: * -> *).
IsStream t =>
a -> t m a
fromPure a
a)
            stop :: m r
stop = a -> t m a -> m r
yld a
x t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
t m a
nil
            yieldk :: a -> t m a -> m r
yieldk a
a t m a
r = case a -> a -> Ordering
cmp a
x a
a of
                Ordering
GT -> a -> t m a -> m r
yld a
a (t m a -> t m a
go t m a
r)
                Ordering
_  -> a -> t m a -> m r
yld a
x (a
a a -> t m a -> t m a
forall (t :: (* -> *) -> * -> *) a (m :: * -> *).
IsStream t =>
a -> t m a -> t m a
`cons` t m a
r)
         in State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
st a -> t m a -> m r
yieldk a -> m r
single m r
stop t m a
m1

------------------------------------------------------------------------------
-- Deleting
------------------------------------------------------------------------------

{-# INLINE deleteBy #-}
deleteBy :: IsStream t => (a -> a -> Bool) -> a -> t m a -> t m a
deleteBy :: (a -> a -> Bool) -> a -> t m a -> t m a
deleteBy a -> a -> Bool
eq a
x t m a
m = t m a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *).
IsStream t =>
t m a -> t m a
go t m a
m
  where
    go :: t m a -> t m a
go t m a
m1 = (forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
(forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
mkStream ((forall r.
  State Stream m a
  -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
 -> t m a)
-> (forall r.
    State Stream m a
    -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> t m a -> m r
yld a -> m r
sng m r
stp ->
        let single :: a -> m r
single a
a = if a -> a -> Bool
eq a
x a
a then m r
stp else a -> m r
sng a
a
            yieldk :: a -> t m a -> m r
yieldk a
a t m a
r = if a -> a -> Bool
eq a
x a
a
              then State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
st a -> t m a -> m r
yld a -> m r
sng m r
stp t m a
r
              else a -> t m a -> m r
yld a
a (t m a -> t m a
go t m a
r)
         in State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
st a -> t m a -> m r
yieldk a -> m r
single m r
stp t m a
m1

------------------------------------------------------------------------------
-- Reordering
------------------------------------------------------------------------------

{-# INLINE reverse #-}
reverse :: IsStream t => t m a -> t m a
reverse :: t m a -> t m a
reverse = (t m a -> a -> t m a) -> t m a -> t m a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) b a.
IsStream t =>
(t m b -> a -> t m b) -> t m b -> t m a -> t m b
foldlS ((a -> t m a -> t m a) -> t m a -> a -> t m a
forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> t m a -> t m a
forall (t :: (* -> *) -> * -> *) a (m :: * -> *).
IsStream t =>
a -> t m a -> t m a
cons) t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
t m a
nil

-------------------------------------------------------------------------------
-- Map and Filter
-------------------------------------------------------------------------------

{-# INLINE mapMaybe #-}
mapMaybe :: IsStream t => (a -> Maybe b) -> t m a -> t m b
mapMaybe :: (a -> Maybe b) -> t m a -> t m b
mapMaybe a -> Maybe b
f t m a
m = t m a -> t m b
forall (t :: (* -> *) -> * -> *) (t :: (* -> *) -> * -> *)
       (m :: * -> *).
(IsStream t, IsStream t) =>
t m a -> t m b
go t m a
m
  where
    go :: t m a -> t m b
go t m a
m1 = (forall r.
 State Stream m b
 -> (b -> t m b -> m r) -> (b -> m r) -> m r -> m r)
-> t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
(forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
mkStream ((forall r.
  State Stream m b
  -> (b -> t m b -> m r) -> (b -> m r) -> m r -> m r)
 -> t m b)
-> (forall r.
    State Stream m b
    -> (b -> t m b -> m r) -> (b -> m r) -> m r -> m r)
-> t m b
forall a b. (a -> b) -> a -> b
$ \State Stream m b
st b -> t m b -> m r
yld b -> m r
sng m r
stp ->
        let single :: a -> m r
single a
a = case a -> Maybe b
f a
a of
                Just b
b  -> b -> m r
sng b
b
                Maybe b
Nothing -> m r
stp
            yieldk :: a -> t m a -> m r
yieldk a
a t m a
r = case a -> Maybe b
f a
a of
                Just b
b  -> b -> t m b -> m r
yld b
b (t m b -> m r) -> t m b -> m r
forall a b. (a -> b) -> a -> b
$ t m a -> t m b
go t m a
r
                Maybe b
Nothing -> State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream (State Stream m b -> State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m b
st) a -> t m a -> m r
yieldk a -> m r
single m r
stp t m a
r
        in State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream (State Stream m b -> State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m b
st) a -> t m a -> m r
yieldk a -> m r
single m r
stp t m a
m1

------------------------------------------------------------------------------
-- Serial Zipping
------------------------------------------------------------------------------

-- | Zip two streams serially using a pure zipping function.
--
-- @since 0.1.0
{-# INLINABLE zipWith #-}
zipWith :: IsStream t => (a -> b -> c) -> t m a -> t m b -> t m c
zipWith :: (a -> b -> c) -> t m a -> t m b -> t m c
zipWith a -> b -> c
f = t m a -> t m b -> t m c
forall (t :: (* -> *) -> * -> *) (t :: (* -> *) -> * -> *)
       (t :: (* -> *) -> * -> *) (m :: * -> *).
(IsStream t, IsStream t, IsStream t) =>
t m a -> t m b -> t m c
go
    where
    go :: t m a -> t m b -> t m c
go t m a
mx t m b
my = (forall r.
 State Stream m c
 -> (c -> t m c -> m r) -> (c -> m r) -> m r -> m r)
-> t m c
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
(forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
mkStream ((forall r.
  State Stream m c
  -> (c -> t m c -> m r) -> (c -> m r) -> m r -> m r)
 -> t m c)
-> (forall r.
    State Stream m c
    -> (c -> t m c -> m r) -> (c -> m r) -> m r -> m r)
-> t m c
forall a b. (a -> b) -> a -> b
$ \State Stream m c
st c -> t m c -> m r
yld c -> m r
sng m r
stp -> do
        let merge :: a -> t m a -> m r
merge a
a t m a
ra =
                let single2 :: b -> m r
single2 b
b = c -> m r
sng (a -> b -> c
f a
a b
b)
                    yield2 :: b -> t m b -> m r
yield2 b
b t m b
rb = c -> t m c -> m r
yld (a -> b -> c
f a
a b
b) (t m a -> t m b -> t m c
go t m a
ra t m b
rb)
                 in State Stream m b
-> (b -> t m b -> m r) -> (b -> m r) -> m r -> t m b -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream (State Stream m c -> State Stream m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m c
st) b -> t m b -> m r
yield2 b -> m r
single2 m r
stp t m b
my
        let single1 :: a -> m r
single1 a
a = a -> t m a -> m r
merge a
a t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
t m a
nil
            yield1 :: a -> t m a -> m r
yield1 = a -> t m a -> m r
merge
        State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream (State Stream m c -> State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m c
st) a -> t m a -> m r
yield1 a -> m r
single1 m r
stp t m a
mx

-- | Zip two streams serially using a monadic zipping function.
--
-- @since 0.1.0
{-# INLINABLE zipWithM #-}
zipWithM :: (IsStream t, Monad m) => (a -> b -> m c) -> t m a -> t m b -> t m c
zipWithM :: (a -> b -> m c) -> t m a -> t m b -> t m c
zipWithM a -> b -> m c
f t m a
m1 t m b
m2 = t m a -> t m b -> t m c
forall (t :: (* -> *) -> * -> *) (t :: (* -> *) -> * -> *)
       (t :: (* -> *) -> * -> *).
(IsStream t, IsStream t, IsStream t) =>
t m a -> t m b -> t m c
go t m a
m1 t m b
m2
    where
    go :: t m a -> t m b -> t m c
go t m a
mx t m b
my = (forall r.
 State Stream m c
 -> (c -> t m c -> m r) -> (c -> m r) -> m r -> m r)
-> t m c
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
(forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
mkStream ((forall r.
  State Stream m c
  -> (c -> t m c -> m r) -> (c -> m r) -> m r -> m r)
 -> t m c)
-> (forall r.
    State Stream m c
    -> (c -> t m c -> m r) -> (c -> m r) -> m r -> m r)
-> t m c
forall a b. (a -> b) -> a -> b
$ \State Stream m c
st c -> t m c -> m r
yld c -> m r
sng m r
stp -> do
        let merge :: a -> t m a -> m r
merge a
a t m a
ra =
                let runIt :: t m c -> m r
runIt t m c
x = State Stream m c
-> (c -> t m c -> m r) -> (c -> m r) -> m r -> t m c -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m c
st c -> t m c -> m r
yld c -> m r
sng m r
stp t m c
x
                    single2 :: b -> m r
single2 b
b   = a -> b -> m c
f a
a b
b m c -> (c -> m r) -> m r
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= c -> m r
sng
                    yield2 :: b -> t m b -> m r
yield2 b
b t m b
rb = a -> b -> m c
f a
a b
b m c -> (c -> m r) -> m r
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \c
x -> t m c -> m r
runIt (c
x c -> t m c -> t m c
forall (t :: (* -> *) -> * -> *) a (m :: * -> *).
IsStream t =>
a -> t m a -> t m a
`cons` t m a -> t m b -> t m c
go t m a
ra t m b
rb)
                 in State Stream m b
-> (b -> t m b -> m r) -> (b -> m r) -> m r -> t m b -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream (State Stream m c -> State Stream m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m c
st) b -> t m b -> m r
yield2 b -> m r
single2 m r
stp t m b
my
        let single1 :: a -> m r
single1 a
a = a -> t m a -> m r
merge a
a t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
t m a
nil
            yield1 :: a -> t m a -> m r
yield1 = a -> t m a -> m r
merge
        State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream (State Stream m c -> State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m c
st) a -> t m a -> m r
yield1 a -> m r
single1 m r
stp t m a
mx

------------------------------------------------------------------------------
-- Merging
------------------------------------------------------------------------------

{-# INLINE mergeByM #-}
mergeByM
    :: (IsStream t, Monad m)
    => (a -> a -> m Ordering) -> t m a -> t m a -> t m a
mergeByM :: (a -> a -> m Ordering) -> t m a -> t m a -> t m a
mergeByM a -> a -> m Ordering
cmp = t m a -> t m a -> t m a
forall (t :: (* -> *) -> * -> *).
IsStream t =>
t m a -> t m a -> t m a
go
    where
    go :: t m a -> t m a -> t m a
go t m a
mx t m a
my = (forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
(forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
mkStream ((forall r.
  State Stream m a
  -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
 -> t m a)
-> (forall r.
    State Stream m a
    -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> t m a -> m r
yld a -> m r
sng m r
stp -> do
        let mergeWithY :: a -> t m a -> m r
mergeWithY a
a t m a
ra =
                let stop2 :: m r
stop2 = State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
st a -> t m a -> m r
yld a -> m r
sng m r
stp t m a
mx
                    single2 :: a -> m r
single2 a
b = do
                        Ordering
r <- a -> a -> m Ordering
cmp a
a a
b
                        case Ordering
r of
                            Ordering
GT -> a -> t m a -> m r
yld a
b (t m a -> t m a -> t m a
go (a
a a -> t m a -> t m a
forall (t :: (* -> *) -> * -> *) a (m :: * -> *).
IsStream t =>
a -> t m a -> t m a
`cons` t m a
ra) t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
t m a
nil)
                            Ordering
_  -> a -> t m a -> m r
yld a
a (t m a -> t m a -> t m a
go t m a
ra (a
b a -> t m a -> t m a
forall (t :: (* -> *) -> * -> *) a (m :: * -> *).
IsStream t =>
a -> t m a -> t m a
`cons` t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
t m a
nil))
                    yield2 :: a -> t m a -> m r
yield2 a
b t m a
rb = do
                        Ordering
r <- a -> a -> m Ordering
cmp a
a a
b
                        case Ordering
r of
                            Ordering
GT -> a -> t m a -> m r
yld a
b (t m a -> t m a -> t m a
go (a
a a -> t m a -> t m a
forall (t :: (* -> *) -> * -> *) a (m :: * -> *).
IsStream t =>
a -> t m a -> t m a
`cons` t m a
ra) t m a
rb)
                            Ordering
_  -> a -> t m a -> m r
yld a
a (t m a -> t m a -> t m a
go t m a
ra (a
b a -> t m a -> t m a
forall (t :: (* -> *) -> * -> *) a (m :: * -> *).
IsStream t =>
a -> t m a -> t m a
`cons` t m a
rb))
                 in State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
st a -> t m a -> m r
yield2 a -> m r
single2 m r
stop2 t m a
my
        let stopX :: m r
stopX = State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
st a -> t m a -> m r
yld a -> m r
sng m r
stp t m a
my
            singleX :: a -> m r
singleX a
a = a -> t m a -> m r
mergeWithY a
a t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
t m a
nil
            yieldX :: a -> t m a -> m r
yieldX = a -> t m a -> m r
mergeWithY
        State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
st a -> t m a -> m r
yieldX a -> m r
singleX m r
stopX t m a
mx

{-# INLINABLE mergeBy #-}
mergeBy
    :: (IsStream t, Monad m)
    => (a -> a -> Ordering) -> t m a -> t m a -> t m a
mergeBy :: (a -> a -> Ordering) -> t m a -> t m a -> t m a
mergeBy a -> a -> Ordering
cmp = (a -> a -> m Ordering) -> t m a -> t m a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
(a -> a -> m Ordering) -> t m a -> t m a -> t m a
mergeByM (\a
a a
b -> Ordering -> m Ordering
forall (m :: * -> *) a. Monad m => a -> m a
return (Ordering -> m Ordering) -> Ordering -> m Ordering
forall a b. (a -> b) -> a -> b
$ a -> a -> Ordering
cmp a
a a
b)

------------------------------------------------------------------------------
-- Transformation comprehensions
------------------------------------------------------------------------------

{-# INLINE the #-}
the :: (Eq a, IsStream t, Monad m) => t m a -> m (Maybe a)
the :: t m a -> m (Maybe a)
the t m a
m = do
    Maybe (a, t m a)
r <- t m a -> m (Maybe (a, t m a))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> m (Maybe (a, t m a))
uncons t m a
m
    case Maybe (a, t m a)
r of
        Maybe (a, t m a)
Nothing -> Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
        Just (a
h, t m a
t) -> a -> t m a -> m (Maybe a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m, Eq a) =>
a -> t m a -> m (Maybe a)
go a
h t m a
t
    where
    go :: a -> t m a -> m (Maybe a)
go a
h t m a
m1 =
        let single :: a -> m (Maybe a)
single a
a   | a
h a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
a    = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe a -> m (Maybe a)) -> Maybe a -> m (Maybe a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a
forall a. a -> Maybe a
Just a
h
                       | Bool
otherwise = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
            yieldk :: a -> t m a -> m (Maybe a)
yieldk a
a t m a
r | a
h a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
a    = a -> t m a -> m (Maybe a)
go a
h t m a
r
                       | Bool
otherwise = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
         in State Stream m a
-> (a -> t m a -> m (Maybe a))
-> (a -> m (Maybe a))
-> m (Maybe a)
-> t m a
-> m (Maybe a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> t m a -> m (Maybe a)
yieldk a -> m (Maybe a)
forall (m :: * -> *). Monad m => a -> m (Maybe a)
single (Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe a -> m (Maybe a)) -> Maybe a -> m (Maybe a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a
forall a. a -> Maybe a
Just a
h) t m a
m1

------------------------------------------------------------------------------
-- Alternative & MonadPlus
------------------------------------------------------------------------------

_alt :: Stream m a -> Stream m a -> Stream m a
_alt :: Stream m a -> Stream m a -> Stream m a
_alt Stream m a
m1 Stream m a
m2 = (forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
(forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
mkStream ((forall r.
  State Stream m a
  -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
 -> Stream m a)
-> (forall r.
    State Stream m a
    -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp ->
    let stop :: m r
stop  = State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp Stream m a
m2
    in State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stop Stream m a
m1

------------------------------------------------------------------------------
-- MonadReader
------------------------------------------------------------------------------

{-# INLINABLE withLocal #-}
withLocal :: MonadReader r m => (r -> r) -> Stream m a -> Stream m a
withLocal :: (r -> r) -> Stream m a -> Stream m a
withLocal r -> r
f Stream m a
m =
    (forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
(forall r.
 State Stream m a
 -> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
mkStream ((forall r.
  State Stream m a
  -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
 -> Stream m a)
-> (forall r.
    State Stream m a
    -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp ->
        let single :: a -> m r
single = (r -> r) -> m r -> m r
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local r -> r
f (m r -> m r) -> (a -> m r) -> a -> m r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m r
sng
            yieldk :: a -> Stream m a -> m r
yieldk a
a Stream m a
r = (r -> r) -> m r -> m r
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local r -> r
f (m r -> m r) -> m r -> m r
forall a b. (a -> b) -> a -> b
$ a -> Stream m a -> m r
yld a
a ((r -> r) -> Stream m a -> Stream m a
forall r (m :: * -> *) a.
MonadReader r m =>
(r -> r) -> Stream m a -> Stream m a
withLocal r -> r
f Stream m a
r)
        in State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yieldk a -> m r
single ((r -> r) -> m r -> m r
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local r -> r
f m r
stp) Stream m a
m

------------------------------------------------------------------------------
-- MonadError
------------------------------------------------------------------------------

{-
-- XXX handle and test cross thread state transfer
withCatchError
    :: MonadError e m
    => Stream m a -> (e -> Stream m a) -> Stream m a
withCatchError m h =
    mkStream $ \_ stp sng yld ->
        let run x = unStream x Nothing stp sng yieldk
            handle r = r `catchError` \e -> run $ h e
            yieldk a r = yld a (withCatchError r h)
        in handle $ run m
-}