@@ -198,6 +198,7 @@ module Streamly.Internal.Data.Fold.Types
200 -- * Transformations
200 -- * Transformations
@@ -300,6 +301,13 @@ instance Functor (Step s) where
300 {-# INLINE fmap #-}
301 {-# INLINE fmap #-}
301 fmap = second
302 fmap = second
304+{-# INLINE mapMStep #-}
305+mapMStep :: Applicative m => (a -> m b) -> Step s a -> m (Step s b)
308+ Partial s -> pure $ Partial s
309+ Done b -> Done <$> f b
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)
337+-- | Map a monadic function on the output of a fold.
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)
346+ initial1 = initial >>= mapMStep f
347+ step1 s a = step s a >>= mapMStep f
329------------------------------------------------------------------------------
349------------------------------------------------------------------------------
330-- Left fold constructors
350-- Left fold constructors
331------------------------------------------------------------------------------
351------------------------------------------------------------------------------