{-# LANGUAGE UndecidableInstances #-}
#include "inline.hs"
module Streamly.Internal.Data.Stream.StreamK.Type
(
Stream (..)
, toStreamK
, fromStreamK
, mkStream
, foldStream
, foldStreamShared
, foldrM
, foldrS
, foldrSShared
, foldrSM
, build
, buildS
, buildM
, buildSM
, augmentS
, augmentSM
, fromStopK
, fromYieldK
, consK
, cons
, (.:)
, consM
, consMBy
, nil
, nilM
, fromEffect
, fromPure
, unfoldr
, unfoldrMWith
, repeat
, repeatMWith
, replicateMWith
, fromIndicesMWith
, iterateMWith
, fromFoldable
, fromFoldableM
, mfix
, uncons
, foldl'
, foldlx'
, drain
, null
, tail
, init
, conjoin
, serial
, map
, mapMWith
, mapMSerial
, unShare
, concatMapWith
, concatMap
, bindWith
, concatPairsWith
, apWith
, apSerial
, apSerialDiscardFst
, apSerialDiscardSnd
, foldlS
, reverse
, withLocal
)
where
import Control.Monad (ap, (>=>))
import Control.Monad.Reader.Class (MonadReader(..))
import Control.Monad.Trans.Class (MonadTrans(lift))
import Data.Function (fix)
#if __GLASGOW_HASKELL__ < 804
import Data.Semigroup (Semigroup(..))
#endif
import Streamly.Internal.Data.SVar.Type (State, adaptState, defState)
import Prelude hiding
(map, mapM, concatMap, foldr, repeat, null, reverse, tail, init)
import qualified Prelude
newtype Stream m a =
MkStream (forall r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> m r
)
{-# INLINE fromStreamK #-}
fromStreamK :: Stream m a -> Stream m a
fromStreamK :: Stream m a -> Stream m a
fromStreamK = Stream m a -> Stream m a
forall a. a -> a
id
{-# 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
mkStream
:: (forall r. State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> m r)
-> Stream 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
mkStream = (forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
MkStream
type StopK m = forall r. m r -> m r
type YieldK m a = forall r. (a -> m r) -> m r
_wrapM :: Monad m => m a -> YieldK m a
_wrapM :: m a -> YieldK m a
_wrapM m a
m = (m a
m m a -> (a -> m r) -> m r
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=)
fromStopK :: StopK m -> Stream m a
fromStopK :: StopK m -> Stream m a
fromStopK StopK m
k = (forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream 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
_ a -> Stream m a -> m r
_ a -> m r
_ m r
stp -> m r -> m r
StopK m
k m r
stp
fromYieldK :: YieldK m a -> Stream m a
fromYieldK :: YieldK m a -> Stream m a
fromYieldK YieldK m a
k = (forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream 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
_ a -> Stream m a -> m r
_ a -> m r
sng m r
_ -> (a -> m r) -> m r
YieldK m a
k a -> m r
sng
consK :: YieldK m a -> Stream m a -> Stream m a
consK :: YieldK m a -> Stream m a -> Stream m a
consK YieldK m a
k Stream m a
r = (forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream 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
_ a -> Stream m a -> m r
yld a -> m r
_ m r
_ -> (a -> m r) -> m r
YieldK m a
k (a -> Stream m a -> m r
`yld` Stream m a
r)
infixr 5 `cons`
{-# INLINE_NORMAL cons #-}
cons :: a -> Stream m a -> Stream m a
cons :: a -> Stream m a -> Stream m a
cons a
a Stream m a
r = (forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream 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
_ a -> Stream m a -> m r
yield a -> m r
_ m r
_ -> a -> Stream m a -> m r
yield a
a Stream m a
r
infixr 5 .:
{-# INLINE (.:) #-}
(.:) :: a -> Stream m a -> Stream m a
.: :: a -> Stream m a -> Stream m a
(.:) = a -> Stream m a -> Stream m a
forall a (m :: * -> *). a -> Stream m a -> Stream m a
cons
{-# INLINE_NORMAL nil #-}
nil :: Stream m a
nil :: Stream m a
nil = (forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream 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
_ a -> Stream m a -> m r
_ a -> m r
_ m r
stp -> m r
stp
{-# INLINE_NORMAL nilM #-}
nilM :: Applicative m => m b -> Stream m a
nilM :: m b -> Stream m a
nilM m b
m = (forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream 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
_ a -> Stream m a -> m r
_ a -> m r
_ m r
stp -> m b
m m b -> m r -> m r
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> m r
stp
{-# INLINE_NORMAL fromPure #-}
fromPure :: a -> Stream m a
fromPure :: a -> Stream m a
fromPure a
a = (forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream 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
_ a -> Stream m a -> m r
_ a -> m r
single m r
_ -> a -> m r
single a
a
{-# INLINE_NORMAL fromEffect #-}
fromEffect :: Monad m => m a -> Stream m a
fromEffect :: m a -> Stream m a
fromEffect 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 (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream 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
_ a -> Stream m a -> m r
_ a -> m r
single m r
_ -> m a
m m a -> (a -> m r) -> m r
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> m r
single
infixr 5 `consM`
{-# INLINE consM #-}
{-# SPECIALIZE consM :: IO a -> Stream IO a -> Stream IO a #-}
consM :: Monad m => m a -> Stream m a -> Stream m a
consM :: m a -> Stream m a -> Stream m a
consM m a
m Stream m a
r = (forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream 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
_ a -> Stream m a -> m r
yld a -> m r
_ m r
_ -> m a
m m a -> (a -> m r) -> m r
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (a -> Stream m a -> m r
`yld` Stream m a
r)
{-# INLINE consMBy #-}
consMBy :: Monad m =>
(Stream m a -> Stream m a -> Stream m a) -> m a -> Stream m a -> Stream m a
consMBy :: (Stream m a -> Stream m a -> Stream m a)
-> m a -> Stream m a -> Stream m a
consMBy Stream m a -> Stream m a -> Stream m a
f m a
m Stream m a
r = m a -> Stream m a
forall (m :: * -> *) a. Monad m => m a -> Stream m a
fromEffect m a
m Stream m a -> Stream m a -> Stream m a
`f` Stream m a
r
{-# INLINE_EARLY foldStreamShared #-}
foldStreamShared
:: State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStreamShared :: State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStreamShared State Stream m a
s a -> Stream m a -> m r
yield a -> m r
single m r
stop (MkStream forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r
k) = State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r
forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r
k State Stream m a
s a -> Stream m a -> m r
yield a -> m r
single m r
stop
{-# INLINE foldStream #-}
foldStream
:: State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream :: State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
s a -> Stream m a -> m r
yield a -> m r
single m r
stop (MkStream forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r
k) =
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r
forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r
k (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
s) a -> Stream m a -> m r
yield a -> m r
single m r
stop
{-# INLINE foldrSWith #-}
foldrSWith ::
(forall r. State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r)
-> (a -> Stream m b -> Stream m b)
-> Stream m b
-> Stream m a
-> Stream m b
foldrSWith :: (forall r.
State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r)
-> (a -> Stream m b -> Stream m b)
-> Stream m b
-> Stream m a
-> Stream m b
foldrSWith forall r.
State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r
f a -> Stream m b -> Stream m b
step Stream m b
final Stream m a
m = Stream m a -> Stream m b
go Stream m a
m
where
go :: Stream m a -> Stream m b
go Stream m a
m1 = (forall r.
State Stream m b
-> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
-> Stream m b
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
State Stream m b
-> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
-> Stream m b)
-> (forall r.
State Stream m b
-> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
-> Stream m b
forall a b. (a -> b) -> a -> b
$ \State Stream m b
st b -> Stream m b -> m r
yld b -> m r
sng m r
stp ->
let run :: Stream m b -> m r
run Stream m b
x = State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r
forall r.
State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r
f State Stream m b
st b -> Stream m b -> m r
yld b -> m r
sng m r
stp Stream m b
x
stop :: m r
stop = Stream m b -> m r
run Stream m b
final
single :: a -> m r
single a
a = Stream m b -> m r
run (Stream m b -> m r) -> Stream m b -> m r
forall a b. (a -> b) -> a -> b
$ a -> Stream m b -> Stream m b
step a
a Stream m b
final
yieldk :: a -> Stream m a -> m r
yieldk a
a Stream m a
r = Stream m b -> m r
run (Stream m b -> m r) -> Stream m b -> m r
forall a b. (a -> b) -> a -> b
$ a -> Stream m b -> Stream m b
step a
a (Stream m a -> Stream m b
go 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 (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream 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 -> Stream m a -> m r
yieldk a -> m r
single m r
stop Stream m a
m1
{-# INLINE_NORMAL foldrSShared #-}
foldrSShared ::
(a -> Stream m b -> Stream m b)
-> Stream m b
-> Stream m a
-> Stream m b
= (forall r.
State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r)
-> (a -> Stream m b -> Stream m b)
-> Stream m b
-> Stream m a
-> Stream m b
forall (m :: * -> *) b a.
(forall r.
State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r)
-> (a -> Stream m b -> Stream m b)
-> Stream m b
-> Stream m a
-> Stream m b
foldrSWith forall r.
State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStreamShared
{-# RULES "foldrSShared/nil"
forall k z. foldrSShared k z nil = z #-}
{-# RULES "foldrSShared/single"
forall k z x. foldrSShared k z (fromPure x) = k x z #-}
{-# INLINE_NORMAL foldrS #-}
foldrS ::
(a -> Stream m b -> Stream m b)
-> Stream m b
-> Stream m a
-> Stream m b
foldrS :: (a -> Stream m b -> Stream m b)
-> Stream m b -> Stream m a -> Stream m b
foldrS = (forall r.
State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r)
-> (a -> Stream m b -> Stream m b)
-> Stream m b
-> Stream m a
-> Stream m b
forall (m :: * -> *) b a.
(forall r.
State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r)
-> (a -> Stream m b -> Stream m b)
-> Stream m b
-> Stream m a
-> Stream m b
foldrSWith forall r.
State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream
{-# RULES "foldrS/id" foldrS cons nil = \x -> x #-}
{-# RULES "foldrS/nil" forall k z. foldrS k z nil = z #-}
{-# RULES "foldrS/single" forall k z x. foldrS k z (fromPure x) = k x z #-}
{-# INLINE foldrSMWith #-}
foldrSMWith :: Monad m
=> (forall r. State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r)
-> (m a -> Stream m b -> Stream m b)
-> Stream m b
-> Stream m a
-> Stream m b
foldrSMWith :: (forall r.
State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r)
-> (m a -> Stream m b -> Stream m b)
-> Stream m b
-> Stream m a
-> Stream m b
foldrSMWith forall r.
State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r
f m a -> Stream m b -> Stream m b
step Stream m b
final Stream m a
m = Stream m a -> Stream m b
go Stream m a
m
where
go :: Stream m a -> Stream m b
go Stream m a
m1 = (forall r.
State Stream m b
-> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
-> Stream m b
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
State Stream m b
-> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
-> Stream m b)
-> (forall r.
State Stream m b
-> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
-> Stream m b
forall a b. (a -> b) -> a -> b
$ \State Stream m b
st b -> Stream m b -> m r
yld b -> m r
sng m r
stp ->
let run :: Stream m b -> m r
run Stream m b
x = State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r
forall r.
State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r
f State Stream m b
st b -> Stream m b -> m r
yld b -> m r
sng m r
stp Stream m b
x
stop :: m r
stop = Stream m b -> m r
run Stream m b
final
single :: a -> m r
single a
a = Stream m b -> m r
run (Stream m b -> m r) -> Stream m b -> m r
forall a b. (a -> b) -> a -> b
$ m a -> Stream m b -> Stream m b
step (a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a) Stream m b
final
yieldk :: a -> Stream m a -> m r
yieldk a
a Stream m a
r = Stream m b -> m r
run (Stream m b -> m r) -> Stream m b -> m r
forall a b. (a -> b) -> a -> b
$ m a -> Stream m b -> Stream m b
step (a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a) (Stream m a -> Stream m b
go 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 (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream 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 -> Stream m a -> m r
yieldk a -> m r
single m r
stop Stream m a
m1
{-# INLINE_NORMAL foldrSM #-}
foldrSM :: Monad m
=> (m a -> Stream m b -> Stream m b)
-> Stream m b
-> Stream m a
-> Stream m b
foldrSM :: (m a -> Stream m b -> Stream m b)
-> Stream m b -> Stream m a -> Stream m b
foldrSM = (forall r.
State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r)
-> (m a -> Stream m b -> Stream m b)
-> Stream m b
-> Stream m a
-> Stream m b
forall (m :: * -> *) b a.
Monad m =>
(forall r.
State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r)
-> (m a -> Stream m b -> Stream m b)
-> Stream m b
-> Stream m a
-> Stream m b
foldrSMWith forall r.
State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream
{-# RULES "foldrSM/nil" forall k z. foldrSM k z nil = z #-}
{-# RULES "foldrSM/single" forall k z x. foldrSM k z (fromEffect x) = k x z #-}
{-# INLINE_NORMAL foldrSMShared #-}
foldrSMShared :: Monad m
=> (m a -> Stream m b -> Stream m b)
-> Stream m b
-> Stream m a
-> Stream m b
foldrSMShared :: (m a -> Stream m b -> Stream m b)
-> Stream m b -> Stream m a -> Stream m b
foldrSMShared = (forall r.
State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r)
-> (m a -> Stream m b -> Stream m b)
-> Stream m b
-> Stream m a
-> Stream m b
forall (m :: * -> *) b a.
Monad m =>
(forall r.
State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r)
-> (m a -> Stream m b -> Stream m b)
-> Stream m b
-> Stream m a
-> Stream m b
foldrSMWith forall r.
State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStreamShared
{-# RULES "foldrSMShared/nil"
forall k z. foldrSMShared k z nil = z #-}
{-# RULES "foldrSMShared/single"
forall k z x. foldrSMShared k z (fromEffect x) = k x z #-}
{-# INLINE_NORMAL build #-}
build :: forall m a. (forall b. (a -> b -> b) -> b -> b) -> Stream m a
build :: (forall b. (a -> b -> b) -> b -> b) -> Stream m a
build forall b. (a -> b -> b) -> b -> b
g = (a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a
forall b. (a -> b -> b) -> b -> b
g a -> Stream m a -> Stream m a
forall a (m :: * -> *). a -> Stream m a -> Stream m a
cons Stream m a
forall (m :: * -> *) a. Stream m a
nil
{-# RULES "foldrM/build"
forall k z (g :: forall b. (a -> b -> b) -> b -> b).
foldrM k z (build g) = g k z #-}
{-# RULES "foldrS/build"
forall k z (g :: forall b. (a -> b -> b) -> b -> b).
foldrS k z (build g) = g k z #-}
{-# RULES "foldrS/cons/build"
forall k z x (g :: forall b. (a -> b -> b) -> b -> b).
foldrS k z (x `cons` build g) = k x (g k z) #-}
{-# RULES "foldrSShared/build"
forall k z (g :: forall b. (a -> b -> b) -> b -> b).
foldrSShared k z (build g) = g k z #-}
{-# RULES "foldrSShared/cons/build"
forall k z x (g :: forall b. (a -> b -> b) -> b -> b).
foldrSShared k z (x `cons` build g) = k x (g k z) #-}
{-# INLINE_NORMAL buildS #-}
buildS ::
((a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a)
-> Stream m a
buildS :: ((a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a)
-> Stream m a
buildS (a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a
g = (a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a
g a -> Stream m a -> Stream m a
forall a (m :: * -> *). a -> Stream m a -> Stream m a
cons Stream m a
forall (m :: * -> *) a. Stream m a
nil
{-# RULES "foldrS/buildS"
forall k z
(g :: (a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a).
foldrS k z (buildS g) = g k z #-}
{-# RULES "foldrS/cons/buildS"
forall k z x
(g :: (a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a).
foldrS k z (x `cons` buildS g) = k x (g k z) #-}
{-# RULES "foldrSShared/buildS"
forall k z
(g :: (a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a).
foldrSShared k z (buildS g) = g k z #-}
{-# RULES "foldrSShared/cons/buildS"
forall k z x
(g :: (a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a).
foldrSShared k z (x `cons` buildS g) = k x (g k z) #-}
{-# INLINE_NORMAL buildSM #-}
buildSM :: Monad m
=> ((m a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a)
-> Stream m a
buildSM :: ((m a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a)
-> Stream m a
buildSM (m a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a
g = (m a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a
g m a -> Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => m a -> Stream m a -> Stream m a
consM Stream m a
forall (m :: * -> *) a. Stream m a
nil
{-# RULES "foldrSM/buildSM"
forall k z
(g :: (m a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a).
foldrSM k z (buildSM g) = g k z #-}
{-# RULES "foldrSMShared/buildSM"
forall k z
(g :: (m a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a).
foldrSMShared k z (buildSM g) = g k z #-}
{-# INLINE_NORMAL buildM #-}
buildM :: Monad m
=> (forall r. (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> m r
)
-> Stream m a
buildM :: (forall r. (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
buildM forall r. (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r
g = (forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream 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 ->
(a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r
forall r. (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r
g (\a
a Stream m a
r -> State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp (a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a m a -> Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => m a -> Stream m a -> Stream m a
`consM` Stream m a
r)) a -> m r
sng m r
stp
{-# INLINE_NORMAL sharedMWith #-}
sharedMWith :: Monad m
=> (m a -> Stream m a -> Stream m a)
-> (forall r. (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> m r
)
-> Stream m a
sharedMWith :: (m a -> Stream m a -> Stream m a)
-> (forall r. (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
sharedMWith m a -> Stream m a -> Stream m a
cns forall r. (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r
g = (forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream 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 ->
(a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r
forall r. (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r
g (\a
a Stream m a
r -> State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStreamShared State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp (a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a m a -> Stream m a -> Stream m a
`cns` Stream m a
r)) a -> m r
sng m r
stp
{-# INLINE_NORMAL augmentS #-}
augmentS ::
((a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a)
-> Stream m a
-> Stream m a
augmentS :: ((a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a)
-> Stream m a -> Stream m a
augmentS (a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a
g Stream m a
xs = (a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a
g a -> Stream m a -> Stream m a
forall a (m :: * -> *). a -> Stream m a -> Stream m a
cons Stream m a
xs
{-# RULES "augmentS/nil"
forall (g :: (a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a).
augmentS g nil = buildS g
#-}
{-# RULES "foldrS/augmentS"
forall k z xs
(g :: (a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a).
foldrS k z (augmentS g xs) = g k (foldrS k z xs)
#-}
{-# RULES "augmentS/buildS"
forall (g :: (a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a)
(h :: (a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a).
augmentS g (buildS h) = buildS (\c n -> g c (h c n))
#-}
{-# INLINE_NORMAL augmentSM #-}
augmentSM :: Monad m =>
((m a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a)
-> Stream m a -> Stream m a
augmentSM :: ((m a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a)
-> Stream m a -> Stream m a
augmentSM (m a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a
g Stream m a
xs = (m a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a
g m a -> Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => m a -> Stream m a -> Stream m a
consM Stream m a
xs
{-# RULES "augmentSM/nil"
forall
(g :: (m a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a).
augmentSM g nil = buildSM g
#-}
{-# RULES "foldrSM/augmentSM"
forall k z xs
(g :: (m a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a).
foldrSM k z (augmentSM g xs) = g k (foldrSM k z xs)
#-}
{-# RULES "augmentSM/buildSM"
forall
(g :: (m a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a)
(h :: (m a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a).
augmentSM g (buildSM h) = buildSM (\c n -> g c (h c n))
#-}
{-# INLINE_NORMAL foldrM #-}
foldrM :: (a -> m b -> m b) -> m b -> Stream m a -> m b
foldrM :: (a -> m b -> m b) -> m b -> Stream m a -> m b
foldrM a -> m b -> m b
step m b
acc Stream m a
m = Stream m a -> m b
go Stream m a
m
where
go :: Stream m a -> m b
go Stream m a
m1 =
let stop :: m b
stop = m b
acc
single :: a -> m b
single a
a = a -> m b -> m b
step a
a m b
acc
yieldk :: a -> Stream m a -> m b
yieldk a
a Stream m a
r = a -> m b -> m b
step a
a (Stream m a -> m b
go Stream m a
r)
in State Stream m a
-> (a -> Stream m a -> m b)
-> (a -> m b)
-> m b
-> Stream m a
-> m b
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> Stream m a -> m b
yieldk a -> m b
single m b
stop Stream m a
m1
{-# INLINE_NORMAL foldrMKWith #-}
foldrMKWith
:: (State Stream m a
-> (a -> Stream m a -> m b)
-> (a -> m b)
-> m b
-> Stream m a
-> m b)
-> (a -> m b -> m b)
-> m b
-> ((a -> Stream m a -> m b) -> (a -> m b) -> m b -> m b)
-> m b
foldrMKWith :: (State Stream m a
-> (a -> Stream m a -> m b)
-> (a -> m b)
-> m b
-> Stream m a
-> m b)
-> (a -> m b -> m b)
-> m b
-> ((a -> Stream m a -> m b) -> (a -> m b) -> m b -> m b)
-> m b
foldrMKWith State Stream m a
-> (a -> Stream m a -> m b)
-> (a -> m b)
-> m b
-> Stream m a
-> m b
f a -> m b -> m b
step m b
acc = ((a -> Stream m a -> m b) -> (a -> m b) -> m b -> m b) -> m b
go
where
go :: ((a -> Stream m a -> m b) -> (a -> m b) -> m b -> m b) -> m b
go (a -> Stream m a -> m b) -> (a -> m b) -> m b -> m b
k =
let stop :: m b
stop = m b
acc
single :: a -> m b
single a
a = a -> m b -> m b
step a
a m b
acc
yieldk :: a -> Stream m a -> m b
yieldk a
a Stream m a
r = a -> m b -> m b
step a
a (((a -> Stream m a -> m b) -> (a -> m b) -> m b -> m b) -> m b
go (\a -> Stream m a -> m b
yld a -> m b
sng m b
stp -> State Stream m a
-> (a -> Stream m a -> m b)
-> (a -> m b)
-> m b
-> Stream m a
-> m b
f State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> Stream m a -> m b
yld a -> m b
sng m b
stp Stream m a
r))
in (a -> Stream m a -> m b) -> (a -> m b) -> m b -> m b
k a -> Stream m a -> m b
yieldk a -> m b
single m b
stop
{-# RULES "foldrM/buildM"
forall step acc (g :: (forall r.
(a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> m r
)).
foldrM step acc (buildM g) = foldrMKWith foldStream step acc g
#-}
{-# INLINE foldlx' #-}
foldlx' :: forall m a b x. Monad m
=> (x -> a -> x) -> x -> (x -> b) -> Stream m a -> m b
foldlx' :: (x -> a -> x) -> x -> (x -> b) -> Stream m a -> m b
foldlx' x -> a -> x
step x
begin x -> b
done Stream m a
m = Stream m x -> m b
get (Stream m x -> m b) -> Stream m x -> m b
forall a b. (a -> b) -> a -> b
$ Stream m a -> x -> Stream m x
go Stream m a
m x
begin
where
{-# NOINLINE get #-}
get :: Stream m x -> m b
get :: Stream m x -> m b
get Stream m x
m1 =
let single :: x -> m b
single = b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> m b) -> (x -> b) -> x -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. x -> b
done
in State Stream m x
-> (x -> Stream m x -> m b)
-> (x -> m b)
-> m b
-> Stream m x
-> m b
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStreamShared State Stream m x
forall a. HasCallStack => a
undefined x -> Stream m x -> m b
forall a. HasCallStack => a
undefined x -> m b
single m b
forall a. HasCallStack => a
undefined Stream m x
m1
go :: Stream m a -> x -> Stream m x
go :: Stream m a -> x -> Stream m x
go Stream m a
m1 !x
acc = (forall r.
State Stream m x
-> (x -> Stream m x -> m r) -> (x -> m r) -> m r -> m r)
-> Stream m x
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
State Stream m x
-> (x -> Stream m x -> m r) -> (x -> m r) -> m r -> m r)
-> Stream m x)
-> (forall r.
State Stream m x
-> (x -> Stream m x -> m r) -> (x -> m r) -> m r -> m r)
-> Stream m x
forall a b. (a -> b) -> a -> b
$ \State Stream m x
_ x -> Stream m x -> m r
yld x -> m r
sng m r
_ ->
let stop :: m r
stop = x -> m r
sng x
acc
single :: a -> m r
single a
a = x -> m r
sng (x -> m r) -> x -> m r
forall a b. (a -> b) -> a -> b
$ x -> a -> x
step x
acc a
a
yieldk :: a -> Stream m a -> m r
yieldk a
a Stream m a
r = State Stream m x
-> (x -> Stream m x -> m r)
-> (x -> m r)
-> m r
-> Stream m x
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m x
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState x -> Stream m x -> m r
yld x -> m r
sng m r
forall a. HasCallStack => a
undefined (Stream m x -> m r) -> Stream m x -> m r
forall a b. (a -> b) -> a -> b
$
Stream m a -> x -> Stream m x
go Stream m a
r (x -> a -> x
step x
acc a
a)
in State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> Stream m a -> m r
yieldk a -> m r
single m r
stop Stream m a
m1
{-# INLINE foldl' #-}
foldl' :: Monad m => (b -> a -> b) -> b -> Stream m a -> m b
foldl' :: (b -> a -> b) -> b -> Stream m a -> m b
foldl' b -> a -> b
step b
begin = (b -> a -> b) -> b -> (b -> b) -> Stream m a -> m b
forall (m :: * -> *) a b x.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Stream m a -> m b
foldlx' b -> a -> b
step b
begin b -> b
forall a. a -> a
id
{-# INLINE drain #-}
drain :: Monad m => Stream m a -> m ()
drain :: Stream m a -> m ()
drain = (a -> m () -> m ()) -> m () -> Stream m a -> m ()
forall a (m :: * -> *) b.
(a -> m b -> m b) -> m b -> Stream m a -> m b
foldrM (\a
_ m ()
xs -> m ()
xs) (() -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
{-# INLINE null #-}
null :: Monad m => Stream m a -> m Bool
null :: Stream m a -> m Bool
null Stream 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 -> Stream m a -> m Bool)
-> (a -> m Bool)
-> m Bool
-> Stream m a
-> m Bool
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> Stream 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 Stream m a
m
infixr 6 `serial`
{-# INLINE serial #-}
serial :: Stream m a -> Stream m a -> Stream m a
serial :: Stream m a -> Stream m a -> Stream m a
serial Stream m a
m1 Stream m a
m2 = Stream m a -> Stream m a
go Stream m a
m1
where
go :: Stream m a -> Stream m a
go 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 (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream 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 (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream 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
single :: a -> m r
single a
a = a -> Stream m a -> m r
yld a
a Stream m a
m2
yieldk :: a -> Stream m a -> m r
yieldk a
a Stream m a
r = a -> Stream m a -> m r
yld a
a (Stream m a -> Stream m a
go 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 (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yieldk a -> m r
single m r
stop Stream m a
m
{-# INLINE conjoin #-}
conjoin :: Monad m => Stream m a -> Stream m a -> Stream m a
conjoin :: Stream m a -> Stream m a -> Stream m a
conjoin Stream m a
xs = ((m a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a)
-> Stream m a -> Stream m a
forall (m :: * -> *) a.
Monad m =>
((m a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a)
-> Stream m a -> Stream m a
augmentSM (\m a -> Stream m a -> Stream m a
c Stream m a
n -> (m a -> Stream m a -> Stream m a)
-> Stream m a -> Stream m a -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
(m a -> Stream m b -> Stream m b)
-> Stream m b -> Stream m a -> Stream m b
foldrSM m a -> Stream m a -> Stream m a
c Stream m a
n Stream m a
xs)
instance Semigroup (Stream m a) where
<> :: Stream m a -> Stream m a -> Stream m a
(<>) = Stream m a -> Stream m a -> Stream m a
forall (m :: * -> *) a. Stream m a -> Stream m a -> Stream m a
serial
instance Monoid (Stream m a) where
mempty :: Stream m a
mempty = Stream m a
forall (m :: * -> *) a. Stream m a
nil
mappend :: Stream m a -> Stream m a -> Stream m a
mappend = Stream m a -> Stream m a -> Stream m a
forall a. Semigroup a => a -> a -> a
(<>)
{-# INLINE_LATE mapFB #-}
mapFB :: forall b m a.
(b -> Stream m b -> Stream m b)
-> (a -> b)
-> a
-> Stream m b
-> Stream m b
mapFB :: (b -> Stream m b -> Stream m b)
-> (a -> b) -> a -> Stream m b -> Stream m b
mapFB b -> Stream m b -> Stream m b
c a -> b
f = \a
x Stream m b
ys -> b -> Stream m b -> Stream m b
c (a -> b
f a
x) Stream m b
ys
{-# RULES
"mapFB/mapFB" forall c f g. mapFB (mapFB c f) g = mapFB c (f . g)
"mapFB/id" forall c. mapFB c (\x -> x) = c
#-}
{-# INLINE map #-}
map :: (a -> b) -> Stream m a -> Stream m b
map :: (a -> b) -> Stream m a -> Stream m b
map a -> b
f Stream m a
xs = ((b -> Stream m b -> Stream m b) -> Stream m b -> Stream m b)
-> Stream m b
forall a (m :: * -> *).
((a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a)
-> Stream m a
buildS (\b -> Stream m b -> Stream m b
c Stream m b
n -> (a -> Stream m b -> Stream m b)
-> Stream m b -> Stream m a -> Stream m b
forall a (m :: * -> *) b.
(a -> Stream m b -> Stream m b)
-> Stream m b -> Stream m a -> Stream m b
foldrS ((b -> Stream m b -> Stream m b)
-> (a -> b) -> a -> Stream m b -> Stream m b
forall b (m :: * -> *) a.
(b -> Stream m b -> Stream m b)
-> (a -> b) -> a -> Stream m b -> Stream m b
mapFB b -> Stream m b -> Stream m b
c a -> b
f) Stream m b
n Stream m a
xs)
{-# INLINE_LATE mapMFB #-}
mapMFB :: Monad m => (m b -> t m b -> t m b) -> (a -> m b) -> m a -> t m b -> t m b
mapMFB :: (m b -> t m b -> t m b) -> (a -> m b) -> m a -> t m b -> t m b
mapMFB m b -> t m b -> t m b
c a -> m b
f m a
x = m b -> t m b -> t m b
c (m a
x m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> m b
f)
{-# RULES
"mapMFB/mapMFB" forall c f g. mapMFB (mapMFB c f) g = mapMFB c (f >=> g)
#-}
{-# INLINE mapMSerial #-}
mapMSerial :: Monad m => (a -> m b) -> Stream m a -> Stream m b
mapMSerial :: (a -> m b) -> Stream m a -> Stream m b
mapMSerial a -> m b
f Stream m a
xs = ((m b -> Stream m b -> Stream m b) -> Stream m b -> Stream m b)
-> Stream m b
forall (m :: * -> *) a.
Monad m =>
((m a -> Stream m a -> Stream m a) -> Stream m a -> Stream m a)
-> Stream m a
buildSM (\m b -> Stream m b -> Stream m b
c Stream m b
n -> (m a -> Stream m b -> Stream m b)
-> Stream m b -> Stream m a -> Stream m b
forall (m :: * -> *) a b.
Monad m =>
(m a -> Stream m b -> Stream m b)
-> Stream m b -> Stream m a -> Stream m b
foldrSMShared ((m b -> Stream m b -> Stream m b)
-> (a -> m b) -> m a -> Stream m b -> Stream m b
forall (m :: * -> *) b (t :: (* -> *) -> * -> *) a.
Monad m =>
(m b -> t m b -> t m b) -> (a -> m b) -> m a -> t m b -> t m b
mapMFB m b -> Stream m b -> Stream m b
c a -> m b
f) Stream m b
n Stream m a
xs)
{-# INLINE mapMWith #-}
mapMWith ::
(m b -> Stream m b -> Stream m b)
-> (a -> m b)
-> Stream m a
-> Stream m b
mapMWith :: (m b -> Stream m b -> Stream m b)
-> (a -> m b) -> Stream m a -> Stream m b
mapMWith m b -> Stream m b -> Stream m b
cns a -> m b
f = (a -> Stream m b -> Stream m b)
-> Stream m b -> Stream m a -> Stream m b
forall a (m :: * -> *) b.
(a -> Stream m b -> Stream m b)
-> Stream m b -> Stream m a -> Stream m b
foldrSShared (\a
x Stream m b
xs -> a -> m b
f a
x m b -> Stream m b -> Stream m b
`cns` Stream m b
xs) Stream m b
forall (m :: * -> *) a. Stream m a
nil
instance Monad m => Functor (Stream m) where
fmap :: (a -> b) -> Stream m a -> Stream m b
fmap = (a -> b) -> Stream m a -> Stream m b
forall a b (m :: * -> *). (a -> b) -> Stream m a -> Stream m b
map
instance MonadTrans Stream where
{-# INLINE lift #-}
lift :: m a -> Stream m a
lift = m a -> Stream m a
forall (m :: * -> *) a. Monad m => m a -> Stream m a
fromEffect
{-# INLINE unShare #-}
unShare :: Stream m a -> Stream m a
unShare :: Stream m a -> Stream m a
unShare Stream m a
x = (forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream 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 ->
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream 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
x
{-# INLINE apWith #-}
apWith ::
(Stream m b -> Stream m b -> Stream m b)
-> Stream m (a -> b)
-> Stream m a
-> Stream m b
apWith :: (Stream m b -> Stream m b -> Stream m b)
-> Stream m (a -> b) -> Stream m a -> Stream m b
apWith Stream m b -> Stream m b -> Stream m b
par Stream m (a -> b)
fstream Stream m a
stream = Stream m (a -> b) -> Stream m b
go1 Stream m (a -> b)
fstream
where
go1 :: Stream m (a -> b) -> Stream m b
go1 Stream m (a -> b)
m =
(forall r.
State Stream m b
-> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
-> Stream m b
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
State Stream m b
-> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
-> Stream m b)
-> (forall r.
State Stream m b
-> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
-> Stream m b
forall a b. (a -> b) -> a -> b
$ \State Stream m b
st b -> Stream m b -> m r
yld b -> m r
sng m r
stp ->
let foldShared :: Stream m b -> m r
foldShared = State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStreamShared State Stream m b
st b -> Stream m b -> m r
yld b -> m r
sng m r
stp
single :: (a -> b) -> m r
single a -> b
f = Stream m b -> m r
foldShared (Stream m b -> m r) -> Stream m b -> m r
forall a b. (a -> b) -> a -> b
$ Stream m b -> Stream m b
forall (m :: * -> *) a. Stream m a -> Stream m a
unShare ((a -> b) -> Stream m a -> Stream m b
forall a b (m :: * -> *). (a -> b) -> Stream m a -> Stream m b
go2 a -> b
f Stream m a
stream)
yieldk :: (a -> b) -> Stream m (a -> b) -> m r
yieldk a -> b
f Stream m (a -> b)
r = Stream m b -> m r
foldShared (Stream m b -> m r) -> Stream m b -> m r
forall a b. (a -> b) -> a -> b
$ Stream m b -> Stream m b
forall (m :: * -> *) a. Stream m a -> Stream m a
unShare ((a -> b) -> Stream m a -> Stream m b
forall a b (m :: * -> *). (a -> b) -> Stream m a -> Stream m b
go2 a -> b
f Stream m a
stream) Stream m b -> Stream m b -> Stream m b
`par` Stream m (a -> b) -> Stream m b
go1 Stream m (a -> b)
r
in State Stream m (a -> b)
-> ((a -> b) -> Stream m (a -> b) -> m r)
-> ((a -> b) -> m r)
-> m r
-> Stream m (a -> b)
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream (State Stream m b -> State Stream m (a -> b)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m b
st) (a -> b) -> Stream m (a -> b) -> m r
yieldk (a -> b) -> m r
single m r
stp Stream m (a -> b)
m
go2 :: (t -> a) -> Stream m t -> Stream m a
go2 t -> a
f Stream m t
m =
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream 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 :: t -> m r
single t
a = a -> m r
sng (t -> a
f t
a)
yieldk :: t -> Stream m t -> m r
yieldk t
a Stream m t
r = a -> Stream m a -> m r
yld (t -> a
f t
a) ((t -> a) -> Stream m t -> Stream m a
go2 t -> a
f Stream m t
r)
in State Stream m t
-> (t -> Stream m t -> m r)
-> (t -> m r)
-> m r
-> Stream m t
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream (State Stream m a -> State Stream m t
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m a
st) t -> Stream m t -> m r
yieldk t -> m r
single m r
stp Stream m t
m
{-# INLINE apSerial #-}
apSerial ::
Stream m (a -> b)
-> Stream m a
-> Stream m b
apSerial :: Stream m (a -> b) -> Stream m a -> Stream m b
apSerial Stream m (a -> b)
fstream Stream m a
stream = Stream m (a -> b) -> Stream m b
forall a. Stream m (a -> a) -> Stream m a
go1 Stream m (a -> b)
fstream
where
go1 :: Stream m (a -> a) -> Stream m a
go1 Stream m (a -> 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 (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream 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 foldShared :: Stream m a -> m r
foldShared = State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStreamShared State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp
single :: (a -> a) -> m r
single a -> a
f = Stream m a -> m r
foldShared (Stream m a -> m r) -> Stream m a -> m r
forall a b. (a -> b) -> a -> b
$ (a -> a) -> Stream m a -> Stream m a
forall a b (m :: * -> *). (a -> b) -> Stream m a -> Stream m b
go3 a -> a
f Stream m a
stream
yieldk :: (a -> a) -> Stream m (a -> a) -> m r
yieldk a -> a
f Stream m (a -> a)
r = Stream m a -> m r
foldShared (Stream m a -> m r) -> Stream m a -> m r
forall a b. (a -> b) -> a -> b
$ (a -> a) -> Stream m (a -> a) -> Stream m a -> Stream m a
go2 a -> a
f Stream m (a -> a)
r Stream m a
stream
in State Stream m (a -> a)
-> ((a -> a) -> Stream m (a -> a) -> m r)
-> ((a -> a) -> m r)
-> m r
-> Stream m (a -> a)
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream (State Stream m a -> State Stream m (a -> a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m a
st) (a -> a) -> Stream m (a -> a) -> m r
yieldk (a -> a) -> m r
single m r
stp Stream m (a -> a)
m
go2 :: (a -> a) -> Stream m (a -> a) -> Stream m a -> Stream m a
go2 a -> a
f Stream m (a -> a)
r1 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 (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream 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 foldShared :: Stream m a -> m r
foldShared = State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStreamShared State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp
stop :: m r
stop = Stream m a -> m r
foldShared (Stream m a -> m r) -> Stream m a -> m r
forall a b. (a -> b) -> a -> b
$ Stream m (a -> a) -> Stream m a
go1 Stream m (a -> a)
r1
single :: a -> m r
single a
a = a -> Stream m a -> m r
yld (a -> a
f a
a) (Stream m (a -> a) -> Stream m a
go1 Stream m (a -> a)
r1)
yieldk :: a -> Stream m a -> m r
yieldk a
a Stream m a
r = a -> Stream m a -> m r
yld (a -> a
f a
a) ((a -> a) -> Stream m (a -> a) -> Stream m a -> Stream m a
go2 a -> a
f Stream m (a -> a)
r1 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 (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream 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 -> Stream m a -> m r
yieldk a -> m r
single m r
stop Stream m a
m
go3 :: (t -> a) -> Stream m t -> Stream m a
go3 t -> a
f Stream m t
m =
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream 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 :: t -> m r
single t
a = a -> m r
sng (t -> a
f t
a)
yieldk :: t -> Stream m t -> m r
yieldk t
a Stream m t
r = a -> Stream m a -> m r
yld (t -> a
f t
a) ((t -> a) -> Stream m t -> Stream m a
go3 t -> a
f Stream m t
r)
in State Stream m t
-> (t -> Stream m t -> m r)
-> (t -> m r)
-> m r
-> Stream m t
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream (State Stream m a -> State Stream m t
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m a
st) t -> Stream m t -> m r
yieldk t -> m r
single m r
stp Stream m t
m
{-# INLINE apSerialDiscardFst #-}
apSerialDiscardFst ::
Stream m a
-> Stream m b
-> Stream m b
apSerialDiscardFst :: Stream m a -> Stream m b -> Stream m b
apSerialDiscardFst Stream m a
fstream Stream m b
stream = Stream m a -> Stream m b
forall p. Stream m p -> Stream m b
go1 Stream m a
fstream
where
go1 :: Stream m p -> Stream m b
go1 Stream m p
m =
(forall r.
State Stream m b
-> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
-> Stream m b
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
State Stream m b
-> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
-> Stream m b)
-> (forall r.
State Stream m b
-> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
-> Stream m b
forall a b. (a -> b) -> a -> b
$ \State Stream m b
st b -> Stream m b -> m r
yld b -> m r
sng m r
stp ->
let foldShared :: Stream m b -> m r
foldShared = State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStreamShared State Stream m b
st b -> Stream m b -> m r
yld b -> m r
sng m r
stp
single :: p -> m r
single p
_ = Stream m b -> m r
foldShared Stream m b
stream
yieldk :: p -> Stream m p -> m r
yieldk p
_ Stream m p
r = Stream m b -> m r
foldShared (Stream m b -> m r) -> Stream m b -> m r
forall a b. (a -> b) -> a -> b
$ Stream m p -> Stream m b -> Stream m b
go2 Stream m p
r Stream m b
stream
in State Stream m p
-> (p -> Stream m p -> m r)
-> (p -> m r)
-> m r
-> Stream m p
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream (State Stream m b -> State Stream m p
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m b
st) p -> Stream m p -> m r
forall p. p -> Stream m p -> m r
yieldk p -> m r
forall p. p -> m r
single m r
stp Stream m p
m
go2 :: Stream m p -> Stream m b -> Stream m b
go2 Stream m p
r1 Stream m b
m =
(forall r.
State Stream m b
-> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
-> Stream m b
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
State Stream m b
-> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
-> Stream m b)
-> (forall r.
State Stream m b
-> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
-> Stream m b
forall a b. (a -> b) -> a -> b
$ \State Stream m b
st b -> Stream m b -> m r
yld b -> m r
sng m r
stp ->
let foldShared :: Stream m b -> m r
foldShared = State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStreamShared State Stream m b
st b -> Stream m b -> m r
yld b -> m r
sng m r
stp
stop :: m r
stop = Stream m b -> m r
foldShared (Stream m b -> m r) -> Stream m b -> m r
forall a b. (a -> b) -> a -> b
$ Stream m p -> Stream m b
go1 Stream m p
r1
single :: b -> m r
single b
a = b -> Stream m b -> m r
yld b
a (Stream m p -> Stream m b
go1 Stream m p
r1)
yieldk :: b -> Stream m b -> m r
yieldk b
a Stream m b
r = b -> Stream m b -> m r
yld b
a (Stream m p -> Stream m b -> Stream m b
go2 Stream m p
r1 Stream m b
r)
in State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m b
st b -> Stream m b -> m r
yieldk b -> m r
single m r
stop Stream m b
m
{-# INLINE apSerialDiscardSnd #-}
apSerialDiscardSnd ::
Stream m a
-> Stream m b
-> Stream m a
apSerialDiscardSnd :: Stream m a -> Stream m b -> Stream m a
apSerialDiscardSnd Stream m a
fstream Stream m b
stream = Stream m a -> Stream m a
forall a. Stream m a -> Stream m a
go1 Stream m a
fstream
where
go1 :: Stream m a -> Stream m a
go1 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 (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream 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 foldShared :: Stream m a -> m r
foldShared = State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStreamShared State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp
single :: a -> m r
single a
f = Stream m a -> m r
foldShared (Stream m a -> m r) -> Stream m a -> m r
forall a b. (a -> b) -> a -> b
$ a -> Stream m b -> Stream m a
forall a (m :: * -> *) p. a -> Stream m p -> Stream m a
go3 a
f Stream m b
stream
yieldk :: a -> Stream m a -> m r
yieldk a
f Stream m a
r = Stream m a -> m r
foldShared (Stream m a -> m r) -> Stream m a -> m r
forall a b. (a -> b) -> a -> b
$ a -> Stream m a -> Stream m b -> Stream m a
go2 a
f Stream m a
r Stream m b
stream
in State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yieldk a -> m r
single m r
stp Stream m a
m
go2 :: a -> Stream m a -> Stream m b -> Stream m a
go2 a
f Stream m a
r1 Stream m b
m =
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream 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 foldShared :: Stream m a -> m r
foldShared = State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStreamShared State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp
stop :: m r
stop = Stream m a -> m r
foldShared (Stream m a -> m r) -> Stream m a -> m r
forall a b. (a -> b) -> a -> b
$ Stream m a -> Stream m a
go1 Stream m a
r1
single :: p -> m r
single p
_ = a -> Stream m a -> m r
yld a
f (Stream m a -> Stream m a
go1 Stream m a
r1)
yieldk :: p -> Stream m b -> m r
yieldk p
_ Stream m b
r = a -> Stream m a -> m r
yld a
f (a -> Stream m a -> Stream m b -> Stream m a
go2 a
f Stream m a
r1 Stream m b
r)
in State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream (State Stream m a -> State Stream m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m a
st) b -> Stream m b -> m r
forall p. p -> Stream m b -> m r
yieldk b -> m r
forall p. p -> m r
single m r
stop Stream m b
m
go3 :: a -> Stream m p -> Stream m a
go3 a
f Stream m p
m =
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream 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 :: p -> m r
single p
_ = a -> m r
sng a
f
yieldk :: p -> Stream m p -> m r
yieldk p
_ Stream m p
r = a -> Stream m a -> m r
yld a
f (a -> Stream m p -> Stream m a
go3 a
f Stream m p
r)
in State Stream m p
-> (p -> Stream m p -> m r)
-> (p -> m r)
-> m r
-> Stream m p
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream (State Stream m a -> State Stream m p
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m a
st) p -> Stream m p -> m r
forall p. p -> Stream m p -> m r
yieldk p -> m r
forall p. p -> m r
single m r
stp Stream m p
m
{-# INLINE bindWith #-}
bindWith ::
(Stream m b -> Stream m b -> Stream m b)
-> Stream m a
-> (a -> Stream m b)
-> Stream m b
bindWith :: (Stream m b -> Stream m b -> Stream m b)
-> Stream m a -> (a -> Stream m b) -> Stream m b
bindWith Stream m b -> Stream m b -> Stream m b
par Stream m a
m1 a -> Stream m b
f = Stream m a -> Stream m b
go Stream m a
m1
where
go :: Stream m a -> Stream m b
go Stream m a
m =
(forall r.
State Stream m b
-> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
-> Stream m b
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
State Stream m b
-> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
-> Stream m b)
-> (forall r.
State Stream m b
-> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
-> Stream m b
forall a b. (a -> b) -> a -> b
$ \State Stream m b
st b -> Stream m b -> m r
yld b -> m r
sng m r
stp ->
let foldShared :: Stream m b -> m r
foldShared = State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStreamShared State Stream m b
st b -> Stream m b -> m r
yld b -> m r
sng m r
stp
single :: a -> m r
single a
a = Stream m b -> m r
foldShared (Stream m b -> m r) -> Stream m b -> m r
forall a b. (a -> b) -> a -> b
$ Stream m b -> Stream m b
forall (m :: * -> *) a. Stream m a -> Stream m a
unShare (a -> Stream m b
f a
a)
yieldk :: a -> Stream m a -> m r
yieldk a
a Stream m a
r = Stream m b -> m r
foldShared (Stream m b -> m r) -> Stream m b -> m r
forall a b. (a -> b) -> a -> b
$ Stream m b -> Stream m b
forall (m :: * -> *) a. Stream m a -> Stream m a
unShare (a -> Stream m b
f a
a) Stream m b -> Stream m b -> Stream m b
`par` Stream m a -> Stream m b
go 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 (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream 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 -> Stream m a -> m r
yieldk a -> m r
single m r
stp Stream m a
m
{-# INLINE concatMapWith #-}
concatMapWith
::
(Stream m b -> Stream m b -> Stream m b)
-> (a -> Stream m b)
-> Stream m a
-> Stream m b
concatMapWith :: (Stream m b -> Stream m b -> Stream m b)
-> (a -> Stream m b) -> Stream m a -> Stream m b
concatMapWith Stream m b -> Stream m b -> Stream m b
par a -> Stream m b
f Stream m a
xs = (Stream m b -> Stream m b -> Stream m b)
-> Stream m a -> (a -> Stream m b) -> Stream m b
forall (m :: * -> *) b a.
(Stream m b -> Stream m b -> Stream m b)
-> Stream m a -> (a -> Stream m b) -> Stream m b
bindWith Stream m b -> Stream m b -> Stream m b
par Stream m a
xs a -> Stream m b
f
{-# INLINE concatMap #-}
concatMap :: (a -> Stream m b) -> Stream m a -> Stream m b
concatMap :: (a -> Stream m b) -> Stream m a -> Stream m b
concatMap = (Stream m b -> Stream m b -> Stream m b)
-> (a -> Stream m b) -> Stream m a -> Stream m b
forall (m :: * -> *) b a.
(Stream m b -> Stream m b -> Stream m b)
-> (a -> Stream m b) -> Stream m a -> Stream m b
concatMapWith Stream m b -> Stream m b -> Stream m b
forall (m :: * -> *) a. Stream m a -> Stream m a -> Stream m a
serial
{-# INLINE concatPairsWith #-}
concatPairsWith
::
(Stream m b -> Stream m b -> Stream m b)
-> (a -> Stream m b)
-> Stream m a
-> Stream m b
concatPairsWith :: (Stream m b -> Stream m b -> Stream m b)
-> (a -> Stream m b) -> Stream m a -> Stream m b
concatPairsWith Stream m b -> Stream m b -> Stream m b
combine a -> Stream m b
f Stream m a
str = Stream m (Stream m b) -> Stream m b
go (Stream m a -> Stream m (Stream m b)
forall (m :: * -> *). Stream m a -> Stream m (Stream m b)
leafPairs Stream m a
str)
where
go :: Stream m (Stream m b) -> Stream m b
go Stream m (Stream m b)
stream =
(forall r.
State Stream m b
-> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
-> Stream m b
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
State Stream m b
-> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
-> Stream m b)
-> (forall r.
State Stream m b
-> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
-> Stream m b
forall a b. (a -> b) -> a -> b
$ \State Stream m b
st b -> Stream m b -> m r
yld b -> m r
sng m r
stp ->
let foldShared :: Stream m b -> m r
foldShared = State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStreamShared State Stream m b
st b -> Stream m b -> m r
yld b -> m r
sng m r
stp
single :: Stream m b -> m r
single Stream m b
a = Stream m b -> m r
foldShared (Stream m b -> m r) -> Stream m b -> m r
forall a b. (a -> b) -> a -> b
$ Stream m b -> Stream m b
forall (m :: * -> *) a. Stream m a -> Stream m a
unShare Stream m b
a
yieldk :: Stream m b -> Stream m (Stream m b) -> m r
yieldk Stream m b
a Stream m (Stream m b)
r = Stream m b -> m r
foldShared (Stream m b -> m r) -> Stream m b -> m r
forall a b. (a -> b) -> a -> b
$ Stream m b -> Stream m (Stream m b) -> Stream m b
go1 Stream m b
a Stream m (Stream m b)
r
in State Stream m (Stream m b)
-> (Stream m b -> Stream m (Stream m b) -> m r)
-> (Stream m b -> m r)
-> m r
-> Stream m (Stream m b)
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream (State Stream m b -> State Stream m (Stream m b)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m b
st) Stream m b -> Stream m (Stream m b) -> m r
yieldk Stream m b -> m r
single m r
stp Stream m (Stream m b)
stream
go1 :: Stream m b -> Stream m (Stream m b) -> Stream m b
go1 Stream m b
a1 Stream m (Stream m b)
stream =
(forall r.
State Stream m b
-> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
-> Stream m b
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
State Stream m b
-> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
-> Stream m b)
-> (forall r.
State Stream m b
-> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
-> Stream m b
forall a b. (a -> b) -> a -> b
$ \State Stream m b
st b -> Stream m b -> m r
yld b -> m r
sng m r
stp ->
let foldShared :: Stream m b -> m r
foldShared = State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStreamShared State Stream m b
st b -> Stream m b -> m r
yld b -> m r
sng m r
stp
stop :: m r
stop = Stream m b -> m r
foldShared (Stream m b -> m r) -> Stream m b -> m r
forall a b. (a -> b) -> a -> b
$ Stream m b -> Stream m b
forall (m :: * -> *) a. Stream m a -> Stream m a
unShare Stream m b
a1
single :: Stream m b -> m r
single Stream m b
a = Stream m b -> m r
foldShared (Stream m b -> m r) -> Stream m b -> m r
forall a b. (a -> b) -> a -> b
$ Stream m b -> Stream m b
forall (m :: * -> *) a. Stream m a -> Stream m a
unShare Stream m b
a1 Stream m b -> Stream m b -> Stream m b
`combine` Stream m b
a
yieldk :: Stream m b -> Stream m (Stream m b) -> m r
yieldk Stream m b
a Stream m (Stream m b)
r =
Stream m b -> m r
foldShared (Stream m b -> m r) -> Stream m b -> m r
forall a b. (a -> b) -> a -> b
$ Stream m (Stream m b) -> Stream m b
go (Stream m (Stream m b) -> Stream m b)
-> Stream m (Stream m b) -> Stream m b
forall a b. (a -> b) -> a -> b
$ Stream m b -> Stream m b -> Stream m b
combine Stream m b
a1 Stream m b
a Stream m b -> Stream m (Stream m b) -> Stream m (Stream m b)
forall a (m :: * -> *). a -> Stream m a -> Stream m a
`cons` Stream m (Stream m b) -> Stream m (Stream m b)
forall (m :: * -> *).
Stream m (Stream m b) -> Stream m (Stream m b)
nonLeafPairs Stream m (Stream m b)
r
in State Stream m (Stream m b)
-> (Stream m b -> Stream m (Stream m b) -> m r)
-> (Stream m b -> m r)
-> m r
-> Stream m (Stream m b)
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream (State Stream m b -> State Stream m (Stream m b)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m b
st) Stream m b -> Stream m (Stream m b) -> m r
yieldk Stream m b -> m r
single m r
stop Stream m (Stream m b)
stream
leafPairs :: Stream m a -> Stream m (Stream m b)
leafPairs Stream m a
stream =
(forall r.
State Stream m (Stream m b)
-> (Stream m b -> Stream m (Stream m b) -> m r)
-> (Stream m b -> m r)
-> m r
-> m r)
-> Stream m (Stream m b)
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
State Stream m (Stream m b)
-> (Stream m b -> Stream m (Stream m b) -> m r)
-> (Stream m b -> m r)
-> m r
-> m r)
-> Stream m (Stream m b))
-> (forall r.
State Stream m (Stream m b)
-> (Stream m b -> Stream m (Stream m b) -> m r)
-> (Stream m b -> m r)
-> m r
-> m r)
-> Stream m (Stream m b)
forall a b. (a -> b) -> a -> b
$ \State Stream m (Stream m b)
st Stream m b -> Stream m (Stream m b) -> m r
yld Stream m b -> m r
sng m r
stp ->
let foldShared :: Stream m (Stream m b) -> m r
foldShared = State Stream m (Stream m b)
-> (Stream m b -> Stream m (Stream m b) -> m r)
-> (Stream m b -> m r)
-> m r
-> Stream m (Stream m b)
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStreamShared State Stream m (Stream m b)
st Stream m b -> Stream m (Stream m b) -> m r
yld Stream m b -> m r
sng m r
stp
single :: a -> m r
single a
a = Stream m b -> m r
sng (a -> Stream m b
f a
a)
yieldk :: a -> Stream m a -> m r
yieldk a
a Stream m a
r = Stream m (Stream m b) -> m r
foldShared (Stream m (Stream m b) -> m r) -> Stream m (Stream m b) -> m r
forall a b. (a -> b) -> a -> b
$ a -> Stream m a -> Stream m (Stream m b)
leafPairs1 a
a 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 (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream (State Stream m (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 (Stream m b)
st) a -> Stream m a -> m r
yieldk a -> m r
single m r
stp Stream m a
stream
leafPairs1 :: a -> Stream m a -> Stream m (Stream m b)
leafPairs1 a
a1 Stream m a
stream =
(forall r.
State Stream m (Stream m b)
-> (Stream m b -> Stream m (Stream m b) -> m r)
-> (Stream m b -> m r)
-> m r
-> m r)
-> Stream m (Stream m b)
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
State Stream m (Stream m b)
-> (Stream m b -> Stream m (Stream m b) -> m r)
-> (Stream m b -> m r)
-> m r
-> m r)
-> Stream m (Stream m b))
-> (forall r.
State Stream m (Stream m b)
-> (Stream m b -> Stream m (Stream m b) -> m r)
-> (Stream m b -> m r)
-> m r
-> m r)
-> Stream m (Stream m b)
forall a b. (a -> b) -> a -> b
$ \State Stream m (Stream m b)
st Stream m b -> Stream m (Stream m b) -> m r
yld Stream m b -> m r
sng m r
_ ->
let stop :: m r
stop = Stream m b -> m r
sng (a -> Stream m b
f a
a1)
single :: a -> m r
single a
a = Stream m b -> m r
sng (a -> Stream m b
f a
a1 Stream m b -> Stream m b -> Stream m b
`combine` a -> Stream m b
f a
a)
yieldk :: a -> Stream m a -> m r
yieldk a
a Stream m a
r = Stream m b -> Stream m (Stream m b) -> m r
yld (a -> Stream m b
f a
a1 Stream m b -> Stream m b -> Stream m b
`combine` a -> Stream m b
f a
a) (Stream m (Stream m b) -> m r) -> Stream m (Stream m b) -> m r
forall a b. (a -> b) -> a -> b
$ Stream m a -> Stream m (Stream m b)
leafPairs 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 (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream (State Stream m (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 (Stream m b)
st) a -> Stream m a -> m r
yieldk a -> m r
single m r
stop Stream m a
stream
nonLeafPairs :: Stream m (Stream m b) -> Stream m (Stream m b)
nonLeafPairs Stream m (Stream m b)
stream =
(forall r.
State Stream m (Stream m b)
-> (Stream m b -> Stream m (Stream m b) -> m r)
-> (Stream m b -> m r)
-> m r
-> m r)
-> Stream m (Stream m b)
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
State Stream m (Stream m b)
-> (Stream m b -> Stream m (Stream m b) -> m r)
-> (Stream m b -> m r)
-> m r
-> m r)
-> Stream m (Stream m b))
-> (forall r.
State Stream m (Stream m b)
-> (Stream m b -> Stream m (Stream m b) -> m r)
-> (Stream m b -> m r)
-> m r
-> m r)
-> Stream m (Stream m b)
forall a b. (a -> b) -> a -> b
$ \State Stream m (Stream m b)
st Stream m b -> Stream m (Stream m b) -> m r
yld Stream m b -> m r
sng m r
stp ->
let foldShared :: Stream m (Stream m b) -> m r
foldShared = State Stream m (Stream m b)
-> (Stream m b -> Stream m (Stream m b) -> m r)
-> (Stream m b -> m r)
-> m r
-> Stream m (Stream m b)
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStreamShared State Stream m (Stream m b)
st Stream m b -> Stream m (Stream m b) -> m r
yld Stream m b -> m r
sng m r
stp
single :: Stream m b -> m r
single Stream m b
a = Stream m b -> m r
sng Stream m b
a
yieldk :: Stream m b -> Stream m (Stream m b) -> m r
yieldk Stream m b
a Stream m (Stream m b)
r = Stream m (Stream m b) -> m r
foldShared (Stream m (Stream m b) -> m r) -> Stream m (Stream m b) -> m r
forall a b. (a -> b) -> a -> b
$ Stream m b -> Stream m (Stream m b) -> Stream m (Stream m b)
nonLeafPairs1 Stream m b
a Stream m (Stream m b)
r
in State Stream m (Stream m b)
-> (Stream m b -> Stream m (Stream m b) -> m r)
-> (Stream m b -> m r)
-> m r
-> Stream m (Stream m b)
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream (State Stream m (Stream m b) -> State Stream m (Stream m b)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m (Stream m b)
st) Stream m b -> Stream m (Stream m b) -> m r
yieldk Stream m b -> m r
single m r
stp Stream m (Stream m b)
stream
nonLeafPairs1 :: Stream m b -> Stream m (Stream m b) -> Stream m (Stream m b)
nonLeafPairs1 Stream m b
a1 Stream m (Stream m b)
stream =
(forall r.
State Stream m (Stream m b)
-> (Stream m b -> Stream m (Stream m b) -> m r)
-> (Stream m b -> m r)
-> m r
-> m r)
-> Stream m (Stream m b)
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
State Stream m (Stream m b)
-> (Stream m b -> Stream m (Stream m b) -> m r)
-> (Stream m b -> m r)
-> m r
-> m r)
-> Stream m (Stream m b))
-> (forall r.
State Stream m (Stream m b)
-> (Stream m b -> Stream m (Stream m b) -> m r)
-> (Stream m b -> m r)
-> m r
-> m r)
-> Stream m (Stream m b)
forall a b. (a -> b) -> a -> b
$ \State Stream m (Stream m b)
st Stream m b -> Stream m (Stream m b) -> m r
yld Stream m b -> m r
sng m r
_ ->
let stop :: m r
stop = Stream m b -> m r
sng Stream m b
a1
single :: Stream m b -> m r
single Stream m b
a = Stream m b -> m r
sng (Stream m b
a1 Stream m b -> Stream m b -> Stream m b
`combine` Stream m b
a)
yieldk :: Stream m b -> Stream m (Stream m b) -> m r
yieldk Stream m b
a Stream m (Stream m b)
r = Stream m b -> Stream m (Stream m b) -> m r
yld (Stream m b
a1 Stream m b -> Stream m b -> Stream m b
`combine` Stream m b
a) (Stream m (Stream m b) -> m r) -> Stream m (Stream m b) -> m r
forall a b. (a -> b) -> a -> b
$ Stream m (Stream m b) -> Stream m (Stream m b)
nonLeafPairs Stream m (Stream m b)
r
in State Stream m (Stream m b)
-> (Stream m b -> Stream m (Stream m b) -> m r)
-> (Stream m b -> m r)
-> m r
-> Stream m (Stream m b)
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream (State Stream m (Stream m b) -> State Stream m (Stream m b)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m (Stream m b)
st) Stream m b -> Stream m (Stream m b) -> m r
yieldk Stream m b -> m r
single m r
stop Stream m (Stream m b)
stream
instance Monad m => Applicative (Stream m) where
{-# INLINE pure #-}
pure :: a -> Stream m a
pure = a -> Stream m a
forall a (m :: * -> *). a -> Stream m a
fromPure
{-# INLINE (<*>) #-}
<*> :: Stream m (a -> b) -> Stream m a -> Stream m b
(<*>) = Stream m (a -> b) -> Stream m a -> Stream m b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap
instance Monad m => Monad (Stream m) where
{-# INLINE return #-}
return :: a -> Stream m a
return = a -> Stream m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
{-# INLINE (>>=) #-}
>>= :: Stream m a -> (a -> Stream m b) -> Stream m b
(>>=) = ((a -> Stream m b) -> Stream m a -> Stream m b)
-> Stream m a -> (a -> Stream m b) -> Stream m b
forall a b c. (a -> b -> c) -> b -> a -> c
flip (a -> Stream m b) -> Stream m a -> Stream m b
forall a (m :: * -> *) b.
(a -> Stream m b) -> Stream m a -> Stream m b
concatMap
{-# 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 (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream 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 (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream 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
{-# INLINE unfoldr #-}
unfoldr :: (b -> Maybe (a, b)) -> b -> Stream m a
unfoldr :: (b -> Maybe (a, b)) -> b -> Stream m a
unfoldr b -> Maybe (a, b)
next b
s0 = (forall b. (a -> b -> b) -> b -> b) -> Stream m a
forall (m :: * -> *) a.
(forall b. (a -> b -> b) -> b -> b) -> Stream m a
build ((forall b. (a -> b -> b) -> b -> b) -> Stream m a)
-> (forall b. (a -> b -> b) -> b -> b) -> Stream 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 unfoldrMWith #-}
unfoldrMWith :: Monad m =>
(m a -> Stream m a -> Stream m a)
-> (b -> m (Maybe (a, b)))
-> b
-> Stream m a
unfoldrMWith :: (m a -> Stream m a -> Stream m a)
-> (b -> m (Maybe (a, b))) -> b -> Stream m a
unfoldrMWith m a -> Stream m a -> Stream m a
cns b -> m (Maybe (a, b))
step = b -> Stream m a
go
where
go :: b -> Stream m a
go b
s = (m a -> Stream m a -> Stream m a)
-> (forall r. (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
Monad m =>
(m a -> Stream m a -> Stream m a)
-> (forall r. (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
sharedMWith m a -> Stream m a -> Stream m a
cns ((forall r. (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a)
-> (forall r. (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall a b. (a -> b) -> a -> b
$ \a -> Stream 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 -> Stream m a -> m r
yld a
a (b -> Stream m a
go b
b)
Maybe (a, b)
Nothing -> m r
stp
{-# INLINE repeat #-}
repeat :: a -> Stream m a
repeat :: a -> Stream m a
repeat a
a = let x :: Stream m a
x = a -> Stream m a -> Stream m a
forall a (m :: * -> *). a -> Stream m a -> Stream m a
cons a
a Stream m a
x in Stream m a
forall (m :: * -> *). Stream m a
x
repeatMWith :: (m a -> t m a -> t m a) -> m a -> t m a
repeatMWith :: (m a -> t m a -> t m a) -> m a -> t m a
repeatMWith m a -> t m a -> t m a
cns = 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
`cns` m a -> t m a
go m a
m
{-# INLINE replicateMWith #-}
replicateMWith :: (m a -> Stream m a -> Stream m a) -> Int -> m a -> Stream m a
replicateMWith :: (m a -> Stream m a -> Stream m a) -> Int -> m a -> Stream m a
replicateMWith m a -> Stream m a -> Stream m a
cns Int
n m a
m = Int -> Stream m a
forall t. (Ord t, Num t) => t -> Stream m a
go Int
n
where
go :: t -> Stream m a
go t
cnt = if t
cnt t -> t -> Bool
forall a. Ord a => a -> a -> Bool
<= t
0 then Stream m a
forall (m :: * -> *) a. Stream m a
nil else m a
m m a -> Stream m a -> Stream m a
`cns` t -> Stream m a
go (t
cnt t -> t -> t
forall a. Num a => a -> a -> a
- t
1)
{-# INLINE fromIndicesMWith #-}
fromIndicesMWith ::
(m a -> Stream m a -> Stream m a) -> (Int -> m a) -> Stream m a
fromIndicesMWith :: (m a -> Stream m a -> Stream m a) -> (Int -> m a) -> Stream m a
fromIndicesMWith m a -> Stream m a -> Stream m a
cns Int -> m a
gen = Int -> Stream m a
go Int
0
where
go :: Int -> Stream m a
go Int
i = (forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream 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
stp a -> m r
sng m r
yld -> do
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStreamShared State Stream m a
st a -> Stream m a -> m r
stp a -> m r
sng m r
yld (Int -> m a
gen Int
i m a -> Stream m a -> Stream m a
`cns` Int -> Stream m a
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1))
{-# INLINE iterateMWith #-}
iterateMWith :: Monad m =>
(m a -> Stream m a -> Stream m a) -> (a -> m a) -> m a -> Stream m a
iterateMWith :: (m a -> Stream m a -> Stream m a)
-> (a -> m a) -> m a -> Stream m a
iterateMWith m a -> Stream m a -> Stream m a
cns a -> m a
step = m a -> Stream m a
go
where
go :: m a -> Stream m a
go m a
s = (forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream 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
stp a -> m r
sng m r
yld -> do
!a
next <- m a
s
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStreamShared State Stream m a
st a -> Stream 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 -> Stream m a -> Stream m a
`cns` m a -> Stream m a
go (a -> m a
step a
next))
{-# INLINE headPartial #-}
headPartial :: Monad m => Stream m a -> m a
headPartial :: Stream m a -> m a
headPartial = (a -> m a -> m a) -> m a -> Stream m a -> m a
forall a (m :: * -> *) b.
(a -> m b -> m b) -> m b -> Stream 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 :: Stream m a -> Stream m a
tailPartial :: Stream m a -> Stream m a
tailPartial 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 (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream 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 :: 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 -> Stream m a -> m r
yieldk p
_ Stream m a
r = State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream 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
r
in State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
forall p. p -> Stream m a -> m r
yieldk a -> m r
forall p. p -> m r
single m r
forall a. a
stop Stream m a
m
{-# INLINE mfix #-}
mfix :: Monad m => (m a -> Stream m a) -> Stream m a
mfix :: (m a -> Stream m a) -> Stream m a
mfix m a -> Stream m a
f = (forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream 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 a
a = State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream 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 -> m r) -> Stream m a -> m r
forall a b. (a -> b) -> a -> b
$ a
a a -> Stream m a -> Stream m a
forall a (m :: * -> *). a -> Stream m a -> Stream m a
`cons` Stream m a
ys
yieldk :: a -> p -> m r
yieldk a
a p
_ = State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream 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 -> m r) -> Stream m a -> m r
forall a b. (a -> b) -> a -> b
$ a
a a -> Stream m a -> Stream m a
forall a (m :: * -> *). a -> Stream m a -> Stream m a
`cons` Stream m a
ys
in State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
forall p. a -> p -> m r
yieldk a -> m r
single m r
stp Stream m a
xs
where
xs :: Stream m a
xs = (Stream m a -> Stream m a) -> Stream m a
forall a. (a -> a) -> a
fix (m a -> Stream m a
f (m a -> Stream m a)
-> (Stream m a -> m a) -> Stream m a -> Stream m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Stream m a -> m a
forall (m :: * -> *) a. Monad m => Stream m a -> m a
headPartial)
ys :: Stream m a
ys = (m a -> Stream m a) -> Stream m a
forall (m :: * -> *) a.
Monad m =>
(m a -> Stream m a) -> Stream m a
mfix (Stream m a -> Stream m a
forall (m :: * -> *) a. Stream m a -> Stream m a
tailPartial (Stream m a -> Stream m a)
-> (m a -> Stream m a) -> m a -> Stream m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m a -> Stream m a
f)
{-# INLINE fromFoldable #-}
fromFoldable :: Foldable f => f a -> Stream m a
fromFoldable :: f a -> Stream m a
fromFoldable = (a -> Stream m a -> Stream m a) -> Stream m a -> f a -> Stream m a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Prelude.foldr a -> Stream m a -> Stream m a
forall a (m :: * -> *). a -> Stream m a -> Stream m a
cons Stream m a
forall (m :: * -> *) a. Stream m a
nil
{-# INLINE fromFoldableM #-}
fromFoldableM :: (Foldable f, Monad m) => f (m a) -> Stream m a
fromFoldableM :: f (m a) -> Stream m a
fromFoldableM = (m a -> Stream m a -> Stream m a)
-> Stream m a -> f (m a) -> Stream m a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Prelude.foldr m a -> Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => m a -> Stream m a -> Stream m a
consM Stream m a
forall (m :: * -> *) a. Stream m a
nil
{-# INLINE uncons #-}
uncons :: Applicative m => Stream m a -> m (Maybe (a, Stream m a))
uncons :: Stream m a -> m (Maybe (a, Stream m a))
uncons Stream m a
m =
let stop :: m (Maybe a)
stop = Maybe a -> m (Maybe a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe a
forall a. Maybe a
Nothing
single :: a -> f (Maybe (a, Stream m a))
single a
a = Maybe (a, Stream m a) -> f (Maybe (a, Stream m a))
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((a, Stream m a) -> Maybe (a, Stream m a)
forall a. a -> Maybe a
Just (a
a, Stream m a
forall (m :: * -> *) a. Stream m a
nil))
yieldk :: a -> b -> f (Maybe (a, b))
yieldk a
a b
r = Maybe (a, b) -> f (Maybe (a, b))
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((a, b) -> Maybe (a, b)
forall a. a -> Maybe a
Just (a
a, b
r))
in State Stream m a
-> (a -> Stream m a -> m (Maybe (a, Stream m a)))
-> (a -> m (Maybe (a, Stream m a)))
-> m (Maybe (a, Stream m a))
-> Stream m a
-> m (Maybe (a, Stream m a))
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> Stream m a -> m (Maybe (a, Stream m a))
forall (f :: * -> *) a b.
Applicative f =>
a -> b -> f (Maybe (a, b))
yieldk a -> m (Maybe (a, Stream m a))
forall (f :: * -> *) a (m :: * -> *) a.
Applicative f =>
a -> f (Maybe (a, Stream m a))
single m (Maybe (a, Stream m a))
forall a. m (Maybe a)
stop Stream m a
m
{-# INLINE tail #-}
tail :: Applicative m => Stream m a -> m (Maybe (Stream m a))
tail :: Stream m a -> m (Maybe (Stream m a))
tail =
let stop :: m (Maybe a)
stop = Maybe a -> m (Maybe a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe a
forall a. Maybe a
Nothing
single :: p -> f (Maybe (Stream m a))
single p
_ = Maybe (Stream m a) -> f (Maybe (Stream m a))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (Stream m a) -> f (Maybe (Stream m a)))
-> Maybe (Stream m a) -> f (Maybe (Stream m a))
forall a b. (a -> b) -> a -> b
$ Stream m a -> Maybe (Stream m a)
forall a. a -> Maybe a
Just Stream m a
forall (m :: * -> *) a. Stream m a
nil
yieldk :: p -> a -> f (Maybe a)
yieldk p
_ a
r = Maybe a -> f (Maybe a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe a -> f (Maybe a)) -> Maybe a -> f (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 -> Stream m a -> m (Maybe (Stream m a)))
-> (a -> m (Maybe (Stream m a)))
-> m (Maybe (Stream m a))
-> Stream m a
-> m (Maybe (Stream m a))
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> Stream m a -> m (Maybe (Stream m a))
forall (f :: * -> *) p a. Applicative f => p -> a -> f (Maybe a)
yieldk a -> m (Maybe (Stream m a))
forall (f :: * -> *) p (m :: * -> *) a.
Applicative f =>
p -> f (Maybe (Stream m a))
single m (Maybe (Stream m a))
forall a. m (Maybe a)
stop
{-# INLINE init #-}
init :: Applicative m => Stream m a -> m (Maybe (Stream m a))
init :: Stream m a -> m (Maybe (Stream m a))
init = Stream m a -> m (Maybe (Stream m a))
forall (m :: * -> *) p.
Applicative m =>
Stream m p -> m (Maybe (Stream m p))
go1
where
go1 :: Stream m p -> m (Maybe (Stream m p))
go1 Stream m p
m1 = do
(\case
Maybe (p, Stream m p)
Nothing -> Maybe (Stream m p)
forall a. Maybe a
Nothing
Just (p
h, Stream m p
t) -> Stream m p -> Maybe (Stream m p)
forall a. a -> Maybe a
Just (Stream m p -> Maybe (Stream m p))
-> Stream m p -> Maybe (Stream m p)
forall a b. (a -> b) -> a -> b
$ p -> Stream m p -> Stream m p
forall a (m :: * -> *). a -> Stream m a -> Stream m a
go p
h Stream m p
t) (Maybe (p, Stream m p) -> Maybe (Stream m p))
-> m (Maybe (p, Stream m p)) -> m (Maybe (Stream m p))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Stream m p -> m (Maybe (p, Stream m p))
forall (m :: * -> *) a.
Applicative m =>
Stream m a -> m (Maybe (a, Stream m a))
uncons Stream m p
m1
go :: p -> Stream m p -> Stream m p
go p
p Stream m p
m1 = (forall r.
State Stream m p
-> (p -> Stream m p -> m r) -> (p -> m r) -> m r -> m r)
-> Stream m p
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
State Stream m p
-> (p -> Stream m p -> m r) -> (p -> m r) -> m r -> m r)
-> Stream m p)
-> (forall r.
State Stream m p
-> (p -> Stream m p -> m r) -> (p -> m r) -> m r -> m r)
-> Stream m p
forall a b. (a -> b) -> a -> b
$ \State Stream m p
_ p -> Stream 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 -> Stream m p -> m r
yieldk p
a Stream m p
x = p -> Stream m p -> m r
yld p
p (Stream m p -> m r) -> Stream m p -> m r
forall a b. (a -> b) -> a -> b
$ p -> Stream m p -> Stream m p
go p
a Stream m p
x
in State Stream m p
-> (p -> Stream m p -> m r)
-> (p -> m r)
-> m r
-> Stream m p
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m p
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState p -> Stream m p -> m r
yieldk p -> m r
forall p. p -> m r
single m r
stp Stream m p
m1
{-# INLINE foldlS #-}
foldlS ::
(Stream m b -> a -> Stream m b) -> Stream m b -> Stream m a -> Stream m b
foldlS :: (Stream m b -> a -> Stream m b)
-> Stream m b -> Stream m a -> Stream m b
foldlS Stream m b -> a -> Stream m b
step = Stream m b -> Stream m a -> Stream m b
go
where
go :: Stream m b -> Stream m a -> Stream m b
go Stream m b
acc Stream m a
rest = (forall r.
State Stream m b
-> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
-> Stream m b
forall (m :: * -> *) a.
(forall r.
State Stream m a
-> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
State Stream m b
-> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
-> Stream m b)
-> (forall r.
State Stream m b
-> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
-> Stream m b
forall a b. (a -> b) -> a -> b
$ \State Stream m b
st b -> Stream m b -> m r
yld b -> m r
sng m r
stp ->
let run :: Stream m b -> m r
run Stream m b
x = State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m b
st b -> Stream m b -> m r
yld b -> m r
sng m r
stp Stream m b
x
stop :: m r
stop = Stream m b -> m r
run Stream m b
acc
single :: a -> m r
single a
a = Stream m b -> m r
run (Stream m b -> m r) -> Stream m b -> m r
forall a b. (a -> b) -> a -> b
$ Stream m b -> a -> Stream m b
step Stream m b
acc a
a
yieldk :: a -> Stream m a -> m r
yieldk a
a Stream m a
r = Stream m b -> m r
run (Stream m b -> m r) -> Stream m b -> m r
forall a b. (a -> b) -> a -> b
$ Stream m b -> Stream m a -> Stream m b
go (Stream m b -> a -> Stream m b
step Stream m b
acc a
a) 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 (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream 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 -> Stream m a -> m r
yieldk a -> m r
single m r
stop Stream m a
rest
{-# INLINE reverse #-}
reverse :: Stream m a -> Stream m a
reverse :: Stream m a -> Stream m a
reverse = (Stream m a -> a -> Stream m a)
-> Stream m a -> Stream m a -> Stream m a
forall (m :: * -> *) b a.
(Stream m b -> a -> Stream m b)
-> Stream m b -> Stream m a -> Stream m b
foldlS ((a -> Stream m a -> Stream m a) -> Stream m a -> a -> Stream m a
forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> Stream m a -> Stream m a
forall a (m :: * -> *). a -> Stream m a -> Stream m a
cons) Stream m a
forall (m :: * -> *) a. Stream m a
nil