module Streamly.Internal.Data.Producer
( Producer (..)
, simplify
, nil
, nilM
, unfoldrM
, fromStreamD
, fromList
, NestedLoop (..)
, concat
)
where
#include "inline.hs"
import Streamly.Internal.Data.Stream.StreamD.Step (Step(..))
import Streamly.Internal.Data.Stream.StreamD.Type (Stream(..))
import Streamly.Internal.Data.SVar.Type (defState)
import Streamly.Internal.Data.Unfold.Type (Unfold(..))
import Streamly.Internal.Data.Producer.Type
import Prelude hiding (concat)
{-# INLINE simplify #-}
simplify :: Producer m a b -> Unfold m a b
simplify :: Producer m a b -> Unfold m a b
simplify (Producer s -> m (Step s b)
step a -> m s
inject s -> m a
_) = (s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold s -> m (Step s b)
step a -> m s
inject
{-# INLINE_NORMAL fromStreamD #-}
fromStreamD :: Monad m => Producer m (Stream m a) a
fromStreamD :: Producer m (Stream m a) a
fromStreamD = (Stream m a -> m (Step (Stream m a) a))
-> (Stream m a -> m (Stream m a))
-> (Stream m a -> m (Stream m a))
-> Producer m (Stream m a) a
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> (s -> m a) -> Producer m a b
Producer Stream m a -> m (Step (Stream m a) a)
forall (m :: * -> *) a.
Monad m =>
Stream m a -> m (Step (Stream m a) a)
step Stream m a -> m (Stream m a)
forall (m :: * -> *) a. Monad m => a -> m a
return Stream m a -> m (Stream m a)
forall (m :: * -> *) a. Monad m => a -> m a
return
where
{-# INLINE_LATE step #-}
step :: Stream m a -> m (Step (Stream m a) a)
step (UnStream State Stream m a -> s -> m (Step s a)
step1 s
state1) = do
Step s a
r <- State Stream m a -> s -> m (Step s a)
step1 State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState s
state1
Step (Stream m a) a -> m (Step (Stream m a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Stream m a) a -> m (Step (Stream m a) a))
-> Step (Stream m a) a -> m (Step (Stream m a) a)
forall a b. (a -> b) -> a -> b
$ case Step s a
r of
Yield a
x s
s -> a -> Stream m a -> Step (Stream m a) a
forall s a. a -> s -> Step s a
Yield a
x ((State Stream m a -> s -> m (Step s a)) -> s -> Stream m a
forall (m :: * -> *) a s.
(State Stream m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State Stream m a -> s -> m (Step s a)
step1 s
s)
Skip s
s -> Stream m a -> Step (Stream m a) a
forall s a. s -> Step s a
Skip ((State Stream m a -> s -> m (Step s a)) -> s -> Stream m a
forall (m :: * -> *) a s.
(State Stream m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State Stream m a -> s -> m (Step s a)
step1 s
s)
Step s a
Stop -> Step (Stream m a) a
forall s a. Step s a
Stop