on master help sign in
Move rmapM to Fold/Types.hs
e9a0e7cHarendra Kumar
Changelog.md+3−3
@@ -64,9 +64,9 @@
64 * `encodeLatin1Lax` to `encodeLatin1`
64 * `encodeLatin1Lax` to `encodeLatin1`
65 * `decodeUtf8Lenient` to `decodeUtf8`
65 * `decodeUtf8Lenient` to `decodeUtf8`
66* Drop support for GHC 7.10.3.
66* Drop support for GHC 7.10.3.
67−* The following functions in `Streamly.Data.Fold` have been renamed:
67+* The following functions in `Streamly.Data.Fold` have been deprecated:
68− * `mapM` to `rmapM`
68+ * `mapM` is replaced by `rmapM`
69− * `sequence` to `rsequence`
69+ * `sequence` is dprecated, please use `rmapM id` instead.
70
70
71### Internal APIs
71### Internal APIs
72
72
benchmark/Streamly/Benchmark/Data/Fold.hs+1−1
@@ -260,7 +260,7 @@ o_1_space_serial_transformation value =
260 , benchIOSink
260 , benchIOSink
261 value
261 value
262 "rsequence"
262 "rsequence"
263− (S.fold (FL.rsequence (return <$> FL.drain)))
263+ (S.fold (FL.rmapM id (return <$> FL.drain)))
264 , benchIOSink value "rmapM" (S.fold (FL.rmapM return FL.drain))
264 , benchIOSink value "rmapM" (S.fold (FL.rmapM return FL.drain))
265 , benchIOSink
265 , benchIOSink
266 value
266 value
src/Streamly/Data/Fold.hs−1
@@ -128,7 +128,6 @@ module Streamly.Data.Fold
128 --
128 --
129 -- Note: Output transformations are also known as covariant
129 -- Note: Output transformations are also known as covariant
130 -- transformations.
130 -- transformations.
131− , rsequence
132 , sequence
131 , sequence
133 , rmapM
132 , rmapM
134 , mapM
133 , mapM
src/Streamly/Internal/Data/Fold.hs+1−8
@@ -307,14 +307,7 @@ rsequence (Fold step initial extract) = Fold step' initial1 extract'
307{-# DEPRECATED sequence "Use rsequence instead" #-}
307{-# DEPRECATED sequence "Use rsequence instead" #-}
308{-# INLINE sequence #-}
308{-# INLINE sequence #-}
309sequence :: Monad m => Fold m a (m b) -> Fold m a b
309sequence :: Monad m => Fold m a (m b) -> Fold m a b
310−sequence = rsequence
310+sequence = rmapM id
311−
312−-- | Map a monadic function on the output of a fold.
313−--
314−-- @since 0.8.0
315−{-# INLINE rmapM #-}
316−rmapM :: Monad m => (b -> m c) -> Fold m a b -> Fold m a c
317−rmapM f = sequence . fmap f
318
311
319-- | Map a monadic function on the output of a fold.
312-- | Map a monadic function on the output of a fold.
320--
313--
src/Streamly/Internal/Data/Fold/Types.hs+20
@@ -198,6 +198,7 @@ module Streamly.Internal.Data.Fold.Types
198 , yieldM
198 , yieldM
199
199
200 -- * Transformations
200 -- * Transformations
201+ , rmapM
201 , map
202 , map
202 , lmap
203 , lmap
203 , lmapM
204 , lmapM
@@ -300,6 +301,13 @@ instance Functor (Step s) where
300 {-# INLINE fmap #-}
301 {-# INLINE fmap #-}
301 fmap = second
302 fmap = second
302
303
304+{-# INLINE mapMStep #-}
305+mapMStep :: Applicative m => (a -> m b) -> Step s a -> m (Step s b)
306+mapMStep f res =
307+ case res of
308+ Partial s -> pure $ Partial s
309+ Done b -> Done <$> f b
310+
303-- The Step functor around b allows expressing early termination like a right
311-- The Step functor around b allows expressing early termination like a right
304-- fold. Traditional list right folds use function composition and laziness to
312-- fold. Traditional list right folds use function composition and laziness to
305-- terminate early whereas we use data constructors. It allows stream fusion in
313-- terminate early whereas we use data constructors. It allows stream fusion in
@@ -326,6 +334,18 @@ data Fold m a b =
326 -- | @Fold @ @ step @ @ initial @ @ extract@
334 -- | @Fold @ @ step @ @ initial @ @ extract@
327 forall s. Fold (s -> a -> m (Step s b)) (m (Step s b)) (s -> m b)
335 forall s. Fold (s -> a -> m (Step s b)) (m (Step s b)) (s -> m b)
328
336
337+-- | Map a monadic function on the output of a fold.
338+--
339+-- @since 0.8.0
340+{-# INLINE rmapM #-}
341+rmapM :: Monad m => (b -> m c) -> Fold m a b -> Fold m a c
342+rmapM f (Fold step initial extract) = Fold step1 initial1 (extract >=> f)
343+
344+ where
345+
346+ initial1 = initial >>= mapMStep f
347+ step1 s a = step s a >>= mapMStep f
348+
329------------------------------------------------------------------------------
349------------------------------------------------------------------------------
330-- Left fold constructors
350-- Left fold constructors
331------------------------------------------------------------------------------
351------------------------------------------------------------------------------