on master help sign in
add lmapM and mapM
e7d2a73Harendra Kumar
src/Streamly/Internal/Data/Unfold.hs+22−1
@@ -62,6 +62,7 @@ module Streamly.Internal.Data.Unfold
62
62
63 -- * Operations on Input
63 -- * Operations on Input
64 , lmap
64 , lmap
65+ , lmapM
65 , supply
66 , supply
66 , supplyFirst
67 , supplyFirst
67 , supplySecond
68 , supplySecond
@@ -94,6 +95,7 @@ module Streamly.Internal.Data.Unfold
94
95
95 -- * Transformations
96 -- * Transformations
96 , map
97 , map
98+ , mapM
97
99
98 -- * Filtering
100 -- * Filtering
99 , takeWhileM
101 , takeWhileM
@@ -121,7 +123,7 @@ where
121import Control.Exception (Exception)
123import Control.Exception (Exception)
122import Data.Void (Void)
124import Data.Void (Void)
123import GHC.Types (SPEC(..))
125import GHC.Types (SPEC(..))
124−import Prelude hiding (concat, map, takeWhile, take, filter, const)
126+import Prelude hiding (concat, map, mapM, takeWhile, take, filter, const)
125
127
126import Streamly.Internal.Data.Stream.StreamD.Type (Stream(..), Step(..))
128import Streamly.Internal.Data.Stream.StreamD.Type (Stream(..), Step(..))
127#if __GLASGOW_HASKELL__ < 800
129#if __GLASGOW_HASKELL__ < 800
@@ -149,6 +151,13 @@ import qualified Streamly.Streams.StreamD as D
149lmap :: (a -> c) -> Unfold m c b -> Unfold m a b
151lmap :: (a -> c) -> Unfold m c b -> Unfold m a b
150lmap f (Unfold ustep uinject) = Unfold ustep (uinject . f)
152lmap f (Unfold ustep uinject) = Unfold ustep (uinject . f)
151
153
154+-- | Map an action on the input argument of the 'Unfold'.
155+--
156+-- /Internal/
157+{-# INLINE_NORMAL lmapM #-}
158+lmapM :: Monad m => (a -> m c) -> Unfold m c b -> Unfold m a b
159+lmapM f (Unfold ustep uinject) = Unfold ustep (\x -> f x >>= uinject)
160+
152-- | Supply the seed to an unfold closing the input end of the unfold.
161-- | Supply the seed to an unfold closing the input end of the unfold.
153--
162--
154-- /Internal/
163-- /Internal/
@@ -244,6 +253,18 @@ map f (Unfold ustep uinject) = Unfold step uinject
244 Skip s -> Skip s
253 Skip s -> Skip s
245 Stop -> Stop
254 Stop -> Stop
246
255
256+{-# INLINE_NORMAL mapM #-}
257+mapM :: Monad m => (b -> m c) -> Unfold m a b -> Unfold m a c
258+mapM f (Unfold ustep uinject) = Unfold step uinject
259+ where
260+ {-# INLINE_LATE step #-}
261+ step st = do
262+ r <- ustep st
263+ case r of
264+ Yield x s -> f x >>= \a -> return $ Yield a s
265+ Skip s -> return $ Skip s
266+ Stop -> return $ Stop
267+
247-------------------------------------------------------------------------------
268-------------------------------------------------------------------------------
248-- Convert streams into unfolds
269-- Convert streams into unfolds
249-------------------------------------------------------------------------------
270-------------------------------------------------------------------------------