@@ -218,6 +218,7 @@ module Streamly.Internal.Data.Fold.Type
218 -- * Nested Application
218 -- * Nested Application
219 , concatMap
219 , concatMap
221 , intervalsOf
222 , intervalsOf
222 , chunksOf
223 , chunksOf
223 , chunksOf2
224 , chunksOf2
@@ -1006,6 +1007,61 @@ many (Fold sstep sinitial sextract) (Fold cstep cinitial cextract) =
1006 Partial s -> cextract s
1007 Partial s -> cextract s
1007 Done b -> return b
1008 Done b -> return b
1010+-- |Like many, but inner fold emits an output at the end even if no input is
1015+-- /See also: Streamly.Prelude.concatMap, Streamly.Prelude.foldMany/
1017+{-# INLINE manyPost #-}
1018+manyPost :: Monad m => Fold m a b -> Fold m b c -> Fold m a c
1019+manyPost (Fold sstep sinitial sextract) (Fold cstep cinitial cextract) =
1020+ Fold step initial extract
1024+ -- cs = collect state
1025+ -- ss = split state
1026+ -- cres = collect state result
1027+ -- sres = split state result
1028+ -- cb = collect done
1031+ -- Caution! There is mutual recursion here, inlining the right functions is
1034+ {-# INLINE handleSplitStep #-}
1035+ handleSplitStep cs sres =
1037+ Partial ss1 -> return $ Partial $ Tuple' ss1 cs
1038+ Done sb -> runCollector cs sb
1040+ {-# INLINE handleCollectStep #-}
1041+ handleCollectStep cres =
1045+ handleSplitStep cs sres
1046+ Done cb -> return $ Done cb
1048+ -- Do not inline this
1049+ runCollector cs sb = cstep cs sb >>= handleCollectStep
1051+ initial = cinitial >>= handleCollectStep
1053+ {-# INLINE step #-}
1054+ step (Tuple' ss cs) a = do
1056+ handleSplitStep cs sres
1058+ extract (Tuple' ss cs) = do
1060+ cres <- cstep cs sb
1062+ Partial s -> cextract s
1009-- | @chunksOf n split collect@ repeatedly applies the @split@ fold to chunks
1065-- | @chunksOf n split collect@ repeatedly applies the @split@ fold to chunks
1010-- of @n@ items in the input stream and supplies the result to the @collect@
1066-- of @n@ items in the input stream and supplies the result to the @collect@