@@ -78,9 +78,9 @@ module Streamly.Internal.Data.Stream.StreamD.Type
7878 , concatMap
7979 , concatMapM
8080 , FoldMany (..) -- for inspection testing
81− , FoldMany1 (..)
81+ , FoldManyPost (..)
8282 , foldMany
83− , foldMany1
83+ , foldManyPost
8484 , groupsOf2
8585 , chunksOf
8686 )
@@ -787,15 +787,14 @@ instance Monad m => Monad (Stream m) where
787787------------------------------------------------------------------------------
788788
789789-- s = stream state, fs = fold state
790−{-# ANN type FoldMany Fuse #-}
791−data FoldMany s fs b a
792− = FoldManyStart s
793− | FoldManyLoop s fs
794− | FoldManyYield b (FoldMany s fs b a)
795− | FoldManyDone
796−
797−-- | This is the stream equivalent of "Data.Fold.Internal.many". The fold
798−-- may consume 0 or more elements. It means:
790+{-# ANN type FoldManyPost Fuse #-}
791+data FoldManyPost s fs b a
792+ = FoldManyPostStart s
793+ | FoldManyPostLoop s fs
794+ | FoldManyPostYield b (FoldManyPost s fs b a)
795+ | FoldManyPostDone
796+
797+-- | Like foldMany but with the following differences:
799798--
800799-- * If the stream is empty the default value of the fold would still be
801800-- emitted in the output.
@@ -803,10 +802,10 @@ data FoldMany s fs b a
803802-- receive any input it would still yield the default fold accumulator as the
804803-- last value.
805804--
806−{-# INLINE_NORMAL foldMany #-}
807−foldMany :: Monad m => Fold m a b -> Stream m a -> Stream m b
808−foldMany (Fold fstep initial extract) (Stream step state) =
809− Stream step' (FoldManyStart state)
805+{-# INLINE_NORMAL foldManyPost #-}
806+foldManyPost :: Monad m => Fold m a b -> Stream m a -> Stream m b
807+foldManyPost (Fold fstep initial extract) (Stream step state) =
808+ Stream step' (FoldManyPostStart state)
810809
811810 where
812811
@@ -816,56 +815,50 @@ foldMany (Fold fstep initial extract) (Stream step state) =
816815 return
817816 $ Skip
818817 $ case res of
819− FL.Done b -> FoldManyYield b (FoldManyStart s)
820− FL.Partial ps -> FoldManyLoop s ps
818+ FL.Done b -> FoldManyPostYield b (FoldManyPostStart s)
819+ FL.Partial ps -> FoldManyPostLoop s ps
821820
822821 {-# INLINE_LATE step' #-}
823− step' _ (FoldManyStart st) = do
822+ step' _ (FoldManyPostStart st) = do
824823 r <- initial
825824 return
826825 $ Skip
827826 $ case r of
828− FL.Done b -> FoldManyYield b (FoldManyStart st)
829− FL.Partial fs -> FoldManyLoop st fs
830− step' gst (FoldManyLoop st fs) = do
827+ FL.Done b -> FoldManyPostYield b (FoldManyPostStart st)
828+ FL.Partial fs -> FoldManyPostLoop st fs
829+ step' gst (FoldManyPostLoop st fs) = do
831830 r <- step (adaptState gst) st
832831 case r of
833832 Yield x s -> consume x s fs
834− Skip s -> return $ Skip (FoldManyLoop s fs)
833+ Skip s -> return $ Skip (FoldManyPostLoop s fs)
835834 Stop -> do
836835 b <- extract fs
837− return $ Skip (FoldManyYield b FoldManyDone)
838− step' _ (FoldManyYield b next) = return $ Yield b next
839− step' _ FoldManyDone = return Stop
836+ return $ Skip (FoldManyPostYield b FoldManyPostDone)
837+ step' _ (FoldManyPostYield b next) = return $ Yield b next
838+ step' _ FoldManyPostDone = return Stop
840839
841−{-# ANN type FoldMany1 Fuse #-}
842−data FoldMany1 s fs b a
843− = FoldMany1Start s
844− | FoldMany1First fs s
845− | FoldMany1Loop s fs
846− | FoldMany1Yield b (FoldMany1 s fs b a)
847− | FoldMany1Done
840+{-# ANN type FoldMany Fuse #-}
841+data FoldMany s fs b a
842+ = FoldManyStart s
843+ | FoldManyFirst fs s
844+ | FoldManyLoop s fs
845+ | FoldManyYield b (FoldMany s fs b a)
846+ | FoldManyDone
848847
849−-- | Like 'foldMany' except that the fold consumes 1 or more elements. It
850−-- means:
848+-- | Apply a fold multiple times until the stream ends. If the stream is empty
849+-- the output would be empty.
851850--
852−-- * If the stream is empty the output would be empty.
853−-- * At the end of the stream if the last application of the fold did not
854−-- receive any input it would not result in any output.
855−--
856−-- @foldMany1 f = parseMany (fromFold f)@
851+-- @foldMany f = parseMany (fromFold f)@
857852--
858−-- This could be problematic unless we use an accumulator fold instead of a
859−-- terminating fold. A terminating fold may terminate even without accepting a
860−-- single element. We can use parseMany instead where we can return the input
861−-- if the fold terminates without accepting any input. The alternative is to
862−-- always execute the fold's initial action and discard it in case the stream
863−-- stops without actually feeding input to it.
853+-- A terminating fold may terminate even without accepting a single input. So
854+-- we run the fold's initial action before evaluating the stream. However, this
855+-- means that if later the stream does not yield anything we have to discard
856+-- the fold's initial result which could have generated an effect.
864857--
865−{-# INLINE_NORMAL foldMany1 #-}
866−foldMany1 :: Monad m => Fold m a b -> Stream m a -> Stream m b
867−foldMany1 (Fold fstep initial extract) (Stream step state) =
868− Stream step' (FoldMany1Start state)
858+{-# INLINE_NORMAL foldMany #-}
859+foldMany :: Monad m => Fold m a b -> Stream m a -> Stream m b
860+foldMany (Fold fstep initial extract) (Stream step state) =
861+ Stream step' (FoldManyStart state)
869862
870863 where
871864
@@ -875,37 +868,37 @@ foldMany1 (Fold fstep initial extract) (Stream step state) =
875868 return
876869 $ Skip
877870 $ case res of
878− FL.Done b -> FoldMany1Yield b (FoldMany1Start s)
879− FL.Partial ps -> FoldMany1Loop s ps
871+ FL.Done b -> FoldManyYield b (FoldManyStart s)
872+ FL.Partial ps -> FoldManyLoop s ps
880873
881874 {-# INLINE_LATE step' #-}
882− step' _ (FoldMany1Start st) = do
875+ step' _ (FoldManyStart st) = do
883876 r <- initial
884877 return
885878 $ Skip
886879 $ case r of
887− FL.Done b -> FoldMany1Yield b (FoldMany1Start st)
888− FL.Partial fs -> FoldMany1First fs st
889− step' gst (FoldMany1First fs st) = do
880+ FL.Done b -> FoldManyYield b (FoldManyStart st)
881+ FL.Partial fs -> FoldManyFirst fs st
882+ step' gst (FoldManyFirst fs st) = do
890883 r <- step (adaptState gst) st
891884 case r of
892885 Yield x s -> consume x s fs
893− Skip s -> return $ Skip (FoldMany1First fs s)
886+ Skip s -> return $ Skip (FoldManyFirst fs s)
894887 Stop -> return Stop
895− step' gst (FoldMany1Loop st fs) = do
888+ step' gst (FoldManyLoop st fs) = do
896889 r <- step (adaptState gst) st
897890 case r of
898891 Yield x s -> consume x s fs
899− Skip s -> return $ Skip (FoldMany1Loop s fs)
892+ Skip s -> return $ Skip (FoldManyLoop s fs)
900893 Stop -> do
901894 b <- extract fs
902− return $ Skip (FoldMany1Yield b FoldMany1Done)
903− step' _ (FoldMany1Yield b next) = return $ Yield b next
904− step' _ FoldMany1Done = return Stop
895+ return $ Skip (FoldManyYield b FoldManyDone)
896+ step' _ (FoldManyYield b next) = return $ Yield b next
897+ step' _ FoldManyDone = return Stop
905898
906899{-# INLINE chunksOf #-}
907900chunksOf :: Monad m => Int -> Fold m a b -> Stream m a -> Stream m b
908−chunksOf n f = foldMany1 (FL.takeLE n f)
901+chunksOf n f = foldMany (FL.takeLE n f)
909902
910903data GroupState2 s fs
911904 = GroupStart2 s