@@ -78,9 +78,9 @@ module Streamly.Internal.Data.Stream.StreamD.Type
78 , concatMap
78 , concatMap
79 , concatMapM
79 , concatMapM
80 , FoldMany (..) -- for inspection testing
80 , FoldMany (..) -- for inspection testing
81− , FoldMany1 (..)
81+ , FoldManyPost (..)
82 , foldMany
82 , foldMany
83− , foldMany1
83+ , foldManyPost
84 , groupsOf2
84 , groupsOf2
85 , chunksOf
85 , chunksOf
@@ -787,15 +787,14 @@ instance Monad m => Monad (Stream m) where
787------------------------------------------------------------------------------
787------------------------------------------------------------------------------
789-- s = stream state, fs = fold state
789-- s = stream state, fs = fold state
790−{-# ANN type FoldMany Fuse #-}
790+{-# ANN type FoldManyPost Fuse #-}
791−data FoldMany s fs b a
791+data FoldManyPost s fs b a
792− = FoldManyStart s
792+ = FoldManyPostStart s
793− | FoldManyLoop s fs
793+ | FoldManyPostLoop s fs
794− | FoldManyYield b (FoldMany s fs b a)
794+ | FoldManyPostYield b (FoldManyPost s fs b a)
795− | FoldManyDone
795+ | FoldManyPostDone
797−-- | This is the stream equivalent of "Data.Fold.Internal.many". The fold
797+-- | Like foldMany but with the following differences:
798−-- may consume 0 or more elements. It means:
800-- * If the stream is empty the default value of the fold would still be
799-- * If the stream is empty the default value of the fold would still be
801-- emitted in the output.
800-- emitted in the output.
@@ -803,10 +802,10 @@ data FoldMany s fs b a
803-- receive any input it would still yield the default fold accumulator as the
802-- receive any input it would still yield the default fold accumulator as the
804-- last value.
803-- last value.
806−{-# INLINE_NORMAL foldMany #-}
805+{-# INLINE_NORMAL foldManyPost #-}
807−foldMany :: Monad m => Fold m a b -> Stream m a -> Stream m b
806+foldManyPost :: Monad m => Fold m a b -> Stream m a -> Stream m b
808−foldMany (Fold fstep initial extract) (Stream step state) =
807+foldManyPost (Fold fstep initial extract) (Stream step state) =
809− Stream step' (FoldManyStart state)
808+ Stream step' (FoldManyPostStart state)
@@ -816,56 +815,50 @@ foldMany (Fold fstep initial extract) (Stream step state) =
818 $ case res of
817 $ case res of
819− FL.Done b -> FoldManyYield b (FoldManyStart s)
818+ FL.Done b -> FoldManyPostYield b (FoldManyPostStart s)
820− FL.Partial ps -> FoldManyLoop s ps
819+ FL.Partial ps -> FoldManyPostLoop s ps
822 {-# INLINE_LATE step' #-}
821 {-# INLINE_LATE step' #-}
823− step' _ (FoldManyStart st) = do
822+ step' _ (FoldManyPostStart st) = do
824 r <- initial
823 r <- initial
827 $ case r of
826 $ case r of
828− FL.Done b -> FoldManyYield b (FoldManyStart st)
827+ FL.Done b -> FoldManyPostYield b (FoldManyPostStart st)
829− FL.Partial fs -> FoldManyLoop st fs
828+ FL.Partial fs -> FoldManyPostLoop st fs
830− step' gst (FoldManyLoop st fs) = do
829+ step' gst (FoldManyPostLoop st fs) = do
831 r <- step (adaptState gst) st
830 r <- step (adaptState gst) st
832 case r of
831 case r of
833 Yield x s -> consume x s fs
832 Yield x s -> consume x s fs
834− Skip s -> return $ Skip (FoldManyLoop s fs)
833+ Skip s -> return $ Skip (FoldManyPostLoop s fs)
835 Stop -> do
834 Stop -> do
836 b <- extract fs
835 b <- extract fs
837− return $ Skip (FoldManyYield b FoldManyDone)
836+ return $ Skip (FoldManyPostYield b FoldManyPostDone)
838− step' _ (FoldManyYield b next) = return $ Yield b next
837+ step' _ (FoldManyPostYield b next) = return $ Yield b next
839− step' _ FoldManyDone = return Stop
838+ step' _ FoldManyPostDone = return Stop
841−{-# ANN type FoldMany1 Fuse #-}
840+{-# ANN type FoldMany Fuse #-}
842−data FoldMany1 s fs b a
841+data FoldMany s fs b a
843− = FoldMany1Start s
842+ = FoldManyStart s
844− | FoldMany1First fs s
843+ | FoldManyFirst fs s
845− | FoldMany1Loop s fs
844+ | FoldManyLoop s fs
846− | FoldMany1Yield b (FoldMany1 s fs b a)
845+ | FoldManyYield b (FoldMany s fs b a)
847− | FoldMany1Done
846+ | FoldManyDone
849−-- | Like 'foldMany' except that the fold consumes 1 or more elements. It
848+-- | Apply a fold multiple times until the stream ends. If the stream is empty
850−-- means:
849+-- the output would be empty.
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.
856−-- @foldMany1 f = parseMany (fromFold f)@
851+-- @foldMany f = parseMany (fromFold f)@
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
853+-- A terminating fold may terminate even without accepting a single input. So
860−-- single element. We can use parseMany instead where we can return the input
854+-- we run the fold's initial action before evaluating the stream. However, this
861−-- if the fold terminates without accepting any input. The alternative is to
855+-- means that if later the stream does not yield anything we have to discard
862−-- always execute the fold's initial action and discard it in case the stream
856+-- the fold's initial result which could have generated an effect.
863−-- stops without actually feeding input to it.
865−{-# INLINE_NORMAL foldMany1 #-}
858+{-# INLINE_NORMAL foldMany #-}
866−foldMany1 :: Monad m => Fold m a b -> Stream m a -> Stream m b
859+foldMany :: Monad m => Fold m a b -> Stream m a -> Stream m b
867−foldMany1 (Fold fstep initial extract) (Stream step state) =
860+foldMany (Fold fstep initial extract) (Stream step state) =
868− Stream step' (FoldMany1Start state)
861+ Stream step' (FoldManyStart state)
@@ -875,37 +868,37 @@ foldMany1 (Fold fstep initial extract) (Stream step state) =
877 $ case res of
870 $ case res of
878− FL.Done b -> FoldMany1Yield b (FoldMany1Start s)
871+ FL.Done b -> FoldManyYield b (FoldManyStart s)
879− FL.Partial ps -> FoldMany1Loop s ps
872+ FL.Partial ps -> FoldManyLoop s ps
881 {-# INLINE_LATE step' #-}
874 {-# INLINE_LATE step' #-}
882− step' _ (FoldMany1Start st) = do
875+ step' _ (FoldManyStart st) = do
883 r <- initial
876 r <- initial
886 $ case r of
879 $ case r of
887− FL.Done b -> FoldMany1Yield b (FoldMany1Start st)
880+ FL.Done b -> FoldManyYield b (FoldManyStart st)
888− FL.Partial fs -> FoldMany1First fs st
881+ FL.Partial fs -> FoldManyFirst fs st
889− step' gst (FoldMany1First fs st) = do
882+ step' gst (FoldManyFirst fs st) = do
890 r <- step (adaptState gst) st
883 r <- step (adaptState gst) st
891 case r of
884 case r of
892 Yield x s -> consume x s fs
885 Yield x s -> consume x s fs
893− Skip s -> return $ Skip (FoldMany1First fs s)
886+ Skip s -> return $ Skip (FoldManyFirst fs s)
894 Stop -> return Stop
887 Stop -> return Stop
895− step' gst (FoldMany1Loop st fs) = do
888+ step' gst (FoldManyLoop st fs) = do
896 r <- step (adaptState gst) st
889 r <- step (adaptState gst) st
897 case r of
890 case r of
898 Yield x s -> consume x s fs
891 Yield x s -> consume x s fs
899− Skip s -> return $ Skip (FoldMany1Loop s fs)
892+ Skip s -> return $ Skip (FoldManyLoop s fs)
900 Stop -> do
893 Stop -> do
901 b <- extract fs
894 b <- extract fs
902− return $ Skip (FoldMany1Yield b FoldMany1Done)
895+ return $ Skip (FoldManyYield b FoldManyDone)
903− step' _ (FoldMany1Yield b next) = return $ Yield b next
896+ step' _ (FoldManyYield b next) = return $ Yield b next
904− step' _ FoldMany1Done = return Stop
897+ step' _ FoldManyDone = return Stop
906{-# INLINE chunksOf #-}
899{-# INLINE chunksOf #-}
907chunksOf :: Monad m => Int -> Fold m a b -> Stream m a -> Stream m b
900chunksOf :: 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)
910data GroupState2 s fs
903data GroupState2 s fs
911 = GroupStart2 s
904 = GroupStart2 s