@@ -217,6 +217,7 @@ module Streamly.Internal.Data.Fold.Type
217217
218218 -- * Nested Application
219219 , concatMap
220+ , ManyState
220221 , many
221222 , manyPost
222223 , intervalsOf
@@ -949,6 +950,11 @@ runStep (Fold step initial extract) a = return $ Fold step initial1 extract
949950-- applied to a fold input stream. groupBy et al can be written as terminating
950951-- folds and then we can apply "many" to use those repeatedly on a stream.
951952
953+{-# ANN type ManyState Fuse #-}
954+data ManyState s1 s2
955+ = ManyFirst !s1 !s2
956+ | ManyLoop !s1 !s2
957+
952958-- | Collect zero or more applications of a fold. @many split collect@ applies
953959-- the @split@ fold repeatedly on the input stream and accumulates zero or more
954960-- fold results using @collect@.
@@ -977,30 +983,35 @@ many (Fold sstep sinitial sextract) (Fold cstep cinitial cextract) =
977983 -- important.
978984
979985 {-# INLINE handleSplitStep #-}
980− handleSplitStep cs sres =
986+ handleSplitStep branch cs sres =
981987 case sres of
982− Partial ss1 -> return $ Partial $ Tuple' ss1 cs
983− Done sb -> runCollector cs sb
988+ Partial ss1 -> return $ Partial $ branch ss1 cs
989+ Done sb -> runCollector ManyFirst cs sb
984990
985991 {-# INLINE handleCollectStep #-}
986− handleCollectStep cres =
992+ handleCollectStep branch cres =
987993 case cres of
988994 Partial cs -> do
989995 sres <- sinitial
990− handleSplitStep cs sres
996+ handleSplitStep branch cs sres
991997 Done cb -> return $ Done cb
992998
993999 -- Do not inline this
994− runCollector cs sb = cstep cs sb >>= handleCollectStep
1000+ runCollector branch cs sb = cstep cs sb >>= handleCollectStep branch
9951001
996− initial = cinitial >>= handleCollectStep
1002+ initial = cinitial >>= handleCollectStep ManyFirst
9971003
998− {-# INLINE step #-}
999− step (Tuple' ss cs) a = do
1004+ {-# INLINE step_ #-}
1005+ step_ ss cs a = do
10001006 sres <- sstep ss a
1001− handleSplitStep cs sres
1007+ handleSplitStep ManyLoop cs sres
10021008
1003− extract (Tuple' ss cs) = do
1009+ {-# INLINE step #-}
1010+ step (ManyFirst ss cs) a = step_ ss cs a
1011+ step (ManyLoop ss cs) a = step_ ss cs a
1012+
1013+ extract (ManyFirst _ cs) = cextract cs
1014+ extract (ManyLoop ss cs) = do
10041015 sb <- sextract ss
10051016 cres <- cstep cs sb
10061017 case cres of