@@ -895,14 +895,7 @@ data FoldManyPost s fs b a
895895 | FoldManyPostYield b (FoldManyPost s fs b a)
896896 | FoldManyPostDone
897897
898−-- | Like foldMany but with the following differences:
899−--
900−-- * If the stream is empty the default value of the fold would still be
901−-- emitted in the output.
902−-- * At the end of the stream if the last application of the fold did not
903−-- receive any input it would still yield the default fold accumulator as the
904−-- last value.
905−--
898+-- | 'Streamly.Internal.Data.Stream.foldManyPost'.
906899{-# INLINE_NORMAL foldManyPost #-}
907900foldManyPost :: Monad m => Fold m a b -> Stream m a -> Stream m b
908901foldManyPost (Fold fstep initial extract) (Stream step state) =
@@ -941,23 +934,14 @@ foldManyPost (Fold fstep initial extract) (Stream step state) =
941934{-# ANN type FoldMany Fuse #-}
942935data FoldMany s fs b a
943936 = FoldManyStart s
944− | FoldManyFirst fs s
937+ | FoldManyFirst a s
945938 | FoldManyLoop s fs
946939 | FoldManyYield b (FoldMany s fs b a)
947940 | FoldManyDone
948941
949942-- XXX Nested foldMany does not fuse.
950−--
951−-- | Apply a fold multiple times until the stream ends. If the stream is empty
952−-- the output would be empty.
953−--
954−-- @foldMany f = parseMany (fromFold f)@
955−--
956−-- A terminating fold may terminate even without accepting a single input. So
957−-- we run the fold's initial action before evaluating the stream. However, this
958−-- means that if later the stream does not yield anything we have to discard
959−-- the fold's initial result which could have generated an effect.
960−--
943+
944+-- | 'Streamly.Internal.Data.Stream.foldMany'.
961945{-# INLINE_NORMAL foldMany #-}
962946foldMany :: Monad m => Fold m a b -> Stream m a -> Stream m b
963947foldMany (Fold fstep initial extract) (Stream step state) =
@@ -975,19 +959,17 @@ foldMany (Fold fstep initial extract) (Stream step state) =
975959 FL.Partial ps -> FoldManyLoop s ps
976960
977961 {-# INLINE_LATE step' #-}
978− step' _ (FoldManyStart st) = do
979− r <- initial
980− return
981− $ Skip
982− $ case r of
983− FL.Done b -> FoldManyYield b (FoldManyStart st)
984− FL.Partial fs -> FoldManyFirst fs st
985− step' gst (FoldManyFirst fs st) = do
962+ step' gst (FoldManyStart st) = do
986963 r <- step (adaptState gst) st
987964 case r of
988− Yield x s -> consume x s fs
989− Skip s -> return $ Skip (FoldManyFirst fs s)
965+ Yield x s -> return $ Skip (FoldManyFirst x s)
966+ Skip s -> return $ Skip (FoldManyStart s)
990967 Stop -> return Stop
968+ step' _ (FoldManyFirst x st) = do
969+ r <- initial
970+ case r of
971+ FL.Done b -> return $ Skip $ FoldManyYield b (FoldManyFirst x st)
972+ FL.Partial fs -> consume x st fs
991973 step' gst (FoldManyLoop st fs) = do
992974 r <- step (adaptState gst) st
993975 case r of
@@ -1003,8 +985,8 @@ foldMany (Fold fstep initial extract) (Stream step state) =
1003985chunksOf :: Monad m => Int -> Fold m a b -> Stream m a -> Stream m b
1004986chunksOf n f = foldMany (FL.take n f)
1005987
1006−-- Keep the argument order consistent with consumeIterateM.
1007−--
988+-- Keep the argument order consistent with refoldIterateM.
989+
1008990-- | Like 'foldMany' but for the 'Refold' type. The supplied action is used as
1009991-- the initial value for each refold.
1010992--
@@ -1026,19 +1008,17 @@ refoldMany (Refold fstep inject extract) action (Stream step state) =
10261008 FL.Partial ps -> FoldManyLoop s ps
10271009
10281010 {-# INLINE_LATE step' #-}
1029− step' _ (FoldManyStart st) = do
1030− r <- action >>= inject
1031− return
1032− $ Skip
1033− $ case r of
1034− FL.Done b -> FoldManyYield b (FoldManyStart st)
1035− FL.Partial fs -> FoldManyFirst fs st
1036− step' gst (FoldManyFirst fs st) = do
1011+ step' gst (FoldManyStart st) = do
10371012 r <- step (adaptState gst) st
10381013 case r of
1039− Yield x s -> consume x s fs
1040− Skip s -> return $ Skip (FoldManyFirst fs s)
1014+ Yield x s -> return $ Skip (FoldManyFirst x s)
1015+ Skip s -> return $ Skip (FoldManyStart s)
10411016 Stop -> return Stop
1017+ step' _ (FoldManyFirst x st) = do
1018+ r <- action >>= inject
1019+ case r of
1020+ FL.Done b -> return $ Skip $ FoldManyYield b (FoldManyFirst x st)
1021+ FL.Partial fs -> consume x st fs
10421022 step' gst (FoldManyLoop st fs) = do
10431023 r <- step (adaptState gst) st
10441024 case r of