2-- Module : Streamly.Internal.Data.Stream.StreamD.Lift
3-- Copyright : (c) 2018 Composewell Technologies
4-- License : BSD-3-Clause
5-- Maintainer : streamly@composewell.com
6-- Stability : experimental
9-- Transform the underlying monad of a stream.
11module Streamly.Internal.Data.Stream.StreamD.Lift
13 -- * Generalize Inner Monad
15 , generally -- XXX generalize
17 -- * Transform Inner Monad
27import Control.Monad.Trans.Class (MonadTrans(lift))
28import Control.Monad.Trans.Reader (ReaderT)
29import Control.Monad.Trans.State.Strict (StateT)
30import Data.Functor.Identity (Identity(..))
31import Streamly.Internal.Data.SVar (adaptState)
33import qualified Control.Monad.Trans.Reader as Reader
34import qualified Control.Monad.Trans.State.Strict as State
36import Streamly.Internal.Data.Stream.StreamD.Type
38-------------------------------------------------------------------------------
39-- Generalize Inner Monad
40-------------------------------------------------------------------------------
42{-# INLINE_NORMAL hoist #-}
43hoist :: Monad n => (forall x. m x -> n x) -> Stream m a -> Stream n a
44hoist f (Stream step state) = (Stream step' state)
46 {-# INLINE_LATE step' #-}
48 r <- f $ step (adaptState gst) st
50 Yield x s -> Yield x s
54{-# INLINE generally #-}
55generally :: Monad m => Stream Identity a -> Stream m a
56generally = hoist (return . runIdentity)
58-------------------------------------------------------------------------------
59-- Transform Inner Monad
60-------------------------------------------------------------------------------
62{-# INLINE_NORMAL liftInner #-}
63liftInner :: (Monad m, MonadTrans t, Monad (t m))
64 => Stream m a -> Stream (t m) a
65liftInner (Stream step state) = Stream step' state
67 {-# INLINE_LATE step' #-}
69 r <- lift $ step (adaptState gst) st
71 Yield x s -> Yield x s
75{-# INLINE_NORMAL runReaderT #-}
76runReaderT :: Monad m => m s -> Stream (ReaderT s m) a -> Stream m a
77runReaderT env (Stream step state) = Stream step' (state, env)
79 {-# INLINE_LATE step' #-}
80 step' gst (st, action) = do
82 r <- Reader.runReaderT (step (adaptState gst) st) sv
84 Yield x s -> Yield x (s, return sv)
85 Skip s -> Skip (s, return sv)
88{-# INLINE_NORMAL evalStateT #-}
89evalStateT :: Monad m => m s -> Stream (StateT s m) a -> Stream m a
90evalStateT initial (Stream step state) = Stream step' (state, initial)
92 {-# INLINE_LATE step' #-}
93 step' gst (st, action) = do
95 (r, !sv') <- State.runStateT (step (adaptState gst) st) sv
97 Yield x s -> Yield x (s, return sv')
98 Skip s -> Skip (s, return sv')
101{-# INLINE_NORMAL runStateT #-}
102runStateT :: Monad m => m s -> Stream (StateT s m) a -> Stream m (s, a)
103runStateT initial (Stream step state) = Stream step' (state, initial)
105 {-# INLINE_LATE step' #-}
106 step' gst (st, action) = do
108 (r, !sv') <- State.runStateT (step (adaptState gst) st) sv
110 Yield x s -> Yield (sv', x) (s, return sv')
111 Skip s -> Skip (s, return sv')
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