@@ -113,6 +113,7 @@ module Streamly.Internal.Data.Fold
113 --, lsequence
113 --, lsequence
116 , indexed
117 , indexed
118 -- ** Filtering
119 -- ** Filtering
@@ -390,6 +391,8 @@ scan (Fold stepL initialL extractL) (Fold stepR initialR extractR) =
394+ -- XXX It can be moved out and used for both scan and postscan
395+ {-# INLINE runStep #-}
393 runStep actionL sR = do
396 runStep actionL sR = do
394 rL <- actionL
397 rL <- actionL
395 case rL of
398 case rL of
@@ -416,6 +419,47 @@ scan (Fold stepL initialL extractL) (Fold stepR initialR extractR) =
417 extract = extractR . snd
420 extract = extractR . snd
422+-- | Postscan the input of a 'Fold' to change it in a stateful manner using
425+{-# INLINE postscan #-}
426+postscan :: Monad m => Fold m a b -> Fold m b c -> Fold m a c
427+postscan (Fold stepL initialL extractL) (Fold stepR initialR extractR) =
428+ Fold step initial extract
432+ {-# INLINE runStep #-}
433+ runStep actionL sR = do
439+ Partial sR1 -> Done <$> extractR sR1
440+ Done bR -> return $ Done bR
446+ Partial sR1 -> Partial (sL, sR1)
455+ Done _ -> Done <$> extractR sR
456+ Partial sL -> return $ Partial (sL, sR)
457+ Done b -> return $ Done b
459+ step (sL, sR) x = runStep (stepL sL x) sR
461+ extract = extractR . snd
419------------------------------------------------------------------------------
463------------------------------------------------------------------------------
420-- Left folds
464-- Left folds
421------------------------------------------------------------------------------
465------------------------------------------------------------------------------