2-- Module : Streamly.Internal.Data.Stream.Lift
3-- Copyright : (c) 2019 Composewell Technologies
4-- License : BSD-3-Clause
5-- Maintainer : streamly@composewell.com
6-- Stability : experimental
9module Streamly.Internal.Data.Stream.Lift
11 -- * Generalize Inner Monad
15 -- * Transform Inner Monad
27import Control.Monad.Trans.Reader (ReaderT)
28import Control.Monad.Trans.State.Strict (StateT)
29import Control.Monad.Trans.Class (MonadTrans(..))
30import Data.Functor.Identity (Identity (..))
31import Streamly.Internal.Data.Stream.Type
32 (Stream, fromStreamK, toStreamK, fromStreamD, toStreamD)
34import qualified Streamly.Internal.Data.Stream.StreamD as D
35import qualified Streamly.Internal.Data.Stream.StreamK as K
39-- >>> import Streamly.Internal.Data.Stream as Stream
41------------------------------------------------------------------------------
42-- Generalize the underlying monad
43------------------------------------------------------------------------------
45-- | Transform the inner monad of a stream using a natural transformation.
50hoist :: (Monad m, Monad n)
51 => (forall x. m x -> n x) -> Stream m a -> Stream n a
52hoist f xs = fromStreamK $ K.hoist f (toStreamK xs)
54-- | Generalize the inner monad of the stream from 'Identity' to any monad.
58{-# INLINE generally #-}
59generally :: Monad m => Stream Identity a -> Stream m a
60generally xs = fromStreamK $ K.hoist (return . runIdentity) (toStreamK xs)
62------------------------------------------------------------------------------
63-- Add and remove a monad transformer
64------------------------------------------------------------------------------
66-- | Lift the inner monad @m@ of a stream @Stream m a@ to @tr m@ using the monad
70{-# INLINE liftInner #-}
71liftInner :: (Monad m, MonadTrans tr, Monad (tr m))
72 => Stream m a -> Stream (tr m) a
73liftInner xs = fromStreamD $ D.liftInner (toStreamD xs)
75------------------------------------------------------------------------------
76-- Sharing read only state in a stream
77------------------------------------------------------------------------------
79-- | Evaluate the inner monad of a stream as 'ReaderT'.
82{-# INLINE runReaderT #-}
83runReaderT :: Monad m => m s -> Stream (ReaderT s m) a -> Stream m a
84runReaderT s xs = fromStreamD $ D.runReaderT s (toStreamD xs)
86-- | Run a stream transformation using a given environment.
88-- See also: 'Serial.map'
92{-# INLINE usingReaderT #-}
96 -> (Stream (ReaderT r m) a -> Stream (ReaderT r m) a)
99usingReaderT r f xs = runReaderT r $ f $ liftInner xs
101------------------------------------------------------------------------------
102-- Sharing read write state in a stream
103------------------------------------------------------------------------------
105-- | Evaluate the inner monad of a stream as 'StateT'.
107-- >>> evalStateT s = fmap snd . Stream.runStateT s
111{-# INLINE evalStateT #-}
112evalStateT :: Monad m => m s -> Stream (StateT s m) a -> Stream m a
113-- evalStateT s = fmap snd . runStateT s
114evalStateT s xs = fromStreamD $ D.evalStateT s (toStreamD xs)
116-- | Run a stateful (StateT) stream transformation using a given state.
118-- >>> usingStateT s f = Stream.evalStateT s . f . Stream.liftInner
124{-# INLINE usingStateT #-}
128 -> (Stream (StateT s m) a -> Stream (StateT s m) a)
131usingStateT s f = evalStateT s . f . liftInner
133-- | Evaluate the inner monad of a stream as 'StateT' and emit the resulting
134-- state and value pair after each step.
137{-# INLINE runStateT #-}
138runStateT :: Monad m => m s -> Stream (StateT s m) a -> Stream m (s, a)
139runStateT s xs = fromStreamD $ D.runStateT s (toStreamD xs)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list