on master help sign in
Add liftInner and evalStateT to StreamK
0780dbbHarendra Kumar
core/src/Streamly/Internal/Data/Stream/StreamK/Type.hs+38−1
@@ -96,17 +96,23 @@ module Streamly.Internal.Data.Stream.StreamK.Type
96
96
97 -- * Reader
97 -- * Reader
98 , withLocal
98 , withLocal
99+ , evalStateT
100+ , liftInner
99 )
101 )
100where
102where
101
103
102import Control.Monad (ap, (>=>))
104import Control.Monad (ap, (>=>))
103import Control.Monad.Reader.Class (MonadReader(..))
105import Control.Monad.Reader.Class (MonadReader(..))
106+import Control.Monad.State.Strict (StateT)
104import Control.Monad.Trans.Class (MonadTrans(lift))
107import Control.Monad.Trans.Class (MonadTrans(lift))
105import Data.Function (fix)
108import Data.Function (fix)
106import Streamly.Internal.Data.SVar.Type (State, adaptState, defState)
109import Streamly.Internal.Data.SVar.Type (State, adaptState, defState)
110+
111+import qualified Control.Monad.State.Strict as State
112+import qualified Prelude
113+
107import Prelude hiding
114import Prelude hiding
108 (map, mapM, concatMap, foldr, repeat, null, reverse, tail, init)
115 (map, mapM, concatMap, foldr, repeat, null, reverse, tail, init)
109−import qualified Prelude
110
116
111------------------------------------------------------------------------------
117------------------------------------------------------------------------------
112-- Basic stream type
118-- Basic stream type
@@ -1368,3 +1374,34 @@ foldlS step = go
1368{-# INLINE reverse #-}
1374{-# INLINE reverse #-}
1369reverse :: Stream m a -> Stream m a
1375reverse :: Stream m a -> Stream m a
1370reverse = foldlS (flip cons) nil
1376reverse = foldlS (flip cons) nil
1377+
1378+------------------------------------------------------------------------------
1379+-- Lifting inner monad
1380+------------------------------------------------------------------------------
1381+
1382+{-# INLINE evalStateT #-}
1383+evalStateT :: Monad m => m s -> Stream (StateT s m) a -> Stream m a
1384+evalStateT = go
1385+
1386+ where
1387+
1388+ -- XXX Do not use stream monad
1389+ go st m1 = do
1390+ (res, s1) <- lift $ st >>= State.runStateT (uncons m1)
1391+ case res of
1392+ Just (h, t) -> cons h (go (return s1) t)
1393+ Nothing -> nil
1394+
1395+{-# INLINE liftInner #-}
1396+liftInner :: (Monad m, MonadTrans t, Monad (t m)) =>
1397+ Stream m a -> Stream (t m) a
1398+liftInner = go
1399+
1400+ where
1401+
1402+ -- XXX Do not use stream monad
1403+ go m1 = do
1404+ res <- fromEffect $ lift $ uncons m1
1405+ case res of
1406+ Just (h, t) -> cons h (go t)
1407+ Nothing -> nil