@@ -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------------------------------------------------------------------------------