{-# LANGUAGE CPP #-}
{-# LANGUAGE UndecidableInstances #-}
module Streamly.Internal.Data.Stream.Type
(
Step (..)
, Stream (Stream, UnStream)
, CrossStream
, unCross
, mkCross
, fromStreamK
, toStreamK
, unfold
, nilM
, consM
, fromPure
, fromEffect
, Streamly.Internal.Data.Stream.Type.fromList
, uncons
, Streamly.Internal.Data.Stream.Type.fold
, foldBreak
, foldAddLazy
, foldAdd
, foldEither
, Streamly.Internal.Data.Stream.Type.foldl'
, foldlM'
, foldlx'
, foldlMx'
, foldrM
, foldrMx
, Streamly.Internal.Data.Stream.Type.foldr
, foldrS
, drain
, Streamly.Internal.Data.Stream.Type.toList
, map
, mapM
, take
, takeWhile
, takeWhileM
, takeEndBy
, takeEndByM
, zipWithM
, zipWith
, crossApply
, crossApplyFst
, crossApplySnd
, crossWith
, cross
, ConcatMapUState (..)
, unfoldMany
, concatEffect
, concatMap
, concatMapM
, concat
, unfoldIterateDfs
, unfoldIterateBfs
, unfoldIterateBfsRev
, concatIterateScan
, concatIterateDfs
, concatIterateBfs
, concatIterateBfsRev
, FoldMany (..)
, FoldManyPost (..)
, foldMany
, foldManyPost
, groupsOf
, refoldMany
, reduceIterateBfs
, foldIterateBfs
, eqBy
, cmpBy
)
where
#include "inline.hs"
#if !MIN_VERSION_base(4,18,0)
import Control.Applicative (liftA2)
#endif
import Control.Monad.Catch (MonadThrow, throwM)
import Control.Monad.Trans.Class (MonadTrans(lift))
import Control.Monad.IO.Class (MonadIO(..))
import Data.Foldable (Foldable(foldl'), fold, foldr)
import Data.Functor (($>))
import Data.Functor.Identity (Identity(..))
import Data.Maybe (fromMaybe)
import Data.Semigroup (Endo(..))
import Fusion.Plugin.Types (Fuse(..))
import GHC.Base (build)
import GHC.Exts (IsList(..), IsString(..), oneShot)
import GHC.Types (SPEC(..))
import Prelude hiding (map, mapM, take, concatMap, takeWhile, zipWith, concat)
import Text.Read
( Lexeme(Ident), lexP, parens, prec, readPrec, readListPrec
, readListPrecDefault)
import Streamly.Internal.BaseCompat ((#.))
import Streamly.Internal.Data.Fold.Type (Fold(..))
import Streamly.Internal.Data.Maybe.Strict (Maybe'(..), toMaybe)
import Streamly.Internal.Data.Refold.Type (Refold(..))
import Streamly.Internal.Data.Stream.Step (Step (..))
import Streamly.Internal.Data.SVar.Type (State, adaptState, defState)
import Streamly.Internal.Data.Unfold.Type (Unfold(..))
import qualified Streamly.Internal.Data.Fold.Type as FL hiding (foldr)
import qualified Streamly.Internal.Data.StreamK.Type as K
#ifdef USE_UNFOLDS_EVERYWHERE
import qualified Streamly.Internal.Data.Unfold.Type as Unfold
#endif
#include "DocTestDataStream.hs"
data Stream m a =
forall s. UnStream (State K.StreamK m a -> s -> m (Step s a)) s
unShare :: Stream m a -> Stream m a
unShare :: forall (m :: * -> *) a. Stream m a -> Stream m a
unShare (UnStream State StreamK m a -> s -> m (Step s a)
step s
state) = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
UnStream forall {m :: * -> *} {a}. State StreamK m a -> s -> m (Step s a)
step' s
state
where step' :: State StreamK m a -> s -> m (Step s a)
step' State StreamK m a
gst = State StreamK m a -> s -> m (Step s a)
step (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst)
pattern Stream :: (State K.StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
pattern $bStream :: forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
$mStream :: forall {r} {m :: * -> *} {a}.
Stream m a
-> (forall {s}. (State StreamK m a -> s -> m (Step s a)) -> s -> r)
-> ((# #) -> r)
-> r
Stream step state <- (unShare -> UnStream step state)
where Stream = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
UnStream
{-# COMPLETE Stream #-}
{-# INLINE_NORMAL nilM #-}
nilM :: Applicative m => m b -> Stream m a
nilM :: forall (m :: * -> *) b a. Applicative m => m b -> Stream m a
nilM m b
m = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream (\State StreamK m a
_ ()
_ -> m b
m forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> forall s a. Step s a
Stop) ()
{-# INLINE_NORMAL consM #-}
consM :: Applicative m => m a -> Stream m a -> Stream m a
consM :: forall (m :: * -> *) a.
Applicative m =>
m a -> Stream m a -> Stream m a
consM m a
m (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a -> Maybe s -> m (Step (Maybe s) a)
step1 forall a. Maybe a
Nothing
where
{-# INLINE_LATE step1 #-}
step1 :: State StreamK m a -> Maybe s -> m (Step (Maybe s) a)
step1 State StreamK m a
_ Maybe s
Nothing = (forall s a. a -> s -> Step s a
`Yield` forall a. a -> Maybe a
Just s
state) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m a
m
step1 State StreamK m a
gst (Just s
st) = do
(\case
Yield a
a s
s -> forall s a. a -> s -> Step s a
Yield a
a (forall a. a -> Maybe a
Just s
s)
Skip s
s -> forall s a. s -> Step s a
Skip (forall a. a -> Maybe a
Just s
s)
Step s a
Stop -> forall s a. Step s a
Stop) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> State StreamK m a -> s -> m (Step s a)
step State StreamK m a
gst s
st
{-# INLINE_NORMAL uncons #-}
uncons :: Monad m => Stream m a -> m (Maybe (a, Stream m a))
uncons :: forall (m :: * -> *) a.
Monad m =>
Stream m a -> m (Maybe (a, Stream m a))
uncons (UnStream State StreamK m a -> s -> m (Step s a)
step s
state) = SPEC -> s -> m (Maybe (a, Stream m a))
go SPEC
SPEC s
state
where
go :: SPEC -> s -> m (Maybe (a, Stream m a))
go !SPEC
_ s
st = do
Step s a
r <- State StreamK m a -> s -> m (Step s a)
step forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState s
st
case Step s a
r of
Yield a
x s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just (a
x, forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a -> s -> m (Step s a)
step s
s)
Skip s
s -> SPEC -> s -> m (Maybe (a, Stream m a))
go SPEC
SPEC s
s
Step s a
Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
data UnfoldState s = UnfoldNothing | UnfoldJust s
{-# INLINE_NORMAL unfold #-}
unfold :: Applicative m => Unfold m a b -> a -> Stream m b
unfold :: forall (m :: * -> *) a b.
Applicative m =>
Unfold m a b -> a -> Stream m b
unfold (Unfold s -> m (Step s b)
ustep a -> m s
inject) a
seed = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream forall {p}. p -> UnfoldState s -> m (Step (UnfoldState s) b)
step forall s. UnfoldState s
UnfoldNothing
where
{-# INLINE_LATE step #-}
step :: p -> UnfoldState s -> m (Step (UnfoldState s) b)
step p
_ UnfoldState s
UnfoldNothing = forall s a. s -> Step s a
Skip forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s. s -> UnfoldState s
UnfoldJust forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> m s
inject a
seed
step p
_ (UnfoldJust s
st) = do
(\case
Yield b
x s
s -> forall s a. a -> s -> Step s a
Yield b
x (forall s. s -> UnfoldState s
UnfoldJust s
s)
Skip s
s -> forall s a. s -> Step s a
Skip (forall s. s -> UnfoldState s
UnfoldJust s
s)
Step s b
Stop -> forall s a. Step s a
Stop) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m (Step s b)
ustep s
st
{-# INLINE_NORMAL fromPure #-}
fromPure :: Applicative m => a -> Stream m a
fromPure :: forall (m :: * -> *) a. Applicative m => a -> Stream m a
fromPure a
x = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream (\State StreamK m a
_ Bool
s -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall {p}. p -> Bool -> Step Bool a
step forall a. HasCallStack => a
undefined Bool
s) Bool
True
where
{-# INLINE_LATE step #-}
step :: p -> Bool -> Step Bool a
step p
_ Bool
True = forall s a. a -> s -> Step s a
Yield a
x Bool
False
step p
_ Bool
False = forall s a. Step s a
Stop
{-# INLINE_NORMAL fromEffect #-}
fromEffect :: Applicative m => m a -> Stream m a
fromEffect :: forall (m :: * -> *) a. Applicative m => m a -> Stream m a
fromEffect m a
m = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream forall {p}. p -> Bool -> m (Step Bool a)
step Bool
True
where
{-# INLINE_LATE step #-}
step :: p -> Bool -> m (Step Bool a)
step p
_ Bool
True = (forall s a. a -> s -> Step s a
`Yield` Bool
False) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m a
m
step p
_ Bool
False = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall s a. Step s a
Stop
{-# INLINE_LATE fromList #-}
fromList :: Applicative m => [a] -> Stream m a
#ifdef USE_UNFOLDS_EVERYWHERE
fromList = unfold Unfold.fromList
#else
fromList :: forall (m :: * -> *) a. Applicative m => [a] -> Stream m a
fromList = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream forall {f :: * -> *} {p} {a}.
Applicative f =>
p -> [a] -> f (Step [a] a)
step
where
{-# INLINE_LATE step #-}
step :: p -> [a] -> f (Step [a] a)
step p
_ (a
x:[a]
xs) = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield a
x [a]
xs
step p
_ [] = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall s a. Step s a
Stop
#endif
{-# INLINE_LATE fromStreamK #-}
fromStreamK :: Applicative m => K.StreamK m a -> Stream m a
fromStreamK :: forall (m :: * -> *) a. Applicative m => StreamK m a -> Stream m a
fromStreamK = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream forall {m :: * -> *} {a}.
Applicative m =>
State StreamK m a -> StreamK m a -> m (Step (StreamK m a) a)
step
where
step :: State StreamK m a -> StreamK m a -> m (Step (StreamK m a) a)
step State StreamK m a
gst StreamK m a
m1 =
let stop :: m (Step s a)
stop = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall s a. Step s a
Stop
single :: a -> f (Step (StreamK m a) a)
single a
a = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield a
a forall (m :: * -> *) a. StreamK m a
K.nil
yieldk :: a -> s -> f (Step s a)
yieldk a
a s
r = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield a
a s
r
in forall (m :: * -> *) a r.
State StreamK m a
-> (a -> StreamK m a -> m r)
-> (a -> m r)
-> m r
-> StreamK m a
-> m r
K.foldStreamShared State StreamK m a
gst forall {f :: * -> *} {a} {s}.
Applicative f =>
a -> s -> f (Step s a)
yieldk forall {f :: * -> *} {a} {m :: * -> *} {a}.
Applicative f =>
a -> f (Step (StreamK m a) a)
single forall {s} {a}. m (Step s a)
stop StreamK m a
m1
{-# INLINE_LATE toStreamK #-}
toStreamK :: Monad m => Stream m a -> K.StreamK m a
toStreamK :: forall (m :: * -> *) a. Monad m => Stream m a -> StreamK m a
toStreamK (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = s -> StreamK m a
go s
state
where
go :: s -> StreamK m a
go s
st = forall (m :: * -> *) a.
(forall r.
State StreamK m a
-> (a -> StreamK m a -> m r) -> (a -> m r) -> m r -> m r)
-> StreamK m a
K.MkStream forall a b. (a -> b) -> a -> b
$ \State StreamK m a
gst a -> StreamK m a -> m r
yld a -> m r
_ m r
stp ->
let go' :: s -> m r
go' s
ss = do
Step s a
r <- State StreamK m a -> s -> m (Step s a)
step State StreamK m a
gst s
ss
case Step s a
r of
Yield a
x s
s -> a -> StreamK m a -> m r
yld a
x (s -> StreamK m a
go s
s)
Skip s
s -> s -> m r
go' s
s
Step s a
Stop -> m r
stp
in s -> m r
go' s
st
{-# RULES "fromStreamK/toStreamK fusion"
forall s. toStreamK (fromStreamK s) = s #-}
{-# RULES "toStreamK/fromStreamK fusion"
forall s. fromStreamK (toStreamK s) = s #-}
{-# INLINE_NORMAL foldEither #-}
foldEither :: Monad m =>
Fold m a b -> Stream m a -> m (Either (Fold m a b) (b, Stream m a))
foldEither :: forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Stream m a -> m (Either (Fold m a b) (b, Stream m a))
foldEither (Fold s -> a -> m (Step s b)
fstep m (Step s b)
begin s -> m b
done s -> m b
final) (UnStream State StreamK m a -> s -> m (Step s a)
step s
state) = do
Step s b
res <- m (Step s b)
begin
case Step s b
res of
FL.Partial s
fs -> SPEC -> s -> s -> m (Either (Fold m a b) (b, Stream m a))
go SPEC
SPEC s
fs s
state
FL.Done b
fb -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! forall a b. b -> Either a b
Right (b
fb, forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a -> s -> m (Step s a)
step s
state)
where
{-# INLINE go #-}
go :: SPEC -> s -> s -> m (Either (Fold m a b) (b, Stream m a))
go !SPEC
_ !s
fs s
st = do
Step s a
r <- State StreamK m a -> s -> m (Step s a)
step forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState s
st
case Step s a
r of
Yield a
x s
s -> do
Step s b
res <- s -> a -> m (Step s b)
fstep s
fs a
x
case Step s b
res of
FL.Done b
b -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! forall a b. b -> Either a b
Right (b
b, forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a -> s -> m (Step s a)
step s
s)
FL.Partial s
fs1 -> SPEC -> s -> s -> m (Either (Fold m a b) (b, Stream m a))
go SPEC
SPEC s
fs1 s
s
Skip s
s -> SPEC -> s -> s -> m (Either (Fold m a b) (b, Stream m a))
go SPEC
SPEC s
fs s
s
Step s a
Stop ->
let f :: Fold m a b
f = forall (m :: * -> *) a b s.
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> (s -> m b) -> Fold m a b
Fold s -> a -> m (Step s b)
fstep (forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. s -> Step s b
FL.Partial s
fs) s -> m b
done s -> m b
final
in forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! forall a b. a -> Either a b
Left Fold m a b
f
{-# INLINE_NORMAL foldBreak #-}
foldBreak :: Monad m => Fold m a b -> Stream m a -> m (b, Stream m a)
foldBreak :: forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Stream m a -> m (b, Stream m a)
foldBreak Fold m a b
fld Stream m a
strm = do
Either (Fold m a b) (b, Stream m a)
r <- forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Stream m a -> m (Either (Fold m a b) (b, Stream m a))
foldEither Fold m a b
fld Stream m a
strm
case Either (Fold m a b) (b, Stream m a)
r of
Right (b, Stream m a)
res -> forall (m :: * -> *) a. Monad m => a -> m a
return (b, Stream m a)
res
Left (Fold s -> a -> m (Step s b)
_ m (Step s b)
initial s -> m b
_ s -> m b
final) -> do
Step s b
res <- m (Step s b)
initial
case Step s b
res of
FL.Done b
_ -> forall a. HasCallStack => [Char] -> a
error [Char]
"foldBreak: unreachable state"
FL.Partial s
s -> do
b
b <- s -> m b
final s
s
forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, forall {a}. Stream m a
nil)
where
nil :: Stream m a
nil = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream (\State StreamK m a
_ ()
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop) ()
{-# INLINE_NORMAL fold #-}
fold :: Monad m => Fold m a b -> Stream m a -> m b
fold :: forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Stream m a -> m b
fold Fold m a b
fld Stream m a
strm = do
(b
b, Stream m a
_) <- forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Stream m a -> m (b, Stream m a)
foldBreak Fold m a b
fld Stream m a
strm
forall (m :: * -> *) a. Monad m => a -> m a
return b
b
{-# INLINE_NORMAL foldAddLazy #-}
foldAddLazy :: Monad m => Fold m a b -> Stream m a -> Fold m a b
foldAddLazy :: forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Stream m a -> Fold m a b
foldAddLazy (Fold s -> a -> m (Step s b)
fstep m (Step s b)
finitial s -> m b
fextract s -> m b
ffinal) (Stream State StreamK m a -> s -> m (Step s a)
sstep s
state) =
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> (s -> m b) -> Fold m a b
Fold s -> a -> m (Step s b)
fstep m (Step s b)
initial s -> m b
fextract s -> m b
ffinal
where
initial :: m (Step s b)
initial = do
Step s b
res <- m (Step s b)
finitial
case Step s b
res of
FL.Partial s
fs -> SPEC -> s -> s -> m (Step s b)
go SPEC
SPEC s
fs s
state
FL.Done b
fb -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. b -> Step s b
FL.Done b
fb
{-# INLINE go #-}
go :: SPEC -> s -> s -> m (Step s b)
go !SPEC
_ !s
fs s
st = do
Step s a
r <- State StreamK m a -> s -> m (Step s a)
sstep forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState s
st
case Step s a
r of
Yield a
x s
s -> do
Step s b
res <- s -> a -> m (Step s b)
fstep s
fs a
x
case Step s b
res of
FL.Done b
b -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. b -> Step s b
FL.Done b
b
FL.Partial s
fs1 -> SPEC -> s -> s -> m (Step s b)
go SPEC
SPEC s
fs1 s
s
Skip s
s -> SPEC -> s -> s -> m (Step s b)
go SPEC
SPEC s
fs s
s
Step s a
Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. s -> Step s b
FL.Partial s
fs
foldAdd :: Monad m => Fold m a b -> Stream m a -> m (Fold m a b)
foldAdd :: forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Stream m a -> m (Fold m a b)
foldAdd Fold m a b
f =
forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Stream m a -> m b
Streamly.Internal.Data.Stream.Type.fold (forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Fold m a (Fold m a b)
FL.duplicate Fold m a b
f)
{-# INLINE_NORMAL foldrM #-}
foldrM :: Monad m => (a -> m b -> m b) -> m b -> Stream m a -> m b
foldrM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b -> m b) -> m b -> Stream m a -> m b
foldrM a -> m b -> m b
f m b
z (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = SPEC -> s -> m b
go SPEC
SPEC s
state
where
{-# INLINE_LATE go #-}
go :: SPEC -> s -> m b
go !SPEC
_ s
st = do
Step s a
r <- State StreamK m a -> s -> m (Step s a)
step forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState s
st
case Step s a
r of
Yield a
x s
s -> a -> m b -> m b
f a
x (SPEC -> s -> m b
go SPEC
SPEC s
s)
Skip s
s -> SPEC -> s -> m b
go SPEC
SPEC s
s
Step s a
Stop -> m b
z
{-# INLINE_NORMAL foldrMx #-}
foldrMx :: Monad m
=> (a -> m x -> m x) -> m x -> (m x -> m b) -> Stream m a -> m b
foldrMx :: forall (m :: * -> *) a x b.
Monad m =>
(a -> m x -> m x) -> m x -> (m x -> m b) -> Stream m a -> m b
foldrMx a -> m x -> m x
fstep m x
final m x -> m b
convert (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = m x -> m b
convert forall a b. (a -> b) -> a -> b
$ SPEC -> s -> m x
go SPEC
SPEC s
state
where
{-# INLINE_LATE go #-}
go :: SPEC -> s -> m x
go !SPEC
_ s
st = do
Step s a
r <- State StreamK m a -> s -> m (Step s a)
step forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState s
st
case Step s a
r of
Yield a
x s
s -> a -> m x -> m x
fstep a
x (SPEC -> s -> m x
go SPEC
SPEC s
s)
Skip s
s -> SPEC -> s -> m x
go SPEC
SPEC s
s
Step s a
Stop -> m x
final
{-# INLINE_NORMAL foldr #-}
foldr :: Monad m => (a -> b -> b) -> b -> Stream m a -> m b
foldr :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Stream m a -> m b
foldr a -> b -> b
f b
z = forall (m :: * -> *) a b.
Monad m =>
(a -> m b -> m b) -> m b -> Stream m a -> m b
foldrM (forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> b -> b
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Monad m => a -> m a
return) (forall (m :: * -> *) a. Monad m => a -> m a
return b
z)
{-# INLINE_NORMAL foldrS #-}
foldrS
:: Monad m
=> (a -> Stream m b -> Stream m b)
-> Stream m b
-> Stream m a
-> Stream m b
foldrS :: forall (m :: * -> *) a b.
Monad m =>
(a -> Stream m b -> Stream m b)
-> Stream m b -> Stream m a -> Stream m b
foldrS a -> Stream m b -> Stream m b
f Stream m b
final (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = SPEC -> s -> Stream m b
go SPEC
SPEC s
state
where
{-# INLINE_LATE go #-}
go :: SPEC -> s -> Stream m b
go !SPEC
_ s
st = forall (m :: * -> *) a. Monad m => m (Stream m a) -> Stream m a
concatEffect forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Step s a -> Stream m b
g forall a b. (a -> b) -> a -> b
$ State StreamK m a -> s -> m (Step s a)
step forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState s
st
g :: Step s a -> Stream m b
g Step s a
r =
case Step s a
r of
Yield a
x s
s -> a -> Stream m b -> Stream m b
f a
x (SPEC -> s -> Stream m b
go SPEC
SPEC s
s)
Skip s
s -> SPEC -> s -> Stream m b
go SPEC
SPEC s
s
Step s a
Stop -> Stream m b
final
{-# INLINE_NORMAL foldlMx' #-}
foldlMx' :: Monad m => (x -> a -> m x) -> m x -> (x -> m b) -> Stream m a -> m b
foldlMx' :: forall (m :: * -> *) x a b.
Monad m =>
(x -> a -> m x) -> m x -> (x -> m b) -> Stream m a -> m b
foldlMx' x -> a -> m x
fstep m x
begin x -> m b
done (Stream State StreamK m a -> s -> m (Step s a)
step s
state) =
m x
begin forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \x
x -> SPEC -> x -> s -> m b
go SPEC
SPEC x
x s
state
where
{-# INLINE_LATE go #-}
go :: SPEC -> x -> s -> m b
go !SPEC
_ x
acc s
st = x
acc seq :: forall a b. a -> b -> b
`seq` do
Step s a
r <- State StreamK m a -> s -> m (Step s a)
step forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState s
st
case Step s a
r of
Yield a
x s
s -> do
x
acc' <- x -> a -> m x
fstep x
acc a
x
SPEC -> x -> s -> m b
go SPEC
SPEC x
acc' s
s
Skip s
s -> SPEC -> x -> s -> m b
go SPEC
SPEC x
acc s
s
Step s a
Stop -> x -> m b
done x
acc
{-# INLINE foldlx' #-}
foldlx' :: Monad m => (x -> a -> x) -> x -> (x -> b) -> Stream m a -> m b
foldlx' :: forall (m :: * -> *) x a b.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Stream m a -> m b
foldlx' x -> a -> x
fstep x
begin x -> b
done =
forall (m :: * -> *) x a b.
Monad m =>
(x -> a -> m x) -> m x -> (x -> m b) -> Stream m a -> m b
foldlMx' (\x
b a
a -> forall (m :: * -> *) a. Monad m => a -> m a
return (x -> a -> x
fstep x
b a
a)) (forall (m :: * -> *) a. Monad m => a -> m a
return x
begin) (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. x -> b
done)
{-# INLINE_NORMAL foldlM' #-}
foldlM' :: Monad m => (b -> a -> m b) -> m b -> Stream m a -> m b
foldlM' :: forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Stream m a -> m b
foldlM' b -> a -> m b
fstep m b
mbegin (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = do
b
begin <- m b
mbegin
SPEC -> b -> s -> m b
go SPEC
SPEC b
begin s
state
where
{-# INLINE_LATE go #-}
go :: SPEC -> b -> s -> m b
go !SPEC
_ b
acc s
st = b
acc seq :: forall a b. a -> b -> b
`seq` do
Step s a
r <- State StreamK m a -> s -> m (Step s a)
step forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState s
st
case Step s a
r of
Yield a
x s
s -> do
b
acc' <- b -> a -> m b
fstep b
acc a
x
SPEC -> b -> s -> m b
go SPEC
SPEC b
acc' s
s
Skip s
s -> SPEC -> b -> s -> m b
go SPEC
SPEC b
acc s
s
Step s a
Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return b
acc
{-# INLINE foldl' #-}
foldl' :: Monad m => (b -> a -> b) -> b -> Stream m a -> m b
foldl' :: forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Stream m a -> m b
foldl' b -> a -> b
fstep b
begin = forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Stream m a -> m b
foldlM' (\b
b a
a -> forall (m :: * -> *) a. Monad m => a -> m a
return (b -> a -> b
fstep b
b a
a)) (forall (m :: * -> *) a. Monad m => a -> m a
return b
begin)
{-# INLINE_LATE drain #-}
drain :: Monad m => Stream m a -> m ()
drain :: forall (m :: * -> *) a. Monad m => Stream m a -> m ()
drain (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = SPEC -> s -> m ()
go SPEC
SPEC s
state
where
go :: SPEC -> s -> m ()
go !SPEC
_ s
st = do
Step s a
r <- State StreamK m a -> s -> m (Step s a)
step forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState s
st
case Step s a
r of
Yield a
_ s
s -> SPEC -> s -> m ()
go SPEC
SPEC s
s
Skip s
s -> SPEC -> s -> m ()
go SPEC
SPEC s
s
Step s a
Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
{-# INLINE_NORMAL toList #-}
toList :: Monad m => Stream m a -> m [a]
toList :: forall (m :: * -> *) a. Monad m => Stream m a -> m [a]
toList = forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Stream m a -> m b
Streamly.Internal.Data.Stream.Type.foldr (:) []
{-# INLINE_LATE toListFB #-}
toListFB :: (a -> b -> b) -> b -> Stream Identity a -> b
toListFB :: forall a b. (a -> b -> b) -> b -> Stream Identity a -> b
toListFB a -> b -> b
c b
n (Stream State StreamK Identity a -> s -> Identity (Step s a)
step s
state) = s -> b
go s
state
where
go :: s -> b
go s
st = case forall a. Identity a -> a
runIdentity (State StreamK Identity a -> s -> Identity (Step s a)
step forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState s
st) of
Yield a
x s
s -> a
x a -> b -> b
`c` s -> b
go s
s
Skip s
s -> s -> b
go s
s
Step s a
Stop -> b
n
{-# RULES "toList Identity" Streamly.Internal.Data.Stream.Type.toList = toListId #-}
{-# INLINE_EARLY toListId #-}
toListId :: Stream Identity a -> Identity [a]
toListId :: forall a. Stream Identity a -> Identity [a]
toListId Stream Identity a
s = forall a. a -> Identity a
Identity forall a b. (a -> b) -> a -> b
$ forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
build (\a -> b -> b
c b
n -> forall a b. (a -> b -> b) -> b -> Stream Identity a -> b
toListFB a -> b -> b
c b
n Stream Identity a
s)
{-# INLINE_NORMAL eqBy #-}
eqBy :: Monad m => (a -> b -> Bool) -> Stream m a -> Stream m b -> m Bool
eqBy :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> Bool) -> Stream m a -> Stream m b -> m Bool
eqBy a -> b -> Bool
eq (Stream State StreamK m a -> s -> m (Step s a)
step1 s
t1) (Stream State StreamK m b -> s -> m (Step s b)
step2 s
t2) = SPEC -> s -> s -> m Bool
eq_loop0 SPEC
SPEC s
t1 s
t2
where
eq_loop0 :: SPEC -> s -> s -> m Bool
eq_loop0 !SPEC
_ s
s1 s
s2 = do
Step s a
r <- State StreamK m a -> s -> m (Step s a)
step1 forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState s
s1
case Step s a
r of
Yield a
x s
s1' -> SPEC -> a -> s -> s -> m Bool
eq_loop1 SPEC
SPEC a
x s
s1' s
s2
Skip s
s1' -> SPEC -> s -> s -> m Bool
eq_loop0 SPEC
SPEC s
s1' s
s2
Step s a
Stop -> s -> m Bool
eq_null s
s2
eq_loop1 :: SPEC -> a -> s -> s -> m Bool
eq_loop1 !SPEC
_ a
x s
s1 s
s2 = do
Step s b
r <- State StreamK m b -> s -> m (Step s b)
step2 forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState s
s2
case Step s b
r of
Yield b
y s
s2'
| a -> b -> Bool
eq a
x b
y -> SPEC -> s -> s -> m Bool
eq_loop0 SPEC
SPEC s
s1 s
s2'
| Bool
otherwise -> forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
Skip s
s2' -> SPEC -> a -> s -> s -> m Bool
eq_loop1 SPEC
SPEC a
x s
s1 s
s2'
Step s b
Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
eq_null :: s -> m Bool
eq_null s
s2 = do
Step s b
r <- State StreamK m b -> s -> m (Step s b)
step2 forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState s
s2
case Step s b
r of
Yield b
_ s
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
Skip s
s2' -> s -> m Bool
eq_null s
s2'
Step s b
Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
{-# INLINE_NORMAL cmpBy #-}
cmpBy
:: Monad m
=> (a -> b -> Ordering) -> Stream m a -> Stream m b -> m Ordering
cmpBy :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> Ordering) -> Stream m a -> Stream m b -> m Ordering
cmpBy a -> b -> Ordering
cmp (Stream State StreamK m a -> s -> m (Step s a)
step1 s
t1) (Stream State StreamK m b -> s -> m (Step s b)
step2 s
t2) = SPEC -> s -> s -> m Ordering
cmp_loop0 SPEC
SPEC s
t1 s
t2
where
cmp_loop0 :: SPEC -> s -> s -> m Ordering
cmp_loop0 !SPEC
_ s
s1 s
s2 = do
Step s a
r <- State StreamK m a -> s -> m (Step s a)
step1 forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState s
s1
case Step s a
r of
Yield a
x s
s1' -> SPEC -> a -> s -> s -> m Ordering
cmp_loop1 SPEC
SPEC a
x s
s1' s
s2
Skip s
s1' -> SPEC -> s -> s -> m Ordering
cmp_loop0 SPEC
SPEC s
s1' s
s2
Step s a
Stop -> s -> m Ordering
cmp_null s
s2
cmp_loop1 :: SPEC -> a -> s -> s -> m Ordering
cmp_loop1 !SPEC
_ a
x s
s1 s
s2 = do
Step s b
r <- State StreamK m b -> s -> m (Step s b)
step2 forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState s
s2
case Step s b
r of
Yield b
y s
s2' -> case a
x a -> b -> Ordering
`cmp` b
y of
Ordering
EQ -> SPEC -> s -> s -> m Ordering
cmp_loop0 SPEC
SPEC s
s1 s
s2'
Ordering
c -> forall (m :: * -> *) a. Monad m => a -> m a
return Ordering
c
Skip s
s2' -> SPEC -> a -> s -> s -> m Ordering
cmp_loop1 SPEC
SPEC a
x s
s1 s
s2'
Step s b
Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return Ordering
GT
cmp_null :: s -> m Ordering
cmp_null s
s2 = do
Step s b
r <- State StreamK m b -> s -> m (Step s b)
step2 forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState s
s2
case Step s b
r of
Yield b
_ s
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return Ordering
LT
Skip s
s2' -> s -> m Ordering
cmp_null s
s2'
Step s b
Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return Ordering
EQ
{-# INLINE_NORMAL mapM #-}
mapM :: Monad m => (a -> m b) -> Stream m a -> Stream m b
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Stream m a -> Stream m b
mapM a -> m b
f (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream forall {m :: * -> *} {a}. State StreamK m a -> s -> m (Step s b)
step' s
state
where
{-# INLINE_LATE step' #-}
step' :: State StreamK m a -> s -> m (Step s b)
step' State StreamK m a
gst s
st = do
Step s a
r <- State StreamK m a -> s -> m (Step s a)
step (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
case Step s a
r of
Yield a
x s
s -> a -> m b
f a
x forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \b
a -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield b
a s
s
Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip s
s
Step s a
Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
{-# INLINE map #-}
map :: Monad m => (a -> b) -> Stream m a -> Stream m b
map :: forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> Stream m a -> Stream m b
map a -> b
f = forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Stream m a -> Stream m b
mapM (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f)
instance Monad m => Functor (Stream m) where
{-# INLINE fmap #-}
fmap :: forall a b. (a -> b) -> Stream m a -> Stream m b
fmap = forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> Stream m a -> Stream m b
map
{-# INLINE (<$) #-}
<$ :: forall a b. a -> Stream m b -> Stream m a
(<$) = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> b -> a
const
instance IsList (Stream Identity a) where
type (Item (Stream Identity a)) = a
{-# INLINE fromList #-}
fromList :: [Item (Stream Identity a)] -> Stream Identity a
fromList = forall (m :: * -> *) a. Applicative m => [a] -> Stream m a
Streamly.Internal.Data.Stream.Type.fromList
{-# INLINE toList #-}
toList :: Stream Identity a -> [Item (Stream Identity a)]
toList = forall a. Identity a -> a
runIdentity forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Monad m => Stream m a -> m [a]
Streamly.Internal.Data.Stream.Type.toList
instance Eq a => Eq (Stream Identity a) where
{-# INLINE (==) #-}
== :: Stream Identity a -> Stream Identity a -> Bool
(==) Stream Identity a
xs Stream Identity a
ys = forall a. Identity a -> a
runIdentity forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b.
Monad m =>
(a -> b -> Bool) -> Stream m a -> Stream m b -> m Bool
eqBy forall a. Eq a => a -> a -> Bool
(==) Stream Identity a
xs Stream Identity a
ys
instance Ord a => Ord (Stream Identity a) where
{-# INLINE compare #-}
compare :: Stream Identity a -> Stream Identity a -> Ordering
compare Stream Identity a
xs Stream Identity a
ys = forall a. Identity a -> a
runIdentity forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b.
Monad m =>
(a -> b -> Ordering) -> Stream m a -> Stream m b -> m Ordering
cmpBy forall a. Ord a => a -> a -> Ordering
compare Stream Identity a
xs Stream Identity a
ys
{-# INLINE (<) #-}
Stream Identity a
x < :: Stream Identity a -> Stream Identity a -> Bool
< Stream Identity a
y =
case forall a. Ord a => a -> a -> Ordering
compare Stream Identity a
x Stream Identity a
y of
Ordering
LT -> Bool
True
Ordering
_ -> Bool
False
{-# INLINE (<=) #-}
Stream Identity a
x <= :: Stream Identity a -> Stream Identity a -> Bool
<= Stream Identity a
y =
case forall a. Ord a => a -> a -> Ordering
compare Stream Identity a
x Stream Identity a
y of
Ordering
GT -> Bool
False
Ordering
_ -> Bool
True
{-# INLINE (>) #-}
Stream Identity a
x > :: Stream Identity a -> Stream Identity a -> Bool
> Stream Identity a
y =
case forall a. Ord a => a -> a -> Ordering
compare Stream Identity a
x Stream Identity a
y of
Ordering
GT -> Bool
True
Ordering
_ -> Bool
False
{-# INLINE (>=) #-}
Stream Identity a
x >= :: Stream Identity a -> Stream Identity a -> Bool
>= Stream Identity a
y =
case forall a. Ord a => a -> a -> Ordering
compare Stream Identity a
x Stream Identity a
y of
Ordering
LT -> Bool
False
Ordering
_ -> Bool
True
{-# INLINE max #-}
max :: Stream Identity a -> Stream Identity a -> Stream Identity a
max Stream Identity a
x Stream Identity a
y = if Stream Identity a
x forall a. Ord a => a -> a -> Bool
<= Stream Identity a
y then Stream Identity a
y else Stream Identity a
x
{-# INLINE min #-}
min :: Stream Identity a -> Stream Identity a -> Stream Identity a
min Stream Identity a
x Stream Identity a
y = if Stream Identity a
x forall a. Ord a => a -> a -> Bool
<= Stream Identity a
y then Stream Identity a
x else Stream Identity a
y
instance Show a => Show (Stream Identity a) where
showsPrec :: Int -> Stream Identity a -> ShowS
showsPrec Int
p Stream Identity a
dl = Bool -> ShowS -> ShowS
showParen (Int
p forall a. Ord a => a -> a -> Bool
> Int
10) forall a b. (a -> b) -> a -> b
$
[Char] -> ShowS
showString [Char]
"fromList " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> ShowS
shows (forall l. IsList l => l -> [Item l]
GHC.Exts.toList Stream Identity a
dl)
instance Read a => Read (Stream Identity a) where
readPrec :: ReadPrec (Stream Identity a)
readPrec = forall a. ReadPrec a -> ReadPrec a
parens forall a b. (a -> b) -> a -> b
$ forall a. Int -> ReadPrec a -> ReadPrec a
prec Int
10 forall a b. (a -> b) -> a -> b
$ do
Ident [Char]
"fromList" <- ReadPrec Lexeme
lexP
forall (m :: * -> *) a. Applicative m => [a] -> Stream m a
Streamly.Internal.Data.Stream.Type.fromList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Read a => ReadPrec a
readPrec
readListPrec :: ReadPrec [Stream Identity a]
readListPrec = forall a. Read a => ReadPrec [a]
readListPrecDefault
instance (a ~ Char) => IsString (Stream Identity a) where
{-# INLINE fromString #-}
fromString :: [Char] -> Stream Identity a
fromString = forall (m :: * -> *) a. Applicative m => [a] -> Stream m a
Streamly.Internal.Data.Stream.Type.fromList
instance (Foldable m, Monad m) => Foldable (Stream m) where
{-# INLINE foldMap #-}
foldMap :: forall m a. Monoid m => (a -> m) -> Stream m a -> m
foldMap a -> m
f =
forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
Data.Foldable.fold
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Stream m a -> m b
Streamly.Internal.Data.Stream.Type.foldr (forall a. Monoid a => a -> a -> a
mappend forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m
f) forall a. Monoid a => a
mempty
{-# INLINE foldr #-}
foldr :: forall a b. (a -> b -> b) -> b -> Stream m a -> b
foldr a -> b -> b
f b
z Stream m a
t = forall a. Endo a -> a -> a
appEndo (forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (forall a. (a -> a) -> Endo a
Endo forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. a -> b -> b
f) Stream m a
t) b
z
{-# INLINE foldl' #-}
foldl' :: forall b a. (b -> a -> b) -> b -> Stream m a -> b
foldl' b -> a -> b
f b
z0 Stream m a
xs = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Data.Foldable.foldr forall {b}. a -> (b -> b) -> b -> b
f' forall a. a -> a
id Stream m a
xs b
z0
where f' :: a -> (b -> b) -> b -> b
f' a
x b -> b
k = oneShot :: forall a b. (a -> b) -> a -> b
oneShot forall a b. (a -> b) -> a -> b
$ \b
z -> b -> b
k forall a b. (a -> b) -> a -> b
$! b -> a -> b
f b
z a
x
{-# INLINE length #-}
length :: forall a. Stream m a -> Int
length = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Data.Foldable.foldl' (\Int
n a
_ -> Int
n forall a. Num a => a -> a -> a
+ Int
1) Int
0
{-# INLINE elem #-}
elem :: forall a. Eq a => a -> Stream m a -> Bool
elem = forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Eq a => a -> a -> Bool
(==)
{-# INLINE maximum #-}
maximum :: forall a. Ord a => Stream m a -> a
maximum =
forall a. a -> Maybe a -> a
fromMaybe (forall a. [Char] -> a
errorWithoutStackTrace [Char]
"maximum: empty stream")
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Maybe' a -> Maybe a
toMaybe
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Data.Foldable.foldl' forall {a}. Ord a => Maybe' a -> a -> Maybe' a
getMax forall a. Maybe' a
Nothing'
where
getMax :: Maybe' a -> a -> Maybe' a
getMax Maybe' a
Nothing' a
x = forall a. a -> Maybe' a
Just' a
x
getMax (Just' a
mx) a
x = forall a. a -> Maybe' a
Just' forall a b. (a -> b) -> a -> b
$! forall a. Ord a => a -> a -> a
max a
mx a
x
{-# INLINE minimum #-}
minimum :: forall a. Ord a => Stream m a -> a
minimum =
forall a. a -> Maybe a -> a
fromMaybe (forall a. [Char] -> a
errorWithoutStackTrace [Char]
"minimum: empty stream")
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Maybe' a -> Maybe a
toMaybe
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Data.Foldable.foldl' forall {a}. Ord a => Maybe' a -> a -> Maybe' a
getMin forall a. Maybe' a
Nothing'
where
getMin :: Maybe' a -> a -> Maybe' a
getMin Maybe' a
Nothing' a
x = forall a. a -> Maybe' a
Just' a
x
getMin (Just' a
mn) a
x = forall a. a -> Maybe' a
Just' forall a b. (a -> b) -> a -> b
$! forall a. Ord a => a -> a -> a
min a
mn a
x
{-# INLINE sum #-}
sum :: forall a. Num a => Stream m a -> a
sum = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Data.Foldable.foldl' forall a. Num a => a -> a -> a
(+) a
0
{-# INLINE product #-}
product :: forall a. Num a => Stream m a -> a
product = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Data.Foldable.foldl' forall a. Num a => a -> a -> a
(*) a
1
{-# INLINE_NORMAL take #-}
take :: Applicative m => Int -> Stream m a -> Stream m a
take :: forall (m :: * -> *) a.
Applicative m =>
Int -> Stream m a -> Stream m a
take Int
n (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = Int
n seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a -> (s, Int) -> m (Step (s, Int) a)
step' (s
state, Int
0)
where
{-# INLINE_LATE step' #-}
step' :: State StreamK m a -> (s, Int) -> m (Step (s, Int) a)
step' State StreamK m a
gst (s
st, Int
i) | Int
i forall a. Ord a => a -> a -> Bool
< Int
n = do
(\case
Yield a
x s
s -> forall s a. a -> s -> Step s a
Yield a
x (s
s, Int
i forall a. Num a => a -> a -> a
+ Int
1)
Skip s
s -> forall s a. s -> Step s a
Skip (s
s, Int
i)
Step s a
Stop -> forall s a. Step s a
Stop) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> State StreamK m a -> s -> m (Step s a)
step State StreamK m a
gst s
st
step' State StreamK m a
_ (s
_, Int
_) = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall s a. Step s a
Stop
{-# INLINE_NORMAL takeWhileM #-}
takeWhileM :: Monad m => (a -> m Bool) -> Stream m a -> Stream m a
takeWhileM :: forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> Stream m a
takeWhileM a -> m Bool
f (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a -> s -> m (Step s a)
step' s
state
where
{-# INLINE_LATE step' #-}
step' :: State StreamK m a -> s -> m (Step s a)
step' State StreamK m a
gst s
st = do
Step s a
r <- State StreamK m a -> s -> m (Step s a)
step State StreamK m a
gst s
st
case Step s a
r of
Yield a
x s
s -> do
Bool
b <- a -> m Bool
f a
x
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ if Bool
b then forall s a. a -> s -> Step s a
Yield a
x s
s else forall s a. Step s a
Stop
Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip s
s
Step s a
Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
{-# INLINE takeWhile #-}
takeWhile :: Monad m => (a -> Bool) -> Stream m a -> Stream m a
takeWhile :: forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Stream m a -> Stream m a
takeWhile a -> Bool
f = forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> Stream m a
takeWhileM (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
f)
{-# INLINE_NORMAL takeEndByM #-}
takeEndByM :: Monad m => (a -> m Bool) -> Stream m a -> Stream m a
takeEndByM :: forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> Stream m a
takeEndByM a -> m Bool
f (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a -> Maybe s -> m (Step (Maybe s) a)
step' (forall a. a -> Maybe a
Just s
state)
where
{-# INLINE_LATE step' #-}
step' :: State StreamK m a -> Maybe s -> m (Step (Maybe s) a)
step' State StreamK m a
gst (Just s
st) = do
Step s a
r <- State StreamK m a -> s -> m (Step s a)
step State StreamK m a
gst s
st
case Step s a
r of
Yield a
x s
s -> do
Bool
b <- a -> m Bool
f a
x
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
if Bool -> Bool
not Bool
b
then forall s a. a -> s -> Step s a
Yield a
x (forall a. a -> Maybe a
Just s
s)
else forall s a. a -> s -> Step s a
Yield a
x forall a. Maybe a
Nothing
Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall a. a -> Maybe a
Just s
s)
Step s a
Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
step' State StreamK m a
_ Maybe s
Nothing = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
{-# INLINE takeEndBy #-}
takeEndBy :: Monad m => (a -> Bool) -> Stream m a -> Stream m a
takeEndBy :: forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Stream m a -> Stream m a
takeEndBy a -> Bool
f = forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> Stream m a
takeEndByM (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
f)
{-# INLINE_NORMAL zipWithM #-}
zipWithM :: Monad m
=> (a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
zipWithM :: forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
zipWithM a -> b -> m c
f (Stream State StreamK m a -> s -> m (Step s a)
stepa s
ta) (Stream State StreamK m b -> s -> m (Step s b)
stepb s
tb) = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream forall {m :: * -> *} {a}.
State StreamK m a -> (s, s, Maybe a) -> m (Step (s, s, Maybe a) c)
step (s
ta, s
tb, forall a. Maybe a
Nothing)
where
{-# INLINE_LATE step #-}
step :: State StreamK m a -> (s, s, Maybe a) -> m (Step (s, s, Maybe a) c)
step State StreamK m a
gst (s
sa, s
sb, Maybe a
Nothing) = do
Step s a
r <- State StreamK m a -> s -> m (Step s a)
stepa (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
sa
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
case Step s a
r of
Yield a
x s
sa' -> forall s a. s -> Step s a
Skip (s
sa', s
sb, forall a. a -> Maybe a
Just a
x)
Skip s
sa' -> forall s a. s -> Step s a
Skip (s
sa', s
sb, forall a. Maybe a
Nothing)
Step s a
Stop -> forall s a. Step s a
Stop
step State StreamK m a
gst (s
sa, s
sb, Just a
x) = do
Step s b
r <- State StreamK m b -> s -> m (Step s b)
stepb (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
sb
case Step s b
r of
Yield b
y s
sb' -> do
c
z <- a -> b -> m c
f a
x b
y
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield c
z (s
sa, s
sb', forall a. Maybe a
Nothing)
Skip s
sb' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (s
sa, s
sb', forall a. a -> Maybe a
Just a
x)
Step s b
Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
{-# RULES "zipWithM xs xs"
forall f xs. zipWithM @Identity f xs xs = mapM (\x -> f x x) xs #-}
{-# INLINE zipWith #-}
zipWith :: Monad m => (a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
zipWith :: forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
zipWith a -> b -> c
f = forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
zipWithM (\a
a b
b -> forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> c
f a
a b
b))
{-# INLINE_NORMAL crossApply #-}
crossApply :: Functor f => Stream f (a -> b) -> Stream f a -> Stream f b
crossApply :: forall (f :: * -> *) a b.
Functor f =>
Stream f (a -> b) -> Stream f a -> Stream f b
crossApply (Stream State StreamK f (a -> b) -> s -> f (Step s (a -> b))
stepa s
statea) (Stream State StreamK f a -> s -> f (Step s a)
stepb s
stateb) =
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream forall {m :: * -> *} {a}.
State StreamK m a
-> Either s (a -> b, s, s) -> f (Step (Either s (a -> b, s, s)) b)
step' (forall a b. a -> Either a b
Left s
statea)
where
{-# INLINE_LATE step' #-}
step' :: State StreamK m a
-> Either s (a -> b, s, s) -> f (Step (Either s (a -> b, s, s)) b)
step' State StreamK m a
gst (Left s
st) = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
(\case
Yield a -> b
f s
s -> forall s a. s -> Step s a
Skip (forall a b. b -> Either a b
Right (a -> b
f, s
s, s
stateb))
Skip s
s -> forall s a. s -> Step s a
Skip (forall a b. a -> Either a b
Left s
s)
Step s (a -> b)
Stop -> forall s a. Step s a
Stop)
(State StreamK f (a -> b) -> s -> f (Step s (a -> b))
stepa (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st)
step' State StreamK m a
gst (Right (a -> b
f, s
os, s
st)) = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
(\case
Yield a
a s
s -> forall s a. a -> s -> Step s a
Yield (a -> b
f a
a) (forall a b. b -> Either a b
Right (a -> b
f, s
os, s
s))
Skip s
s -> forall s a. s -> Step s a
Skip (forall a b. b -> Either a b
Right (a -> b
f,s
os, s
s))
Step s a
Stop -> forall s a. s -> Step s a
Skip (forall a b. a -> Either a b
Left s
os))
(State StreamK f a -> s -> f (Step s a)
stepb (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st)
{-# INLINE_NORMAL crossApplySnd #-}
crossApplySnd :: Functor f => Stream f a -> Stream f b -> Stream f b
crossApplySnd :: forall (f :: * -> *) a b.
Functor f =>
Stream f a -> Stream f b -> Stream f b
crossApplySnd (Stream State StreamK f a -> s -> f (Step s a)
stepa s
statea) (Stream State StreamK f b -> s -> f (Step s b)
stepb s
stateb) =
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK f b
-> Either s (s, s) -> f (Step (Either s (s, s)) b)
step (forall a b. a -> Either a b
Left s
statea)
where
{-# INLINE_LATE step #-}
step :: State StreamK f b
-> Either s (s, s) -> f (Step (Either s (s, s)) b)
step State StreamK f b
gst (Left s
st) =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
(\case
Yield a
_ s
s -> forall s a. s -> Step s a
Skip (forall a b. b -> Either a b
Right (s
s, s
stateb))
Skip s
s -> forall s a. s -> Step s a
Skip (forall a b. a -> Either a b
Left s
s)
Step s a
Stop -> forall s a. Step s a
Stop)
(State StreamK f a -> s -> f (Step s a)
stepa (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK f b
gst) s
st)
step State StreamK f b
gst (Right (s
ostate, s
st)) =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
(\case
Yield b
b s
s -> forall s a. a -> s -> Step s a
Yield b
b (forall a b. b -> Either a b
Right (s
ostate, s
s))
Skip s
s -> forall s a. s -> Step s a
Skip (forall a b. b -> Either a b
Right (s
ostate, s
s))
Step s b
Stop -> forall s a. s -> Step s a
Skip (forall a b. a -> Either a b
Left s
ostate))
(State StreamK f b -> s -> f (Step s b)
stepb State StreamK f b
gst s
st)
{-# INLINE_NORMAL crossApplyFst #-}
crossApplyFst :: Functor f => Stream f a -> Stream f b -> Stream f a
crossApplyFst :: forall (f :: * -> *) a b.
Functor f =>
Stream f a -> Stream f b -> Stream f a
crossApplyFst (Stream State StreamK f a -> s -> f (Step s a)
stepa s
statea) (Stream State StreamK f b -> s -> f (Step s b)
stepb s
stateb) =
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK f a
-> Either s (s, s, a) -> f (Step (Either s (s, s, a)) a)
step (forall a b. a -> Either a b
Left s
statea)
where
{-# INLINE_LATE step #-}
step :: State StreamK f a
-> Either s (s, s, a) -> f (Step (Either s (s, s, a)) a)
step State StreamK f a
gst (Left s
st) =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
(\case
Yield a
b s
s -> forall s a. s -> Step s a
Skip (forall a b. b -> Either a b
Right (s
s, s
stateb, a
b))
Skip s
s -> forall s a. s -> Step s a
Skip (forall a b. a -> Either a b
Left s
s)
Step s a
Stop -> forall s a. Step s a
Stop)
(State StreamK f a -> s -> f (Step s a)
stepa State StreamK f a
gst s
st)
step State StreamK f a
gst (Right (s
ostate, s
st, a
b)) =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
(\case
Yield b
_ s
s -> forall s a. a -> s -> Step s a
Yield a
b (forall a b. b -> Either a b
Right (s
ostate, s
s, a
b))
Skip s
s -> forall s a. s -> Step s a
Skip (forall a b. b -> Either a b
Right (s
ostate, s
s, a
b))
Step s b
Stop -> forall s a. s -> Step s a
Skip (forall a b. a -> Either a b
Left s
ostate))
(State StreamK f b -> s -> f (Step s b)
stepb (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK f a
gst) s
st)
{-# INLINE crossWith #-}
crossWith :: Monad m => (a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
crossWith :: forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
crossWith a -> b -> c
f Stream m a
m1 Stream m b
m2 = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b -> c
f Stream m a
m1 forall (f :: * -> *) a b.
Functor f =>
Stream f (a -> b) -> Stream f a -> Stream f b
`crossApply` Stream m b
m2
{-# INLINE cross #-}
cross :: Monad m => Stream m a -> Stream m b -> Stream m (a, b)
cross :: forall (m :: * -> *) a b.
Monad m =>
Stream m a -> Stream m b -> Stream m (a, b)
cross = forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
crossWith (,)
{-# ANN type ConcatMapUState Fuse #-}
data ConcatMapUState o i =
ConcatMapUOuter o
| ConcatMapUInner o i
{-# INLINE_NORMAL unfoldMany #-}
unfoldMany :: Monad m => Unfold m a b -> Stream m a -> Stream m b
unfoldMany :: forall (m :: * -> *) a b.
Monad m =>
Unfold m a b -> Stream m a -> Stream m b
unfoldMany (Unfold s -> m (Step s b)
istep a -> m s
inject) (Stream State StreamK m a -> s -> m (Step s a)
ostep s
ost) =
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream forall {m :: * -> *} {a}.
State StreamK m a
-> ConcatMapUState s s -> m (Step (ConcatMapUState s s) b)
step (forall o i. o -> ConcatMapUState o i
ConcatMapUOuter s
ost)
where
{-# INLINE_LATE step #-}
step :: State StreamK m a
-> ConcatMapUState s s -> m (Step (ConcatMapUState s s) b)
step State StreamK m a
gst (ConcatMapUOuter s
o) = do
Step s a
r <- State StreamK m a -> s -> m (Step s a)
ostep (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
o
case Step s a
r of
Yield a
a s
o' -> do
s
i <- a -> m s
inject a
a
s
i seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a. Monad m => a -> m a
return (forall s a. s -> Step s a
Skip (forall o i. o -> i -> ConcatMapUState o i
ConcatMapUInner s
o' s
i))
Skip s
o' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall o i. o -> ConcatMapUState o i
ConcatMapUOuter s
o')
Step s a
Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
step State StreamK m a
_ (ConcatMapUInner s
o s
i) = do
Step s b
r <- s -> m (Step s b)
istep s
i
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Step s b
r of
Yield b
x s
i' -> forall s a. a -> s -> Step s a
Yield b
x (forall o i. o -> i -> ConcatMapUState o i
ConcatMapUInner s
o s
i')
Skip s
i' -> forall s a. s -> Step s a
Skip (forall o i. o -> i -> ConcatMapUState o i
ConcatMapUInner s
o s
i')
Step s b
Stop -> forall s a. s -> Step s a
Skip (forall o i. o -> ConcatMapUState o i
ConcatMapUOuter s
o)
{-# INLINE_NORMAL concatMapM #-}
concatMapM :: Monad m => (a -> m (Stream m b)) -> Stream m a -> Stream m b
concatMapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m (Stream m b)) -> Stream m a -> Stream m b
concatMapM a -> m (Stream m b)
f (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream forall {m :: * -> *} {a}.
State StreamK m a
-> Either s (Stream m b, s)
-> m (Step (Either s (Stream m b, s)) b)
step' (forall a b. a -> Either a b
Left s
state)
where
{-# INLINE_LATE step' #-}
step' :: State StreamK m a
-> Either s (Stream m b, s)
-> m (Step (Either s (Stream m b, s)) b)
step' State StreamK m a
gst (Left s
st) = do
Step s a
r <- State StreamK m a -> s -> m (Step s a)
step (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
case Step s a
r of
Yield a
a s
s -> do
Stream m b
b_stream <- a -> m (Stream m b)
f a
a
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall a b. b -> Either a b
Right (Stream m b
b_stream, s
s))
Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall a b. a -> Either a b
Left s
s)
Step s a
Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
step' State StreamK m a
gst (Right (UnStream State StreamK m b -> s -> m (Step s b)
inner_step s
inner_st, s
st)) = do
Step s b
r <- State StreamK m b -> s -> m (Step s b)
inner_step (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
inner_st
case Step s b
r of
Yield b
b s
inner_s ->
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield b
b (forall a b. b -> Either a b
Right (forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m b -> s -> m (Step s b)
inner_step s
inner_s, s
st))
Skip s
inner_s ->
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall a b. b -> Either a b
Right (forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m b -> s -> m (Step s b)
inner_step s
inner_s, s
st))
Step s b
Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall a b. a -> Either a b
Left s
st)
{-# INLINE concatMap #-}
concatMap :: Monad m => (a -> Stream m b) -> Stream m a -> Stream m b
concatMap :: forall (m :: * -> *) a b.
Monad m =>
(a -> Stream m b) -> Stream m a -> Stream m b
concatMap a -> Stream m b
f = forall (m :: * -> *) a b.
Monad m =>
(a -> m (Stream m b)) -> Stream m a -> Stream m b
concatMapM (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Stream m b
f)
{-# INLINE concat #-}
concat :: Monad m => Stream m (Stream m a) -> Stream m a
concat :: forall (m :: * -> *) a.
Monad m =>
Stream m (Stream m a) -> Stream m a
concat = forall (m :: * -> *) a b.
Monad m =>
(a -> Stream m b) -> Stream m a -> Stream m b
concatMap forall a. a -> a
id
{-# INLINE concatEffect #-}
concatEffect :: Monad m => m (Stream m a) -> Stream m a
concatEffect :: forall (m :: * -> *) a. Monad m => m (Stream m a) -> Stream m a
concatEffect m (Stream m a)
generator = forall (m :: * -> *) a b.
Monad m =>
(a -> m (Stream m b)) -> Stream m a -> Stream m b
concatMapM (\() -> m (Stream m a)
generator) (forall (m :: * -> *) a. Applicative m => a -> Stream m a
fromPure ())
{-# INLINE_NORMAL concatIterateScan #-}
concatIterateScan :: Monad m =>
(b -> a -> m b)
-> (b -> m (Maybe (b, Stream m a)))
-> b
-> Stream m a
concatIterateScan :: forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b)
-> (b -> m (Maybe (b, Stream m a))) -> b -> Stream m a
concatIterateScan b -> a -> m b
scanner b -> m (Maybe (b, Stream m a))
generate b
initial = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream forall {m :: * -> *} {a}.
State StreamK m a
-> Either b (b, Stream m a)
-> m (Step (Either b (b, Stream m a)) a)
step (forall a b. a -> Either a b
Left b
initial)
where
{-# INLINE_LATE step #-}
step :: State StreamK m a
-> Either b (b, Stream m a)
-> m (Step (Either b (b, Stream m a)) a)
step State StreamK m a
_ (Left b
acc) = do
Maybe (b, Stream m a)
r <- b -> m (Maybe (b, Stream m a))
generate b
acc
case Maybe (b, Stream m a)
r of
Maybe (b, Stream m a)
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
Just (b, Stream m a)
v -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall a b. b -> Either a b
Right (b, Stream m a)
v)
step State StreamK m a
gst (Right (b
st, UnStream State StreamK m a -> s -> m (Step s a)
inner_step s
inner_st)) = do
Step s a
r <- State StreamK m a -> s -> m (Step s a)
inner_step (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
inner_st
case Step s a
r of
Yield a
b s
inner_s -> do
b
acc <- b -> a -> m b
scanner b
st a
b
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield a
b (forall a b. b -> Either a b
Right (b
acc, forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a -> s -> m (Step s a)
inner_step s
inner_s))
Skip s
inner_s ->
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall a b. b -> Either a b
Right (b
st, forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a -> s -> m (Step s a)
inner_step s
inner_s))
Step s a
Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall a b. a -> Either a b
Left b
st)
{-# INLINE_NORMAL concatIterateBfsRev #-}
concatIterateBfsRev :: Monad m =>
(a -> Maybe (Stream m a))
-> Stream m a
-> Stream m a
concatIterateBfsRev :: forall (m :: * -> *) a.
Monad m =>
(a -> Maybe (Stream m a)) -> Stream m a -> Stream m a
concatIterateBfsRev a -> Maybe (Stream m a)
f Stream m a
stream = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream forall {m :: * -> *} {a}.
State StreamK m a
-> (Stream m a, [Stream m a])
-> m (Step (Stream m a, [Stream m a]) a)
step (Stream m a
stream, [])
where
{-# INLINE_LATE step #-}
step :: State StreamK m a
-> (Stream m a, [Stream m a])
-> m (Step (Stream m a, [Stream m a]) a)
step State StreamK m a
gst (UnStream State StreamK m a -> s -> m (Step s a)
step1 s
st, [Stream m a]
xs) = do
Step s a
r <- State StreamK m a -> s -> m (Step s a)
step1 (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
case Step s a
r of
Yield a
a s
s -> do
let xs1 :: [Stream m a]
xs1 =
case a -> Maybe (Stream m a)
f a
a of
Maybe (Stream m a)
Nothing -> [Stream m a]
xs
Just Stream m a
x -> Stream m a
xforall a. a -> [a] -> [a]
:[Stream m a]
xs
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield a
a (forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a -> s -> m (Step s a)
step1 s
s, [Stream m a]
xs1)
Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a -> s -> m (Step s a)
step1 s
s, [Stream m a]
xs)
Step s a
Stop ->
case [Stream m a]
xs of
(Stream m a
y:[Stream m a]
ys) -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (Stream m a
y, [Stream m a]
ys)
[] -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
{-# INLINE_NORMAL concatIterateBfs #-}
concatIterateBfs :: Monad m =>
(a -> Maybe (Stream m a))
-> Stream m a
-> Stream m a
concatIterateBfs :: forall (m :: * -> *) a.
Monad m =>
(a -> Maybe (Stream m a)) -> Stream m a -> Stream m a
concatIterateBfs a -> Maybe (Stream m a)
f Stream m a
stream = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream forall {m :: * -> *} {a}.
State StreamK m a
-> (Stream m a, [Stream m a], [Stream m a])
-> m (Step (Stream m a, [Stream m a], [Stream m a]) a)
step (Stream m a
stream, [], [])
where
{-# INLINE_LATE step #-}
step :: State StreamK m a
-> (Stream m a, [Stream m a], [Stream m a])
-> m (Step (Stream m a, [Stream m a], [Stream m a]) a)
step State StreamK m a
gst (UnStream State StreamK m a -> s -> m (Step s a)
step1 s
st, [Stream m a]
xs, [Stream m a]
ys) = do
Step s a
r <- State StreamK m a -> s -> m (Step s a)
step1 (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
case Step s a
r of
Yield a
a s
s -> do
let ys1 :: [Stream m a]
ys1 =
case a -> Maybe (Stream m a)
f a
a of
Maybe (Stream m a)
Nothing -> [Stream m a]
ys
Just Stream m a
y -> Stream m a
yforall a. a -> [a] -> [a]
:[Stream m a]
ys
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield a
a (forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a -> s -> m (Step s a)
step1 s
s, [Stream m a]
xs, [Stream m a]
ys1)
Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a -> s -> m (Step s a)
step1 s
s, [Stream m a]
xs, [Stream m a]
ys)
Step s a
Stop ->
case [Stream m a]
xs of
(Stream m a
x:[Stream m a]
xs1) -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (Stream m a
x, [Stream m a]
xs1, [Stream m a]
ys)
[] ->
case forall a. [a] -> [a]
reverse [Stream m a]
ys of
(Stream m a
x:[Stream m a]
xs1) -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (Stream m a
x, [Stream m a]
xs1, [])
[] -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
{-# INLINE_NORMAL concatIterateDfs #-}
concatIterateDfs :: Monad m =>
(a -> Maybe (Stream m a))
-> Stream m a
-> Stream m a
concatIterateDfs :: forall (m :: * -> *) a.
Monad m =>
(a -> Maybe (Stream m a)) -> Stream m a -> Stream m a
concatIterateDfs a -> Maybe (Stream m a)
f Stream m a
stream = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream forall {m :: * -> *} {a}.
State StreamK m a
-> (Stream m a, [Stream m a])
-> m (Step (Stream m a, [Stream m a]) a)
step (Stream m a
stream, [])
where
{-# INLINE_LATE step #-}
step :: State StreamK m a
-> (Stream m a, [Stream m a])
-> m (Step (Stream m a, [Stream m a]) a)
step State StreamK m a
gst (UnStream State StreamK m a -> s -> m (Step s a)
step1 s
st, [Stream m a]
xs) = do
Step s a
r <- State StreamK m a -> s -> m (Step s a)
step1 (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
case Step s a
r of
Yield a
a s
s -> do
let st1 :: (Stream m a, [Stream m a])
st1 =
case a -> Maybe (Stream m a)
f a
a of
Maybe (Stream m a)
Nothing -> (forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a -> s -> m (Step s a)
step1 s
s, [Stream m a]
xs)
Just Stream m a
x -> (Stream m a
x, forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a -> s -> m (Step s a)
step1 s
sforall a. a -> [a] -> [a]
:[Stream m a]
xs)
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield a
a (Stream m a, [Stream m a])
st1
Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a -> s -> m (Step s a)
step1 s
s, [Stream m a]
xs)
Step s a
Stop ->
case [Stream m a]
xs of
(Stream m a
y:[Stream m a]
ys) -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (Stream m a
y, [Stream m a]
ys)
[] -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
{-# ANN type IterateUnfoldState Fuse #-}
data IterateUnfoldState o i =
IterateUnfoldOuter o
| IterateUnfoldInner o i [i]
{-# INLINE_NORMAL unfoldIterateDfs #-}
unfoldIterateDfs :: Monad m =>
Unfold m a a
-> Stream m a
-> Stream m a
unfoldIterateDfs :: forall (m :: * -> *) a.
Monad m =>
Unfold m a a -> Stream m a -> Stream m a
unfoldIterateDfs (Unfold s -> m (Step s a)
istep a -> m s
inject) (Stream State StreamK m a -> s -> m (Step s a)
ostep s
ost) =
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream forall {m :: * -> *} {a}.
State StreamK m a
-> IterateUnfoldState s s -> m (Step (IterateUnfoldState s s) a)
step (forall o i. o -> IterateUnfoldState o i
IterateUnfoldOuter s
ost)
where
{-# INLINE_LATE step #-}
step :: State StreamK m a
-> IterateUnfoldState s s -> m (Step (IterateUnfoldState s s) a)
step State StreamK m a
gst (IterateUnfoldOuter s
o) = do
Step s a
r <- State StreamK m a -> s -> m (Step s a)
ostep (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
o
case Step s a
r of
Yield a
a s
s -> do
s
i <- a -> m s
inject a
a
s
i seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a. Monad m => a -> m a
return (forall s a. a -> s -> Step s a
Yield a
a (forall o i. o -> i -> [i] -> IterateUnfoldState o i
IterateUnfoldInner s
s s
i []))
Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall o i. o -> IterateUnfoldState o i
IterateUnfoldOuter s
s)
Step s a
Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
step State StreamK m a
_ (IterateUnfoldInner s
o s
i [s]
ii) = do
Step s a
r <- s -> m (Step s a)
istep s
i
case Step s a
r of
Yield a
x s
s -> do
s
i1 <- a -> m s
inject a
x
s
i1 seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield a
x (forall o i. o -> i -> [i] -> IterateUnfoldState o i
IterateUnfoldInner s
o s
i1 (s
sforall a. a -> [a] -> [a]
:[s]
ii))
Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall o i. o -> i -> [i] -> IterateUnfoldState o i
IterateUnfoldInner s
o s
s [s]
ii)
Step s a
Stop ->
case [s]
ii of
(s
y:[s]
ys) -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall o i. o -> i -> [i] -> IterateUnfoldState o i
IterateUnfoldInner s
o s
y [s]
ys)
[] -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall o i. o -> IterateUnfoldState o i
IterateUnfoldOuter s
o)
{-# ANN type IterateUnfoldBFSRevState Fuse #-}
data IterateUnfoldBFSRevState o i =
IterateUnfoldBFSRevOuter o [i]
| IterateUnfoldBFSRevInner i [i]
{-# INLINE_NORMAL unfoldIterateBfsRev #-}
unfoldIterateBfsRev :: Monad m =>
Unfold m a a
-> Stream m a
-> Stream m a
unfoldIterateBfsRev :: forall (m :: * -> *) a.
Monad m =>
Unfold m a a -> Stream m a -> Stream m a
unfoldIterateBfsRev (Unfold s -> m (Step s a)
istep a -> m s
inject) (Stream State StreamK m a -> s -> m (Step s a)
ostep s
ost) =
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream forall {m :: * -> *} {a}.
State StreamK m a
-> IterateUnfoldBFSRevState s s
-> m (Step (IterateUnfoldBFSRevState s s) a)
step (forall o i. o -> [i] -> IterateUnfoldBFSRevState o i
IterateUnfoldBFSRevOuter s
ost [])
where
{-# INLINE_LATE step #-}
step :: State StreamK m a
-> IterateUnfoldBFSRevState s s
-> m (Step (IterateUnfoldBFSRevState s s) a)
step State StreamK m a
gst (IterateUnfoldBFSRevOuter s
o [s]
ii) = do
Step s a
r <- State StreamK m a -> s -> m (Step s a)
ostep (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
o
case Step s a
r of
Yield a
a s
s -> do
s
i <- a -> m s
inject a
a
s
i seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a. Monad m => a -> m a
return (forall s a. a -> s -> Step s a
Yield a
a (forall o i. o -> [i] -> IterateUnfoldBFSRevState o i
IterateUnfoldBFSRevOuter s
s (s
iforall a. a -> [a] -> [a]
:[s]
ii)))
Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall o i. o -> [i] -> IterateUnfoldBFSRevState o i
IterateUnfoldBFSRevOuter s
s [s]
ii)
Step s a
Stop ->
case [s]
ii of
(s
y:[s]
ys) -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall o i. i -> [i] -> IterateUnfoldBFSRevState o i
IterateUnfoldBFSRevInner s
y [s]
ys)
[] -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
step State StreamK m a
_ (IterateUnfoldBFSRevInner s
i [s]
ii) = do
Step s a
r <- s -> m (Step s a)
istep s
i
case Step s a
r of
Yield a
x s
s -> do
s
i1 <- a -> m s
inject a
x
s
i1 seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield a
x (forall o i. i -> [i] -> IterateUnfoldBFSRevState o i
IterateUnfoldBFSRevInner s
s (s
i1forall a. a -> [a] -> [a]
:[s]
ii))
Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall o i. i -> [i] -> IterateUnfoldBFSRevState o i
IterateUnfoldBFSRevInner s
s [s]
ii)
Step s a
Stop ->
case [s]
ii of
(s
y:[s]
ys) -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall o i. i -> [i] -> IterateUnfoldBFSRevState o i
IterateUnfoldBFSRevInner s
y [s]
ys)
[] -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
{-# ANN type IterateUnfoldBFSState Fuse #-}
data IterateUnfoldBFSState o i =
IterateUnfoldBFSOuter o [i]
| IterateUnfoldBFSInner i [i] [i]
{-# INLINE_NORMAL unfoldIterateBfs #-}
unfoldIterateBfs :: Monad m =>
Unfold m a a
-> Stream m a
-> Stream m a
unfoldIterateBfs :: forall (m :: * -> *) a.
Monad m =>
Unfold m a a -> Stream m a -> Stream m a
unfoldIterateBfs (Unfold s -> m (Step s a)
istep a -> m s
inject) (Stream State StreamK m a -> s -> m (Step s a)
ostep s
ost) =
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream forall {m :: * -> *} {a}.
State StreamK m a
-> IterateUnfoldBFSState s s
-> m (Step (IterateUnfoldBFSState s s) a)
step (forall o i. o -> [i] -> IterateUnfoldBFSState o i
IterateUnfoldBFSOuter s
ost [])
where
{-# INLINE_LATE step #-}
step :: State StreamK m a
-> IterateUnfoldBFSState s s
-> m (Step (IterateUnfoldBFSState s s) a)
step State StreamK m a
gst (IterateUnfoldBFSOuter s
o [s]
rii) = do
Step s a
r <- State StreamK m a -> s -> m (Step s a)
ostep (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
o
case Step s a
r of
Yield a
a s
s -> do
s
i <- a -> m s
inject a
a
s
i seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a. Monad m => a -> m a
return (forall s a. a -> s -> Step s a
Yield a
a (forall o i. o -> [i] -> IterateUnfoldBFSState o i
IterateUnfoldBFSOuter s
s (s
iforall a. a -> [a] -> [a]
:[s]
rii)))
Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall o i. o -> [i] -> IterateUnfoldBFSState o i
IterateUnfoldBFSOuter s
s [s]
rii)
Step s a
Stop ->
case forall a. [a] -> [a]
reverse [s]
rii of
(s
y:[s]
ys) -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall o i. i -> [i] -> [i] -> IterateUnfoldBFSState o i
IterateUnfoldBFSInner s
y [s]
ys [])
[] -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
step State StreamK m a
_ (IterateUnfoldBFSInner s
i [s]
ii [s]
rii) = do
Step s a
r <- s -> m (Step s a)
istep s
i
case Step s a
r of
Yield a
x s
s -> do
s
i1 <- a -> m s
inject a
x
s
i1 seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield a
x (forall o i. i -> [i] -> [i] -> IterateUnfoldBFSState o i
IterateUnfoldBFSInner s
s [s]
ii (s
i1forall a. a -> [a] -> [a]
:[s]
rii))
Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall o i. i -> [i] -> [i] -> IterateUnfoldBFSState o i
IterateUnfoldBFSInner s
s [s]
ii [s]
rii)
Step s a
Stop ->
case [s]
ii of
(s
y:[s]
ys) -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall o i. i -> [i] -> [i] -> IterateUnfoldBFSState o i
IterateUnfoldBFSInner s
y [s]
ys [s]
rii)
[] ->
case forall a. [a] -> [a]
reverse [s]
rii of
(s
y:[s]
ys) -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall o i. i -> [i] -> [i] -> IterateUnfoldBFSState o i
IterateUnfoldBFSInner s
y [s]
ys [])
[] -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
{-# INLINE_NORMAL reduceIterateBfs #-}
reduceIterateBfs :: Monad m =>
(a -> a -> m a) -> Stream m a -> m (Maybe a)
reduceIterateBfs :: forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Stream m a -> m (Maybe a)
reduceIterateBfs a -> a -> m a
f (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = SPEC -> s -> [a] -> Maybe a -> m (Maybe a)
go SPEC
SPEC s
state [] forall a. Maybe a
Nothing
where
go :: SPEC -> s -> [a] -> Maybe a -> m (Maybe a)
go SPEC
_ s
st [a]
xs Maybe a
Nothing = do
Step s a
r <- State StreamK m a -> s -> m (Step s a)
step forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState s
st
case Step s a
r of
Yield a
x1 s
s -> SPEC -> s -> [a] -> Maybe a -> m (Maybe a)
go SPEC
SPEC s
s [a]
xs (forall a. a -> Maybe a
Just a
x1)
Skip s
s -> SPEC -> s -> [a] -> Maybe a -> m (Maybe a)
go SPEC
SPEC s
s [a]
xs forall a. Maybe a
Nothing
Step s a
Stop ->
case [a]
xs of
[] -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
[a]
_ -> SPEC -> [a] -> [a] -> m (Maybe a)
goBuf SPEC
SPEC [a]
xs []
go SPEC
_ s
st [a]
xs (Just a
x1) = do
Step s a
r2 <- State StreamK m a -> s -> m (Step s a)
step forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState s
st
case Step s a
r2 of
Yield a
x2 s
s -> do
a
x <- a -> a -> m a
f a
x1 a
x2
SPEC -> s -> [a] -> Maybe a -> m (Maybe a)
go SPEC
SPEC s
s (a
xforall a. a -> [a] -> [a]
:[a]
xs) forall a. Maybe a
Nothing
Skip s
s -> SPEC -> s -> [a] -> Maybe a -> m (Maybe a)
go SPEC
SPEC s
s [a]
xs (forall a. a -> Maybe a
Just a
x1)
Step s a
Stop ->
case [a]
xs of
[] -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just a
x1)
[a]
_ -> SPEC -> [a] -> [a] -> m (Maybe a)
goBuf SPEC
SPEC (a
x1forall a. a -> [a] -> [a]
:[a]
xs) []
goBuf :: SPEC -> [a] -> [a] -> m (Maybe a)
goBuf SPEC
_ [] [a]
ys = SPEC -> [a] -> [a] -> m (Maybe a)
goBuf SPEC
SPEC [a]
ys []
goBuf SPEC
_ [a
x1] [a]
ys = do
case [a]
ys of
[] -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just a
x1)
(a
x2:[a]
xs) -> do
a
y <- a -> a -> m a
f a
x1 a
x2
SPEC -> [a] -> [a] -> m (Maybe a)
goBuf SPEC
SPEC [a]
xs [a
y]
goBuf SPEC
_ (a
x1:a
x2:[a]
xs) [a]
ys = do
a
y <- a -> a -> m a
f a
x1 a
x2
SPEC -> [a] -> [a] -> m (Maybe a)
goBuf SPEC
SPEC [a]
xs (a
yforall a. a -> [a] -> [a]
:[a]
ys)
foldIterateBfs ::
Fold m a (Either a a) -> Stream m a -> m (Maybe a)
foldIterateBfs :: forall (m :: * -> *) a.
Fold m a (Either a a) -> Stream m a -> m (Maybe a)
foldIterateBfs = forall a. HasCallStack => a
undefined
{-# ANN type FoldManyPost Fuse #-}
data FoldManyPost s fs b a
= FoldManyPostStart s
| FoldManyPostLoop s fs
| FoldManyPostYield b (FoldManyPost s fs b a)
| FoldManyPostDone
{-# INLINE_NORMAL foldManyPost #-}
foldManyPost :: Monad m => Fold m a b -> Stream m a -> Stream m b
foldManyPost :: forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Stream m a -> Stream m b
foldManyPost (Fold s -> a -> m (Step s b)
fstep m (Step s b)
initial s -> m b
_ s -> m b
final) (Stream State StreamK m a -> s -> m (Step s a)
step s
state) =
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream forall {m :: * -> *} {a} {a}.
State StreamK m a
-> FoldManyPost s s b a -> m (Step (FoldManyPost s s b a) b)
step' (forall s fs b a. s -> FoldManyPost s fs b a
FoldManyPostStart s
state)
where
{-# INLINE consume #-}
consume :: a -> s -> s -> m (Step (FoldManyPost s s b a) a)
consume a
x s
s s
fs = do
Step s b
res <- s -> a -> m (Step s b)
fstep s
fs a
x
forall (m :: * -> *) a. Monad m => a -> m a
return
forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip
forall a b. (a -> b) -> a -> b
$ case Step s b
res of
FL.Done b
b -> forall s fs b a.
b -> FoldManyPost s fs b a -> FoldManyPost s fs b a
FoldManyPostYield b
b (forall s fs b a. s -> FoldManyPost s fs b a
FoldManyPostStart s
s)
FL.Partial s
ps -> forall s fs b a. s -> fs -> FoldManyPost s fs b a
FoldManyPostLoop s
s s
ps
{-# INLINE_LATE step' #-}
step' :: State StreamK m a
-> FoldManyPost s s b a -> m (Step (FoldManyPost s s b a) b)
step' State StreamK m a
_ (FoldManyPostStart s
st) = do
Step s b
r <- m (Step s b)
initial
forall (m :: * -> *) a. Monad m => a -> m a
return
forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip
forall a b. (a -> b) -> a -> b
$ case Step s b
r of
FL.Done b
b -> forall s fs b a.
b -> FoldManyPost s fs b a -> FoldManyPost s fs b a
FoldManyPostYield b
b (forall s fs b a. s -> FoldManyPost s fs b a
FoldManyPostStart s
st)
FL.Partial s
fs -> forall s fs b a. s -> fs -> FoldManyPost s fs b a
FoldManyPostLoop s
st s
fs
step' State StreamK m a
gst (FoldManyPostLoop s
st s
fs) = do
Step s a
r <- State StreamK m a -> s -> m (Step s a)
step (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
case Step s a
r of
Yield a
x s
s -> forall {s} {a} {a}.
a -> s -> s -> m (Step (FoldManyPost s s b a) a)
consume a
x s
s s
fs
Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall s fs b a. s -> fs -> FoldManyPost s fs b a
FoldManyPostLoop s
s s
fs)
Step s a
Stop -> do
b
b <- s -> m b
final s
fs
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall s fs b a.
b -> FoldManyPost s fs b a -> FoldManyPost s fs b a
FoldManyPostYield b
b forall s fs b a. FoldManyPost s fs b a
FoldManyPostDone)
step' State StreamK m a
_ (FoldManyPostYield b
b FoldManyPost s s b a
next) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield b
b FoldManyPost s s b a
next
step' State StreamK m a
_ FoldManyPost s s b a
FoldManyPostDone = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
{-# ANN type FoldMany Fuse #-}
data FoldMany s fs b a
= FoldManyStart s
| FoldManyFirst fs s
| FoldManyLoop s fs
| FoldManyYield b (FoldMany s fs b a)
| FoldManyDone
{-# INLINE_NORMAL foldMany #-}
foldMany :: Monad m => Fold m a b -> Stream m a -> Stream m b
foldMany :: forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Stream m a -> Stream m b
foldMany (Fold s -> a -> m (Step s b)
fstep m (Step s b)
initial s -> m b
_ s -> m b
final) (Stream State StreamK m a -> s -> m (Step s a)
step s
state) =
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream forall {m :: * -> *} {a} {a}.
State StreamK m a
-> FoldMany s s b a -> m (Step (FoldMany s s b a) b)
step' (forall s fs b a. s -> FoldMany s fs b a
FoldManyStart s
state)
where
{-# INLINE consume #-}
consume :: a -> s -> s -> m (Step (FoldMany s s b a) a)
consume a
x s
s s
fs = do
Step s b
res <- s -> a -> m (Step s b)
fstep s
fs a
x
forall (m :: * -> *) a. Monad m => a -> m a
return
forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip
forall a b. (a -> b) -> a -> b
$ case Step s b
res of
FL.Done b
b -> forall s fs b a. b -> FoldMany s fs b a -> FoldMany s fs b a
FoldManyYield b
b (forall s fs b a. s -> FoldMany s fs b a
FoldManyStart s
s)
FL.Partial s
ps -> forall s fs b a. s -> fs -> FoldMany s fs b a
FoldManyLoop s
s s
ps
{-# INLINE_LATE step' #-}
step' :: State StreamK m a
-> FoldMany s s b a -> m (Step (FoldMany s s b a) b)
step' State StreamK m a
_ (FoldManyStart s
st) = do
Step s b
r <- m (Step s b)
initial
forall (m :: * -> *) a. Monad m => a -> m a
return
forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip
forall a b. (a -> b) -> a -> b
$ case Step s b
r of
FL.Done b
b -> forall s fs b a. b -> FoldMany s fs b a -> FoldMany s fs b a
FoldManyYield b
b (forall s fs b a. s -> FoldMany s fs b a
FoldManyStart s
st)
FL.Partial s
fs -> forall s fs b a. fs -> s -> FoldMany s fs b a
FoldManyFirst s
fs s
st
step' State StreamK m a
gst (FoldManyFirst s
fs s
st) = do
Step s a
r <- State StreamK m a -> s -> m (Step s a)
step (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
case Step s a
r of
Yield a
x s
s -> forall {s} {a} {a}. a -> s -> s -> m (Step (FoldMany s s b a) a)
consume a
x s
s s
fs
Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall s fs b a. fs -> s -> FoldMany s fs b a
FoldManyFirst s
fs s
s)
Step s a
Stop -> s -> m b
final s
fs forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
step' State StreamK m a
gst (FoldManyLoop s
st s
fs) = do
Step s a
r <- State StreamK m a -> s -> m (Step s a)
step (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
case Step s a
r of
Yield a
x s
s -> forall {s} {a} {a}. a -> s -> s -> m (Step (FoldMany s s b a) a)
consume a
x s
s s
fs
Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall s fs b a. s -> fs -> FoldMany s fs b a
FoldManyLoop s
s s
fs)
Step s a
Stop -> do
b
b <- s -> m b
final s
fs
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall s fs b a. b -> FoldMany s fs b a -> FoldMany s fs b a
FoldManyYield b
b forall s fs b a. FoldMany s fs b a
FoldManyDone)
step' State StreamK m a
_ (FoldManyYield b
b FoldMany s s b a
next) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield b
b FoldMany s s b a
next
step' State StreamK m a
_ FoldMany s s b a
FoldManyDone = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
{-# INLINE groupsOf #-}
groupsOf :: Monad m => Int -> Fold m a b -> Stream m a -> Stream m b
groupsOf :: forall (m :: * -> *) a b.
Monad m =>
Int -> Fold m a b -> Stream m a -> Stream m b
groupsOf Int
n Fold m a b
f = forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Stream m a -> Stream m b
foldMany (forall (m :: * -> *) a b.
Monad m =>
Int -> Fold m a b -> Fold m a b
FL.take Int
n Fold m a b
f)
{-# INLINE_NORMAL refoldMany #-}
refoldMany :: Monad m => Refold m x a b -> m x -> Stream m a -> Stream m b
refoldMany :: forall (m :: * -> *) x a b.
Monad m =>
Refold m x a b -> m x -> Stream m a -> Stream m b
refoldMany (Refold s -> a -> m (Step s b)
fstep x -> m (Step s b)
inject s -> m b
extract) m x
action (Stream State StreamK m a -> s -> m (Step s a)
step s
state) =
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream forall {m :: * -> *} {a} {a}.
State StreamK m a
-> FoldMany s s b a -> m (Step (FoldMany s s b a) b)
step' (forall s fs b a. s -> FoldMany s fs b a
FoldManyStart s
state)
where
{-# INLINE consume #-}
consume :: a -> s -> s -> m (Step (FoldMany s s b a) a)
consume a
x s
s s
fs = do
Step s b
res <- s -> a -> m (Step s b)
fstep s
fs a
x
forall (m :: * -> *) a. Monad m => a -> m a
return
forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip
forall a b. (a -> b) -> a -> b
$ case Step s b
res of
FL.Done b
b -> forall s fs b a. b -> FoldMany s fs b a -> FoldMany s fs b a
FoldManyYield b
b (forall s fs b a. s -> FoldMany s fs b a
FoldManyStart s
s)
FL.Partial s
ps -> forall s fs b a. s -> fs -> FoldMany s fs b a
FoldManyLoop s
s s
ps
{-# INLINE_LATE step' #-}
step' :: State StreamK m a
-> FoldMany s s b a -> m (Step (FoldMany s s b a) b)
step' State StreamK m a
_ (FoldManyStart s
st) = do
Step s b
r <- m x
action forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= x -> m (Step s b)
inject
forall (m :: * -> *) a. Monad m => a -> m a
return
forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip
forall a b. (a -> b) -> a -> b
$ case Step s b
r of
FL.Done b
b -> forall s fs b a. b -> FoldMany s fs b a -> FoldMany s fs b a
FoldManyYield b
b (forall s fs b a. s -> FoldMany s fs b a
FoldManyStart s
st)
FL.Partial s
fs -> forall s fs b a. fs -> s -> FoldMany s fs b a
FoldManyFirst s
fs s
st
step' State StreamK m a
gst (FoldManyFirst s
fs s
st) = do
Step s a
r <- State StreamK m a -> s -> m (Step s a)
step (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
case Step s a
r of
Yield a
x s
s -> forall {s} {a} {a}. a -> s -> s -> m (Step (FoldMany s s b a) a)
consume a
x s
s s
fs
Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall s fs b a. fs -> s -> FoldMany s fs b a
FoldManyFirst s
fs s
s)
Step s a
Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
step' State StreamK m a
gst (FoldManyLoop s
st s
fs) = do
Step s a
r <- State StreamK m a -> s -> m (Step s a)
step (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
case Step s a
r of
Yield a
x s
s -> forall {s} {a} {a}. a -> s -> s -> m (Step (FoldMany s s b a) a)
consume a
x s
s s
fs
Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall s fs b a. s -> fs -> FoldMany s fs b a
FoldManyLoop s
s s
fs)
Step s a
Stop -> do
b
b <- s -> m b
extract s
fs
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall s fs b a. b -> FoldMany s fs b a -> FoldMany s fs b a
FoldManyYield b
b forall s fs b a. FoldMany s fs b a
FoldManyDone)
step' State StreamK m a
_ (FoldManyYield b
b FoldMany s s b a
next) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield b
b FoldMany s s b a
next
step' State StreamK m a
_ FoldMany s s b a
FoldManyDone = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
newtype CrossStream m a = CrossStream {forall (m :: * -> *) a. CrossStream m a -> Stream m a
unCrossStream :: Stream m a}
deriving (forall a b. a -> CrossStream m b -> CrossStream m a
forall a b. (a -> b) -> CrossStream m a -> CrossStream m b
forall (m :: * -> *) a b.
Monad m =>
a -> CrossStream m b -> CrossStream m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> CrossStream m a -> CrossStream m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> CrossStream m b -> CrossStream m a
$c<$ :: forall (m :: * -> *) a b.
Monad m =>
a -> CrossStream m b -> CrossStream m a
fmap :: forall a b. (a -> b) -> CrossStream m a -> CrossStream m b
$cfmap :: forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> CrossStream m a -> CrossStream m b
Functor, forall a. Eq a => a -> CrossStream m a -> Bool
forall a. Num a => CrossStream m a -> a
forall a. Ord a => CrossStream m a -> a
forall m. Monoid m => CrossStream m m -> m
forall a. CrossStream m a -> Bool
forall a. CrossStream m a -> Int
forall a. CrossStream m a -> [a]
forall a. (a -> a -> a) -> CrossStream m a -> a
forall m a. Monoid m => (a -> m) -> CrossStream m a -> m
forall b a. (b -> a -> b) -> b -> CrossStream m a -> b
forall a b. (a -> b -> b) -> b -> CrossStream m a -> b
forall (m :: * -> *) a.
(Foldable m, Monad m, Eq a) =>
a -> CrossStream m a -> Bool
forall (m :: * -> *) a.
(Foldable m, Monad m, Num a) =>
CrossStream m a -> a
forall (m :: * -> *) a.
(Foldable m, Monad m, Ord a) =>
CrossStream m a -> a
forall (m :: * -> *) m.
(Foldable m, Monad m, Monoid m) =>
CrossStream m m -> m
forall (m :: * -> *) a.
(Foldable m, Monad m) =>
CrossStream m a -> Bool
forall (m :: * -> *) a.
(Foldable m, Monad m) =>
CrossStream m a -> Int
forall (m :: * -> *) a.
(Foldable m, Monad m) =>
CrossStream m a -> [a]
forall (m :: * -> *) a.
(Foldable m, Monad m) =>
(a -> a -> a) -> CrossStream m a -> a
forall (m :: * -> *) m a.
(Foldable m, Monad m, Monoid m) =>
(a -> m) -> CrossStream m a -> m
forall (m :: * -> *) b a.
(Foldable m, Monad m) =>
(b -> a -> b) -> b -> CrossStream m a -> b
forall (m :: * -> *) a b.
(Foldable m, Monad m) =>
(a -> b -> b) -> b -> CrossStream m a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: forall a. Num a => CrossStream m a -> a
$cproduct :: forall (m :: * -> *) a.
(Foldable m, Monad m, Num a) =>
CrossStream m a -> a
sum :: forall a. Num a => CrossStream m a -> a
$csum :: forall (m :: * -> *) a.
(Foldable m, Monad m, Num a) =>
CrossStream m a -> a
minimum :: forall a. Ord a => CrossStream m a -> a
$cminimum :: forall (m :: * -> *) a.
(Foldable m, Monad m, Ord a) =>
CrossStream m a -> a
maximum :: forall a. Ord a => CrossStream m a -> a
$cmaximum :: forall (m :: * -> *) a.
(Foldable m, Monad m, Ord a) =>
CrossStream m a -> a
elem :: forall a. Eq a => a -> CrossStream m a -> Bool
$celem :: forall (m :: * -> *) a.
(Foldable m, Monad m, Eq a) =>
a -> CrossStream m a -> Bool
length :: forall a. CrossStream m a -> Int
$clength :: forall (m :: * -> *) a.
(Foldable m, Monad m) =>
CrossStream m a -> Int
null :: forall a. CrossStream m a -> Bool
$cnull :: forall (m :: * -> *) a.
(Foldable m, Monad m) =>
CrossStream m a -> Bool
toList :: forall a. CrossStream m a -> [a]
$ctoList :: forall (m :: * -> *) a.
(Foldable m, Monad m) =>
CrossStream m a -> [a]
foldl1 :: forall a. (a -> a -> a) -> CrossStream m a -> a
$cfoldl1 :: forall (m :: * -> *) a.
(Foldable m, Monad m) =>
(a -> a -> a) -> CrossStream m a -> a
foldr1 :: forall a. (a -> a -> a) -> CrossStream m a -> a
$cfoldr1 :: forall (m :: * -> *) a.
(Foldable m, Monad m) =>
(a -> a -> a) -> CrossStream m a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> CrossStream m a -> b
$cfoldl' :: forall (m :: * -> *) b a.
(Foldable m, Monad m) =>
(b -> a -> b) -> b -> CrossStream m a -> b
foldl :: forall b a. (b -> a -> b) -> b -> CrossStream m a -> b
$cfoldl :: forall (m :: * -> *) b a.
(Foldable m, Monad m) =>
(b -> a -> b) -> b -> CrossStream m a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> CrossStream m a -> b
$cfoldr' :: forall (m :: * -> *) a b.
(Foldable m, Monad m) =>
(a -> b -> b) -> b -> CrossStream m a -> b
foldr :: forall a b. (a -> b -> b) -> b -> CrossStream m a -> b
$cfoldr :: forall (m :: * -> *) a b.
(Foldable m, Monad m) =>
(a -> b -> b) -> b -> CrossStream m a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> CrossStream m a -> m
$cfoldMap' :: forall (m :: * -> *) m a.
(Foldable m, Monad m, Monoid m) =>
(a -> m) -> CrossStream m a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> CrossStream m a -> m
$cfoldMap :: forall (m :: * -> *) m a.
(Foldable m, Monad m, Monoid m) =>
(a -> m) -> CrossStream m a -> m
fold :: forall m. Monoid m => CrossStream m m -> m
$cfold :: forall (m :: * -> *) m.
(Foldable m, Monad m, Monoid m) =>
CrossStream m m -> m
Foldable)
{-# INLINE mkCross #-}
mkCross :: Stream m a -> CrossStream m a
mkCross :: forall (m :: * -> *) a. Stream m a -> CrossStream m a
mkCross = forall (m :: * -> *) a. Stream m a -> CrossStream m a
CrossStream
{-# INLINE unCross #-}
unCross :: CrossStream m a -> Stream m a
unCross :: forall (m :: * -> *) a. CrossStream m a -> Stream m a
unCross = forall (m :: * -> *) a. CrossStream m a -> Stream m a
unCrossStream
deriving instance IsList (CrossStream Identity a)
deriving instance (a ~ Char) => IsString (CrossStream Identity a)
deriving instance Eq a => Eq (CrossStream Identity a)
deriving instance Ord a => Ord (CrossStream Identity a)
instance Show a => Show (CrossStream Identity a) where
{-# INLINE show #-}
show :: CrossStream Identity a -> [Char]
show (CrossStream Stream Identity a
xs) = forall a. Show a => a -> [Char]
show Stream Identity a
xs
instance Read a => Read (CrossStream Identity a) where
{-# INLINE readPrec #-}
readPrec :: ReadPrec (CrossStream Identity a)
readPrec = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall (m :: * -> *) a. Stream m a -> CrossStream m a
CrossStream forall a. Read a => ReadPrec a
readPrec
instance Monad m => Applicative (CrossStream m) where
{-# INLINE pure #-}
pure :: forall a. a -> CrossStream m a
pure a
x = forall (m :: * -> *) a. Stream m a -> CrossStream m a
CrossStream (forall (m :: * -> *) a. Applicative m => a -> Stream m a
fromPure a
x)
{-# INLINE (<*>) #-}
(CrossStream Stream m (a -> b)
s1) <*> :: forall a b.
CrossStream m (a -> b) -> CrossStream m a -> CrossStream m b
<*> (CrossStream Stream m a
s2) =
forall (m :: * -> *) a. Stream m a -> CrossStream m a
CrossStream (forall (f :: * -> *) a b.
Functor f =>
Stream f (a -> b) -> Stream f a -> Stream f b
crossApply Stream m (a -> b)
s1 Stream m a
s2)
{-# INLINE liftA2 #-}
liftA2 :: forall a b c.
(a -> b -> c)
-> CrossStream m a -> CrossStream m b -> CrossStream m c
liftA2 a -> b -> c
f CrossStream m a
x = forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
(<*>) (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b -> c
f CrossStream m a
x)
{-# INLINE (*>) #-}
(CrossStream Stream m a
s1) *> :: forall a b. CrossStream m a -> CrossStream m b -> CrossStream m b
*> (CrossStream Stream m b
s2) =
forall (m :: * -> *) a. Stream m a -> CrossStream m a
CrossStream (forall (f :: * -> *) a b.
Functor f =>
Stream f a -> Stream f b -> Stream f b
crossApplySnd Stream m a
s1 Stream m b
s2)
{-# INLINE (<*) #-}
(CrossStream Stream m a
s1) <* :: forall a b. CrossStream m a -> CrossStream m b -> CrossStream m a
<* (CrossStream Stream m b
s2) =
forall (m :: * -> *) a. Stream m a -> CrossStream m a
CrossStream (forall (f :: * -> *) a b.
Functor f =>
Stream f a -> Stream f b -> Stream f a
crossApplyFst Stream m a
s1 Stream m b
s2)
instance Monad m => Monad (CrossStream m) where
return :: forall a. a -> CrossStream m a
return = forall (f :: * -> *) a. Applicative f => a -> f a
pure
{-# INLINE (>>=) #-}
>>= :: forall a b.
CrossStream m a -> (a -> CrossStream m b) -> CrossStream m b
(>>=) (CrossStream Stream m a
m) a -> CrossStream m b
f = forall (m :: * -> *) a. Stream m a -> CrossStream m a
CrossStream (forall (m :: * -> *) a b.
Monad m =>
(a -> Stream m b) -> Stream m a -> Stream m b
concatMap (forall (m :: * -> *) a. CrossStream m a -> Stream m a
unCrossStream forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> CrossStream m b
f) Stream m a
m)
{-# INLINE (>>) #-}
>> :: forall a b. CrossStream m a -> CrossStream m b -> CrossStream m b
(>>) = forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
(*>)
instance (MonadIO m) => MonadIO (CrossStream m) where
liftIO :: forall a. IO a -> CrossStream m a
liftIO IO a
x = forall (m :: * -> *) a. Stream m a -> CrossStream m a
CrossStream (forall (m :: * -> *) a. Applicative m => m a -> Stream m a
fromEffect forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO a
x)
instance MonadTrans CrossStream where
{-# INLINE lift #-}
lift :: forall (m :: * -> *) a. Monad m => m a -> CrossStream m a
lift m a
x = forall (m :: * -> *) a. Stream m a -> CrossStream m a
CrossStream (forall (m :: * -> *) a. Applicative m => m a -> Stream m a
fromEffect m a
x)
instance (MonadThrow m) => MonadThrow (CrossStream m) where
throwM :: forall e a. Exception e => e -> CrossStream m a
throwM = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM