on master help sign in
Move rmapM to Fold/Types.hs
commite9a0e7c118a1e01bb493f2fb90ec2f7db7d56626 authorHarendra Kumar <harendra@composewell.com> dateWed 10 Mar 2021, 01:40 (6 years ago) parente95d056
5 files changed+25−13
Changelog.md+3−3 benchmark/Streamly/Benchmark/Data/Fold.hs+1−1 src/Streamly/Data/Fold.hs−1 src/Streamly/Internal/Data/Fold.hs+1−8 src/Streamly/Internal/Data/Fold/Types.hs+20
Changelog.md+3−3
@@ -64,9 +64,9 @@
6464 * `encodeLatin1Lax` to `encodeLatin1`
6565 * `decodeUtf8Lenient` to `decodeUtf8`
6666* Drop support for GHC 7.10.3.
67−* The following functions in `Streamly.Data.Fold` have been renamed:
68− * `mapM` to `rmapM`
69− * `sequence` to `rsequence`
67+* The following functions in `Streamly.Data.Fold` have been deprecated:
68+ * `mapM` is replaced by `rmapM`
69+ * `sequence` is dprecated, please use `rmapM id` instead.
7070
7171### Internal APIs
7272
benchmark/Streamly/Benchmark/Data/Fold.hs+1−1
@@ -260,7 +260,7 @@ o_1_space_serial_transformation value =
260260 , benchIOSink
261261 value
262262 "rsequence"
263− (S.fold (FL.rsequence (return <$> FL.drain)))
263+ (S.fold (FL.rmapM id (return <$> FL.drain)))
264264 , benchIOSink value "rmapM" (S.fold (FL.rmapM return FL.drain))
265265 , benchIOSink
266266 value
src/Streamly/Data/Fold.hs−1
@@ -128,7 +128,6 @@ module Streamly.Data.Fold
128128 --
129129 -- Note: Output transformations are also known as covariant
130130 -- transformations.
131− , rsequence
132131 , sequence
133132 , rmapM
134133 , mapM
src/Streamly/Internal/Data/Fold.hs+1−8
@@ -307,14 +307,7 @@ rsequence (Fold step initial extract) = Fold step' initial1 extract'
307307{-# DEPRECATED sequence "Use rsequence instead" #-}
308308{-# INLINE sequence #-}
309309sequence :: Monad m => Fold m a (m b) -> Fold m a b
310−sequence = rsequence
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
310+sequence = rmapM id
318311
319312-- | Map a monadic function on the output of a fold.
320313--
src/Streamly/Internal/Data/Fold/Types.hs+20
@@ -198,6 +198,7 @@ module Streamly.Internal.Data.Fold.Types
198198 , yieldM
199199
200200 -- * Transformations
201+ , rmapM
201202 , map
202203 , lmap
203204 , lmapM
@@ -300,6 +301,13 @@ instance Functor (Step s) where
300301 {-# INLINE fmap #-}
301302 fmap = second
302303
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+
303311-- The Step functor around b allows expressing early termination like a right
304312-- fold. Traditional list right folds use function composition and laziness to
305313-- terminate early whereas we use data constructors. It allows stream fusion in
@@ -326,6 +334,18 @@ data Fold m a b =
326334 -- | @Fold @ @ step @ @ initial @ @ extract@
327335 forall s. Fold (s -> a -> m (Step s b)) (m (Step s b)) (s -> m b)
328336
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+
329349------------------------------------------------------------------------------
330350-- Left fold constructors
331351------------------------------------------------------------------------------