on master help sign in
add lmapM and mapM
commite7d2a732bda1d66d51cada3f02cc1938a2bbb309 authorHarendra Kumar <harendra.kumar@gmail.com> dateFri 8 Nov 2019, 17:58 (7 years ago) parenta1e093a
1 file changed+22−1
src/Streamly/Internal/Data/Unfold.hs+22−1
src/Streamly/Internal/Data/Unfold.hs+22−1
@@ -62,6 +62,7 @@ module Streamly.Internal.Data.Unfold
6262
6363 -- * Operations on Input
6464 , lmap
65+ , lmapM
6566 , supply
6667 , supplyFirst
6768 , supplySecond
@@ -94,6 +95,7 @@ module Streamly.Internal.Data.Unfold
9495
9596 -- * Transformations
9697 , map
98+ , mapM
9799
98100 -- * Filtering
99101 , takeWhileM
@@ -121,7 +123,7 @@ where
121123import Control.Exception (Exception)
122124import Data.Void (Void)
123125import GHC.Types (SPEC(..))
124−import Prelude hiding (concat, map, takeWhile, take, filter, const)
126+import Prelude hiding (concat, map, mapM, takeWhile, take, filter, const)
125127
126128import Streamly.Internal.Data.Stream.StreamD.Type (Stream(..), Step(..))
127129#if __GLASGOW_HASKELL__ < 800
@@ -149,6 +151,13 @@ import qualified Streamly.Streams.StreamD as D
149151lmap :: (a -> c) -> Unfold m c b -> Unfold m a b
150152lmap f (Unfold ustep uinject) = Unfold ustep (uinject . f)
151153
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+
152161-- | Supply the seed to an unfold closing the input end of the unfold.
153162--
154163-- /Internal/
@@ -244,6 +253,18 @@ map f (Unfold ustep uinject) = Unfold step uinject
244253 Skip s -> Skip s
245254 Stop -> Stop
246255
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+
247268-------------------------------------------------------------------------------
248269-- Convert streams into unfolds
249270-------------------------------------------------------------------------------