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(..), fromStreamS, toStreamS, fromStreamD, toStreamD)
33import Streamly.Internal.Data.Stream.Serial (SerialT)
35import qualified Streamly.Internal.Data.Stream.StreamD as D
36#ifdef USE_STREAMK_ONLY
37import qualified Streamly.Internal.Data.Stream.StreamK as S
39import qualified Streamly.Internal.Data.Stream.StreamD as S
42------------------------------------------------------------------------------
43-- Generalize the underlying monad
44------------------------------------------------------------------------------
46-- | Transform the inner monad of a stream using a natural transformation.
51hoist :: (Monad m, Monad n)
52 => (forall x. m x -> n x) -> SerialT m a -> SerialT n a
53hoist f xs = fromStreamS $ S.hoist f (toStreamS xs)
55-- | Generalize the inner monad of the stream from 'Identity' to any monad.
59{-# INLINE generally #-}
60generally :: (IsStream t, Monad m) => t Identity a -> t m a
61generally xs = fromStreamS $ S.hoist (return . runIdentity) (toStreamS xs)
63------------------------------------------------------------------------------
64-- Add and remove a monad transformer
65------------------------------------------------------------------------------
67-- | Lift the inner monad @m@ of a stream @t m a@ to @tr m@ using the monad
72{-# INLINE liftInner #-}
73liftInner :: (Monad m, IsStream t, MonadTrans tr, Monad (tr m))
74 => t m a -> t (tr m) a
75liftInner xs = fromStreamD $ D.liftInner (toStreamD xs)
77------------------------------------------------------------------------------
78-- Sharing read only state in a stream
79------------------------------------------------------------------------------
81-- | Evaluate the inner monad of a stream as 'ReaderT'.
85{-# INLINE runReaderT #-}
86runReaderT :: (IsStream t, Monad m) => m s -> t (ReaderT s m) a -> t m a
87runReaderT s xs = fromStreamD $ D.runReaderT s (toStreamD xs)
89-- | Run a stream transformation using a given environment.
91-- See also: 'Serial.map'
95{-# INLINE usingReaderT #-}
97 :: (Monad m, IsStream t)
99 -> (t (ReaderT r m) a -> t (ReaderT r m) a)
102usingReaderT r f xs = runReaderT r $ f $ liftInner xs
104------------------------------------------------------------------------------
105-- Sharing read write state in a stream
106------------------------------------------------------------------------------
108-- | Evaluate the inner monad of a stream as 'StateT'.
110-- This is supported only for 'SerialT' as concurrent state updation may not be
113-- > evalStateT s = Stream.map snd . Stream.runStateT s
117{-# INLINE evalStateT #-}
118evalStateT :: Monad m => m s -> SerialT (StateT s m) a -> SerialT m a
119-- evalStateT s = fmap snd . runStateT s
120evalStateT s xs = fromStreamD $ D.evalStateT s (toStreamD xs)
122-- | Run a stateful (StateT) stream transformation using a given state.
124-- This is supported only for 'SerialT' as concurrent state updation may not be
127-- > usingStateT s f = evalStateT s . f . liftInner
133{-# INLINE usingStateT #-}
137 -> (SerialT (StateT s m) a -> SerialT (StateT s m) a)
140usingStateT s f = evalStateT s . f . liftInner
142-- | Evaluate the inner monad of a stream as 'StateT' and emit the resulting
143-- state and value pair after each step.
145-- This is supported only for 'SerialT' as concurrent state updation may not be
150{-# INLINE runStateT #-}
151runStateT :: Monad m => m s -> SerialT (StateT s m) a -> SerialT m (s, a)
152runStateT 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