on master help sign in
Init the fold without first input in foldMany

Reverts commit f593770eb11a856797cdd9a6db4dad4ed44e01bd . To keep it consistent with the "many" fold and parser. Not sure if it is a good idea to init the fold on first input. There is no compelling reason for that.

commit38dda06156046e8d02ce03c22788108c1432d2d5 authorHarendra Kumar <harendra@composewell.com> dateThu 2 Mar 2023, 14:51 (4 years ago) parente95145c
1 file changed+21−17
core/src/Streamly/Internal/Data/Stream/StreamD/Type.hs+21−17
core/src/Streamly/Internal/Data/Stream/StreamD/Type.hs+21−17
@@ -1785,7 +1785,7 @@ foldManyPost (Fold fstep initial extract) (Stream step state) =
17851785{-# ANN type FoldMany Fuse #-}
17861786data FoldMany s fs b a
17871787 = FoldManyStart s
1788− | FoldManyFirst a s
1788+ | FoldManyFirst fs s
17891789 | FoldManyLoop s fs
17901790 | FoldManyYield b (FoldMany s fs b a)
17911791 | FoldManyDone
@@ -1836,17 +1836,19 @@ foldMany (Fold fstep initial extract) (Stream step state) =
18361836 FL.Partial ps -> FoldManyLoop s ps
18371837
18381838 {-# INLINE_LATE step' #-}
1839− step' gst (FoldManyStart st) = do
1839+ step' _ (FoldManyStart st) = do
1840+ r <- initial
1841+ return
1842+ $ Skip
1843+ $ case r of
1844+ FL.Done b -> FoldManyYield b (FoldManyStart st)
1845+ FL.Partial fs -> FoldManyFirst fs st
1846+ step' gst (FoldManyFirst fs st) = do
18401847 r <- step (adaptState gst) st
18411848 case r of
1842− Yield x s -> return $ Skip (FoldManyFirst x s)
1843− Skip s -> return $ Skip (FoldManyStart s)
1849+ Yield x s -> consume x s fs
1850+ Skip s -> return $ Skip (FoldManyFirst fs s)
18441851 Stop -> return Stop
1845− step' _ (FoldManyFirst x st) = do
1846− r <- initial
1847− case r of
1848− FL.Done b -> return $ Skip $ FoldManyYield b (FoldManyFirst x st)
1849− FL.Partial fs -> consume x st fs
18501852 step' gst (FoldManyLoop st fs) = do
18511853 r <- step (adaptState gst) st
18521854 case r of
@@ -1885,17 +1887,19 @@ refoldMany (Refold fstep inject extract) action (Stream step state) =
18851887 FL.Partial ps -> FoldManyLoop s ps
18861888
18871889 {-# INLINE_LATE step' #-}
1888− step' gst (FoldManyStart st) = do
1890+ step' _ (FoldManyStart st) = do
1891+ r <- action >>= inject
1892+ return
1893+ $ Skip
1894+ $ case r of
1895+ FL.Done b -> FoldManyYield b (FoldManyStart st)
1896+ FL.Partial fs -> FoldManyFirst fs st
1897+ step' gst (FoldManyFirst fs st) = do
18891898 r <- step (adaptState gst) st
18901899 case r of
1891− Yield x s -> return $ Skip (FoldManyFirst x s)
1892− Skip s -> return $ Skip (FoldManyStart s)
1900+ Yield x s -> consume x s fs
1901+ Skip s -> return $ Skip (FoldManyFirst fs s)
18931902 Stop -> return Stop
1894− step' _ (FoldManyFirst x st) = do
1895− r <- action >>= inject
1896− case r of
1897− FL.Done b -> return $ Skip $ FoldManyYield b (FoldManyFirst x st)
1898− FL.Partial fs -> consume x st fs
18991903 step' gst (FoldManyLoop st fs) = do
19001904 r <- step (adaptState gst) st
19011905 case r of