1{-# OPTIONS_GHC -Wno-deprecations #-}
4-- Module : Streamly.Internal.Data.Stream.IsStream.Lift
5-- Copyright : (c) 2019 Composewell Technologies
6-- License : BSD-3-Clause
7-- Maintainer : streamly@composewell.com
8-- Stability : experimental
11module Streamly.Internal.Data.Stream.IsStream.Lift {-# DEPRECATED "Please use \"Streamly.Data.Stream.*\" instead." #-}
13 -- * Generalize Inner Monad
17 -- * Transform Inner Monad
29import Control.Monad.Trans.Reader (ReaderT)
30import Control.Monad.Trans.State.Strict (StateT)
31import Control.Monad.Trans.Class (MonadTrans(..))
32import Data.Functor.Identity (Identity (..))
33import Streamly.Internal.Data.Stream.IsStream.Type
34 (IsStream(..), fromStreamD, toStreamD)
35import Streamly.Internal.Data.Stream.Serial (SerialT)
37import qualified Streamly.Internal.Data.Stream.StreamD as D
38 (morphInner, liftInner, runReaderT, evalStateT, runStateT)
40------------------------------------------------------------------------------
41-- Generalize the underlying monad
42------------------------------------------------------------------------------
44-- | Transform the inner monad of a stream using a natural transformation.
49hoist :: (Monad m, Monad n)
50 => (forall x. m x -> n x) -> SerialT m a -> SerialT n a
51hoist f xs = fromStreamD $ D.morphInner f (toStreamD xs)
53-- | Generalize the inner monad of the stream from 'Identity' to any monad.
57{-# INLINE generally #-}
58generally :: (IsStream t, Monad m) => t Identity a -> t m a
59generally xs = fromStreamD $ D.morphInner (return . runIdentity) (toStreamD xs)
61------------------------------------------------------------------------------
62-- Add and remove a monad transformer
63------------------------------------------------------------------------------
65-- | Lift the inner monad @m@ of a stream @t m a@ to @tr m@ using the monad
70{-# INLINE liftInner #-}
71liftInner :: (Monad m, IsStream t, MonadTrans tr, Monad (tr m))
72 => t m a -> t (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'.
83{-# INLINE runReaderT #-}
84runReaderT :: (IsStream t, Monad m) => m s -> t (ReaderT s m) a -> t m a
85runReaderT s xs = fromStreamD $ D.runReaderT s (toStreamD xs)
87-- | Run a stream transformation using a given environment.
89-- See also: 'Serial.map'
93{-# INLINE usingReaderT #-}
95 :: (Monad m, IsStream t)
97 -> (t (ReaderT r m) a -> t (ReaderT r m) a)
100usingReaderT r f xs = runReaderT r $ f $ liftInner xs
102------------------------------------------------------------------------------
103-- Sharing read write state in a stream
104------------------------------------------------------------------------------
106-- | Evaluate the inner monad of a stream as 'StateT'.
108-- This is supported only for 'SerialT' as concurrent state updation may not be
111-- > evalStateT s = Stream.map snd . Stream.runStateT s
115{-# INLINE evalStateT #-}
116evalStateT :: Monad m => m s -> SerialT (StateT s m) a -> SerialT m a
117-- evalStateT s = fmap snd . runStateT s
118evalStateT s xs = fromStreamD $ D.evalStateT s (toStreamD xs)
120-- | Run a stateful (StateT) stream transformation using a given state.
122-- This is supported only for 'SerialT' as concurrent state updation may not be
125-- > usingStateT s f = evalStateT s . f . liftInner
131{-# INLINE usingStateT #-}
135 -> (SerialT (StateT s m) a -> SerialT (StateT s m) a)
138usingStateT s f = evalStateT s . f . liftInner
140-- | Evaluate the inner monad of a stream as 'StateT' and emit the resulting
141-- state and value pair after each step.
143-- This is supported only for 'SerialT' as concurrent state updation may not be
148{-# INLINE runStateT #-}
149runStateT :: Monad m => m s -> SerialT (StateT s m) a -> SerialT m (s, a)
150runStateT 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