@@ -895,14 +895,7 @@ data FoldManyPost s fs b a
895 | FoldManyPostYield b (FoldManyPost s fs b a)
895 | FoldManyPostYield b (FoldManyPost s fs b a)
896 | FoldManyPostDone
896 | FoldManyPostDone
898−-- | Like foldMany but with the following differences:
898+-- | 'Streamly.Internal.Data.Stream.foldManyPost'.
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
906{-# INLINE_NORMAL foldManyPost #-}
899{-# INLINE_NORMAL foldManyPost #-}
907foldManyPost :: Monad m => Fold m a b -> Stream m a -> Stream m b
900foldManyPost :: Monad m => Fold m a b -> Stream m a -> Stream m b
908foldManyPost (Fold fstep initial extract) (Stream step state) =
901foldManyPost (Fold fstep initial extract) (Stream step state) =
@@ -941,23 +934,14 @@ foldManyPost (Fold fstep initial extract) (Stream step state) =
941{-# ANN type FoldMany Fuse #-}
934{-# ANN type FoldMany Fuse #-}
942data FoldMany s fs b a
935data FoldMany s fs b a
943 = FoldManyStart s
936 = FoldManyStart s
944− | FoldManyFirst fs s
937+ | FoldManyFirst a s
945 | FoldManyLoop s fs
938 | FoldManyLoop s fs
946 | FoldManyYield b (FoldMany s fs b a)
939 | FoldManyYield b (FoldMany s fs b a)
947 | FoldManyDone
940 | FoldManyDone
949-- XXX Nested foldMany does not fuse.
942-- XXX Nested foldMany does not fuse.
951−-- | Apply a fold multiple times until the stream ends. If the stream is empty
944+-- | 'Streamly.Internal.Data.Stream.foldMany'.
952−-- the output would be empty.
954−-- @foldMany f = parseMany (fromFold f)@
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.
961{-# INLINE_NORMAL foldMany #-}
945{-# INLINE_NORMAL foldMany #-}
962foldMany :: Monad m => Fold m a b -> Stream m a -> Stream m b
946foldMany :: Monad m => Fold m a b -> Stream m a -> Stream m b
963foldMany (Fold fstep initial extract) (Stream step state) =
947foldMany (Fold fstep initial extract) (Stream step state) =
@@ -975,19 +959,17 @@ foldMany (Fold fstep initial extract) (Stream step state) =
975 FL.Partial ps -> FoldManyLoop s ps
959 FL.Partial ps -> FoldManyLoop s ps
977 {-# INLINE_LATE step' #-}
961 {-# INLINE_LATE step' #-}
978− step' _ (FoldManyStart st) = do
962+ step' gst (FoldManyStart st) = do
983− FL.Done b -> FoldManyYield b (FoldManyStart st)
984− FL.Partial fs -> FoldManyFirst fs st
985− step' gst (FoldManyFirst fs st) = do
986 r <- step (adaptState gst) st
963 r <- step (adaptState gst) st
987 case r of
964 case r of
988− Yield x s -> consume x s fs
965+ Yield x s -> return $ Skip (FoldManyFirst x s)
989− Skip s -> return $ Skip (FoldManyFirst fs s)
966+ Skip s -> return $ Skip (FoldManyStart s)
990 Stop -> return Stop
967 Stop -> return Stop
968+ step' _ (FoldManyFirst x st) = do
971+ FL.Done b -> return $ Skip $ FoldManyYield b (FoldManyFirst x st)
972+ FL.Partial fs -> consume x st fs
991 step' gst (FoldManyLoop st fs) = do
973 step' gst (FoldManyLoop st fs) = do
992 r <- step (adaptState gst) st
974 r <- step (adaptState gst) st
993 case r of
975 case r of
@@ -1003,8 +985,8 @@ foldMany (Fold fstep initial extract) (Stream step state) =
1003chunksOf :: Monad m => Int -> Fold m a b -> Stream m a -> Stream m b
985chunksOf :: Monad m => Int -> Fold m a b -> Stream m a -> Stream m b
1004chunksOf n f = foldMany (FL.take n f)
986chunksOf n f = foldMany (FL.take n f)
1006−-- Keep the argument order consistent with consumeIterateM.
988+-- Keep the argument order consistent with refoldIterateM.
1008-- | Like 'foldMany' but for the 'Refold' type. The supplied action is used as
990-- | Like 'foldMany' but for the 'Refold' type. The supplied action is used as
1009-- the initial value for each refold.
991-- the initial value for each refold.
@@ -1026,19 +1008,17 @@ refoldMany (Refold fstep inject extract) action (Stream step state) =
1026 FL.Partial ps -> FoldManyLoop s ps
1008 FL.Partial ps -> FoldManyLoop s ps
1028 {-# INLINE_LATE step' #-}
1010 {-# INLINE_LATE step' #-}
1029− step' _ (FoldManyStart st) = do
1011+ step' gst (FoldManyStart st) = do
1030− r <- action >>= inject
1034− FL.Done b -> FoldManyYield b (FoldManyStart st)
1035− FL.Partial fs -> FoldManyFirst fs st
1036− step' gst (FoldManyFirst fs st) = do
1037 r <- step (adaptState gst) st
1012 r <- step (adaptState gst) st
1038 case r of
1013 case r of
1039− Yield x s -> consume x s fs
1014+ Yield x s -> return $ Skip (FoldManyFirst x s)
1040− Skip s -> return $ Skip (FoldManyFirst fs s)
1015+ Skip s -> return $ Skip (FoldManyStart s)
1041 Stop -> return Stop
1016 Stop -> return Stop
1017+ step' _ (FoldManyFirst x st) = do
1018+ r <- action >>= inject
1020+ FL.Done b -> return $ Skip $ FoldManyYield b (FoldManyFirst x st)
1021+ FL.Partial fs -> consume x st fs
1042 step' gst (FoldManyLoop st fs) = do
1022 step' gst (FoldManyLoop st fs) = do
1043 r <- step (adaptState gst) st
1023 r <- step (adaptState gst) st
1044 case r of
1024 case r of