2-- Module : Streamly.Internal.Data.Stream.IsStream.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.IsStream.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.IsStream.Type
32 (IsStream(..), fromStreamD, toStreamD)
33import Streamly.Internal.Data.Stream.Serial (SerialT)
35import qualified Streamly.Internal.Data.Stream.StreamD as D
36 (hoist, liftInner, runReaderT, evalStateT, runStateT)
38------------------------------------------------------------------------------
39-- Generalize the underlying monad
40------------------------------------------------------------------------------
42-- | Transform the inner monad of a stream using a natural transformation.
47hoist :: (Monad m, Monad n)
48 => (forall x. m x -> n x) -> SerialT m a -> SerialT n a
49hoist f xs = fromStreamD $ D.hoist f (toStreamD xs)
51-- | Generalize the inner monad of the stream from 'Identity' to any monad.
55{-# INLINE generally #-}
56generally :: (IsStream t, Monad m) => t Identity a -> t m a
57generally xs = fromStreamD $ D.hoist (return . runIdentity) (toStreamD xs)
59------------------------------------------------------------------------------
60-- Add and remove a monad transformer
61------------------------------------------------------------------------------
63-- | Lift the inner monad @m@ of a stream @t m a@ to @tr m@ using the monad
68{-# INLINE liftInner #-}
69liftInner :: (Monad m, IsStream t, MonadTrans tr, Monad (tr m))
70 => t m a -> t (tr m) a
71liftInner xs = fromStreamD $ D.liftInner (toStreamD xs)
73------------------------------------------------------------------------------
74-- Sharing read only state in a stream
75------------------------------------------------------------------------------
77-- | Evaluate the inner monad of a stream as 'ReaderT'.
81{-# INLINE runReaderT #-}
82runReaderT :: (IsStream t, Monad m) => m s -> t (ReaderT s m) a -> t m a
83runReaderT s xs = fromStreamD $ D.runReaderT s (toStreamD xs)
85-- | Run a stream transformation using a given environment.
87-- See also: 'Serial.map'
91{-# INLINE usingReaderT #-}
93 :: (Monad m, IsStream t)
95 -> (t (ReaderT r m) a -> t (ReaderT r m) a)
98usingReaderT r f xs = runReaderT r $ f $ liftInner xs
100------------------------------------------------------------------------------
101-- Sharing read write state in a stream
102------------------------------------------------------------------------------
104-- | Evaluate the inner monad of a stream as 'StateT'.
106-- This is supported only for 'SerialT' as concurrent state updation may not be
109-- > evalStateT s = Stream.map snd . Stream.runStateT s
113{-# INLINE evalStateT #-}
114evalStateT :: Monad m => m s -> SerialT (StateT s m) a -> SerialT m a
115-- evalStateT s = fmap snd . runStateT s
116evalStateT s xs = fromStreamD $ D.evalStateT s (toStreamD xs)
118-- | Run a stateful (StateT) stream transformation using a given state.
120-- This is supported only for 'SerialT' as concurrent state updation may not be
123-- > usingStateT s f = evalStateT s . f . liftInner
129{-# INLINE usingStateT #-}
133 -> (SerialT (StateT s m) a -> SerialT (StateT s m) a)
136usingStateT s f = evalStateT s . f . liftInner
138-- | Evaluate the inner monad of a stream as 'StateT' and emit the resulting
139-- state and value pair after each step.
141-- This is supported only for 'SerialT' as concurrent state updation may not be
146{-# INLINE runStateT #-}
147runStateT :: Monad m => m s -> SerialT (StateT s m) a -> SerialT m (s, a)
148runStateT 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