@@ -126,6 +126,7 @@ module Streamly.Internal.Data.Fold
126 --, lsequence
126 --, lsequence
129 , postscan
130 , postscan
130 , indexed
131 , indexed
@@ -292,10 +293,9 @@ import qualified Data.IntSet as IntSet
292import qualified Data.Map.Strict as Map
293import qualified Data.Map.Strict as Map
293import qualified Data.Set as Set
294import qualified Data.Set as Set
294import qualified Streamly.Internal.Data.IsMap as IsMap
295import qualified Streamly.Internal.Data.IsMap as IsMap
296+import qualified Prelude
295import qualified Streamly.Internal.Data.Pipe.Type as Pipe
297import qualified Streamly.Internal.Data.Pipe.Type as Pipe
296import qualified Streamly.Internal.Data.Stream.StreamD.Type as StreamD
298import qualified Streamly.Internal.Data.Stream.StreamD.Type as StreamD
297−-- import qualified Streamly.Internal.Data.Stream.IsStream.Enumeration as Stream
298−import qualified Prelude
300import Prelude hiding
300import Prelude hiding
301 ( filter, foldl1, drop, dropWhile, take, takeWhile, zipWith
301 ( filter, foldl1, drop, dropWhile, take, takeWhile, zipWith
@@ -417,17 +417,13 @@ transform (Pipe pstep1 pstep2 pinitial) (Fold fstep finitial fextract) =
418 extract (Tuple' _ fs) = fextract fs
418 extract (Tuple' _ fs) = fextract fs
420−-- | Scan the input of a 'Fold' to change it in a stateful manner using another
423−{-# INLINE scan #-}
420+{-# INLINE scanWith #-}
424−scan :: Monad m => Fold m a b -> Fold m b c -> Fold m a c
421+scanWith :: Monad m => Bool -> Fold m a b -> Fold m b c -> Fold m a c
425−scan (Fold stepL initialL extractL) (Fold stepR initialR extractR) =
422+scanWith isMany (Fold stepL initialL extractL) (Fold stepR initialR extractR) =
426 Fold step initial extract
423 Fold step initial extract
430− -- XXX It can be moved out and used for both scan and postscan
431 {-# INLINE runStep #-}
427 {-# INLINE runStep #-}
432 runStep actionL sR = do
428 runStep actionL sR = do
433 rL <- actionL
429 rL <- actionL
@@ -435,7 +431,10 @@ scan (Fold stepL initialL extractL) (Fold stepR initialR extractR) =
435 Done bL -> do
431 Done bL -> do
436 rR <- stepR sR bL
432 rR <- stepR sR bL
437 case rR of
433 case rR of
436+ then runStep initialL sR1
438− Partial sR1 -> Done <$> extractR sR1
437+ else Done <$> extractR sR1
439 Done bR -> return $ Done bR
438 Done bR -> return $ Done bR
440 Partial sL -> do
439 Partial sL -> do
441 !b <- extractL sL
440 !b <- extractL sL
@@ -455,6 +454,24 @@ scan (Fold stepL initialL extractL) (Fold stepR initialR extractR) =
456 extract = extractR . snd
455 extract = extractR . snd
457+-- | Scan the input of a 'Fold' to change it in a stateful manner using another
458+-- 'Fold'. The scan stops as soon as the fold terminates.
462+scan :: Monad m => Fold m a b -> Fold m b c -> Fold m a c
463+scan = scanWith False
465+-- XXX This does not fuse beacuse of the recursive step. Need to investigate.
467+-- | Scan the input of a 'Fold' to change it in a stateful manner using another
468+-- 'Fold'. The scan restarts with a fresh state if the fold terminates.
471+{-# INLINE scanMany #-}
472+scanMany :: Monad m => Fold m a b -> Fold m b c -> Fold m a c
473+scanMany = scanWith True
458-- | Postscan the input of a 'Fold' to change it in a stateful manner using
475-- | Postscan the input of a 'Fold' to change it in a stateful manner using
459-- another 'Fold'.
476-- another 'Fold'.
460-- /Pre-release/
477-- /Pre-release/