@@ -103,6 +103,8 @@ module Streamly.Internal.Data.Stream.StreamD
103103 , foldlx'
104104 , foldlMx'
105105 , foldOnce
106+ , foldMany
107+ , foldMany1
106108
107109 , parselMx'
108110 , parseMany
@@ -157,16 +159,12 @@ module Streamly.Internal.Data.Stream.StreamD
157159 , interpose
158160
159161 -- ** Grouping
160− , groupsOf
161162 , groupsOf2
162163 , groupsBy
163164 , groupsRollingBy
164165
165166 -- ** Splitting
166− , splitBy
167− , splitSuffixBy
168167 , wordsBy
169− , splitSuffixWith
170168
171169 , splitOnSeq
172170 , splitOnSuffixSeq
@@ -1519,11 +1517,6 @@ reverse' m =
15191517-- Grouping/Splitting
15201518------------------------------------------------------------------------------
15211519
1522−{-# INLINE_NORMAL splitSuffixWith #-}
1523−splitSuffixWith :: Monad m
1524− => (a -> Bool) -> Fold m a b -> Stream m a -> Stream m b
1525−splitSuffixWith predicate f = foldMany1 (FL.sliceEndWith predicate f)
1526−
15271520{-# INLINE_NORMAL groupsBy #-}
15281521groupsBy :: Monad m
15291522 => (a -> a -> Bool)
@@ -1540,11 +1533,11 @@ groupsBy cmp f (Stream step state) = Stream (stepOuter f) (Just state, Nothing)
15401533 case res of
15411534 Yield x s -> do
15421535 fs <- initial
1543− sfs <- fstep fs x
1544− case sfs of
1536+ r <- fstep fs x
1537+ case r of
15451538 FL.Partial fs1 -> go SPEC x s fs1
1546− FL.Done fb -> return $ Yield fb (Just s, Just x)
1547− Skip s -> return $ Skip $ (Just s, Nothing)
1539+ FL.Done b -> return $ Yield b (Just s, Just x)
1540+ Skip s -> return $ Skip (Just s, Nothing)
15481541 Stop -> return Stop
15491542
15501543 where
@@ -1555,19 +1548,19 @@ groupsBy cmp f (Stream step state) = Stream (stepOuter f) (Just state, Nothing)
15551548 Yield x s -> do
15561549 if cmp x prev
15571550 then do
1558− sfs <- fstep acc x
1559− case sfs of
1551+ r <- fstep acc x
1552+ case r of
15601553 FL.Partial fs1 -> go SPEC prev s fs1
1561− FL.Done fb -> return $ Yield fb (Just s, Just x)
1554+ FL.Done b -> return $ Yield b (Just s, Just x)
15621555 else done acc >>= \r -> return $ Yield r (Just s, Just x)
15631556 Skip s -> go SPEC prev s acc
15641557 Stop -> done acc >>= \r -> return $ Yield r (Nothing, Nothing)
15651558 stepOuter (Fold fstep initial done) gst (Just st, Just prev) = do
15661559 fs <- initial
1567− sfs <- fstep fs prev
1568− case sfs of
1560+ r <- fstep fs prev
1561+ case r of
15691562 FL.Partial fs1 -> go SPEC st fs1
1570− FL.Done fb -> return $ Yield fb (Just st, Nothing)
1563+ FL.Done b -> return $ Yield b (Just st, Nothing)
15711564
15721565 where
15731566
@@ -1578,10 +1571,10 @@ groupsBy cmp f (Stream step state) = Stream (stepOuter f) (Just state, Nothing)
15781571 Yield x s -> do
15791572 if cmp x prev
15801573 then do
1581− sfs <- fstep acc x
1582− case sfs of
1574+ r <- fstep acc x
1575+ case r of
15831576 FL.Partial fs1 -> go SPEC s fs1
1584− FL.Done fb -> return $ Yield fb (Just s, Just x)
1577+ FL.Done b -> return $ Yield b (Just s, Just x)
15851578 else done acc >>= \r -> return $ Yield r (Just s, Just x)
15861579 Skip s -> go SPEC s acc
15871580 Stop -> done acc >>= \r -> return $ Yield r (Nothing, Nothing)
@@ -1604,10 +1597,10 @@ groupsRollingBy cmp f (Stream step state) =
16041597 case res of
16051598 Yield x s -> do
16061599 fs <- initial
1607− sfs <- fstep fs x
1608− case sfs of
1600+ r <- fstep fs x
1601+ case r of
16091602 FL.Partial fs1 -> go SPEC x s fs1
1610− FL.Done fb -> return $ Yield fb (Just s, Just x)
1603+ FL.Done b -> return $ Yield b (Just s, Just x)
16111604 Skip s -> return $ Skip $ (Just s, Nothing)
16121605 Stop -> return Stop
16131606
@@ -1619,19 +1612,19 @@ groupsRollingBy cmp f (Stream step state) =
16191612 Yield x s -> do
16201613 if cmp prev x
16211614 then do
1622− sfs <- fstep acc x
1623− case sfs of
1615+ r <- fstep acc x
1616+ case r of
16241617 FL.Partial fs1 -> go SPEC x s fs1
1625− FL.Done fb -> return $ Yield fb (Just s, Just x)
1618+ FL.Done b -> return $ Yield b (Just s, Just x)
16261619 else done acc >>= \r -> return $ Yield r (Just s, Just x)
16271620 Skip s -> go SPEC prev s acc
16281621 Stop -> done acc >>= \r -> return $ Yield r (Nothing, Nothing)
16291622 stepOuter (Fold fstep initial done) gst (Just st, Just prev') = do
16301623 fs <- initial
1631− sfs <- fstep fs prev'
1632− case sfs of
1624+ r <- fstep fs prev'
1625+ case r of
16331626 FL.Partial fs1 -> go SPEC prev' st fs1
1634− FL.Done fb -> return $ Yield fb (Just st, Nothing)
1627+ FL.Done b -> return $ Yield b (Just st, Nothing)
16351628
16361629 where
16371630
@@ -1641,24 +1634,15 @@ groupsRollingBy cmp f (Stream step state) =
16411634 Yield x s -> do
16421635 if cmp prevv x
16431636 then do
1644− sfs <- fstep acc x
1645− case sfs of
1637+ r <- fstep acc x
1638+ case r of
16461639 FL.Partial fs1 -> go SPEC x s fs1
1647− FL.Done fb -> return $ Yield fb (Just s, Just x)
1640+ FL.Done b -> return $ Yield b (Just s, Just x)
16481641 else done acc >>= \r -> return $ Yield r (Just s, Just x)
16491642 Skip s -> go SPEC prevv s acc
16501643 Stop -> done acc >>= \r -> return $ Yield r (Nothing, Nothing)
16511644 stepOuter _ _ (Nothing, _) = return Stop
16521645
1653−{-# INLINE_NORMAL splitBy #-}
1654−splitBy :: Monad m => (a -> Bool) -> Fold m a b -> Stream m a -> Stream m b
1655−splitBy predicate f = foldMany (FL.sliceSepBy predicate f)
1656−
1657−{-# INLINE_NORMAL splitSuffixBy #-}
1658−splitSuffixBy :: Monad m
1659− => (a -> Bool) -> Fold m a b -> Stream m a -> Stream m b
1660−splitSuffixBy predicate f = foldMany1 (FL.sliceSepBy predicate f)
1661−
16621646data WordsByState s = WordsByJust s | WordsByNothing
16631647
16641648{-# INLINE_NORMAL wordsBy #-}
@@ -1677,10 +1661,10 @@ wordsBy predicate f (Stream step state) =
16771661 then return $ Skip (WordsByJust s)
16781662 else do
16791663 fs <- initial
1680− sfs <- fstep fs x
1681− case sfs of
1664+ r <- fstep fs x
1665+ case r of
16821666 FL.Partial fs1 -> go SPEC s fs1
1683− FL.Done fb -> return $ Yield fb (WordsByJust s)
1667+ FL.Done b -> return $ Yield b (WordsByJust s)
16841668
16851669 Skip s -> return $ Skip $ WordsByJust s
16861670 Stop -> return Stop
@@ -1694,10 +1678,10 @@ wordsBy predicate f (Stream step state) =
16941678 if predicate x
16951679 then done acc >>= \r -> return $ Yield r (WordsByJust s)
16961680 else do
1697− sfs <- fstep acc x
1698− case sfs of
1681+ r <- fstep acc x
1682+ case r of
16991683 FL.Partial fs1 -> go SPEC s fs1
1700− FL.Done fb -> return $ Yield fb (WordsByJust s)
1684+ FL.Done b -> return $ Yield b (WordsByJust s)
17011685 Skip s -> go SPEC s acc
17021686 Stop -> done acc >>= \r -> return $ Yield r WordsByNothing
17031687
@@ -1808,13 +1792,13 @@ splitOnSeq patArr (Fold fstep initial done) (Stream step state) =
18081792 res <- step (adaptState gst) st
18091793 case res of
18101794 Yield x s -> do
1811− acc <- initial
1812− sfs <- fstep acc x
1813− case sfs of
1814− FL.Partial acc1 -> do
1815− r <- done acc1
1816− skip $ SplitOnSeqYield r (SplitOnSeqEmpty s)
1817− FL.Done fb -> skip $ SplitOnSeqYield fb (SplitOnSeqEmpty s)
1795+ fs <- initial
1796+ r <- fstep fs x
1797+ case r of
1798+ FL.Partial fs1 -> do
1799+ b <- done fs1
1800+ skip $ SplitOnSeqYield b (SplitOnSeqEmpty s)
1801+ FL.Done b -> skip $ SplitOnSeqYield b (SplitOnSeqEmpty s)
18181802
18191803 Skip s -> return $ Skip (SplitOnSeqEmpty s)
18201804 Stop -> return Stop
@@ -1839,12 +1823,12 @@ splitOnSeq patArr (Fold fstep initial done) (Stream step state) =
18391823 fs1 <- initial
18401824 return $ Skip $ SplitOnSeqYield r (SplitOnSeqSingle fs1 s pat)
18411825 else do
1842− sfs <- fstep fs x
1843− case sfs of
1844− FL.Partial fs1 -> skip $ (SplitOnSeqSingle fs1 s pat)
1845− FL.Done fb -> do
1826+ r <- fstep fs x
1827+ case r of
1828+ FL.Partial fs1 -> skip $ SplitOnSeqSingle fs1 s pat
1829+ FL.Done b -> do
18461830 fs1 <- initial
1847− skip $ SplitOnSeqYield fb (SplitOnSeqSingle fs1 s pat)
1831+ skip $ SplitOnSeqYield b (SplitOnSeqSingle fs1 s pat)
18481832 Skip s -> return $ Skip $ SplitOnSeqSingle fs s pat
18491833 Stop -> do
18501834 r <- done fs
@@ -1859,13 +1843,13 @@ splitOnSeq patArr (Fold fstep initial done) (Stream step state) =
18591843 skip $ SplitOnSeqYield r SplitOnSeqDone
18601844 stepOuter _ (SplitOnSeqWordDone n fs wrd) = do
18611845 let old = elemMask .&. (wrd `shiftR` (elemBits * (n - 1)))
1862− sfs <- fstep fs (toEnum $ fromIntegral old)
1863− case sfs of
1846+ r <- fstep fs (toEnum $ fromIntegral old)
1847+ case r of
18641848 FL.Partial fs1 -> skip $ SplitOnSeqWordDone (n - 1) fs1 wrd
1865− FL.Done r -> do
1849+ FL.Done b -> do
18661850 fs1 <- initial
18671851 let next = SplitOnSeqWordDone (n - 1) fs1 wrd
1868− skip $ SplitOnSeqYield r next
1852+ skip $ SplitOnSeqYield b next
18691853
18701854 stepOuter gst (SplitOnSeqWordInit st0) =
18711855 go SPEC 0 0 st0
@@ -1910,18 +1894,18 @@ splitOnSeq patArr (Fold fstep initial done) (Stream step state) =
19101894 let wrd1 = addToWord wrd x
19111895 old = (wordMask .&. wrd)
19121896 `shiftR` (elemBits * (patLen - 1))
1913− sfs <- fstep fs (toEnum $ fromIntegral old)
1914− case sfs of
1897+ r <- fstep fs (toEnum $ fromIntegral old)
1898+ case r of
19151899 FL.Partial fs1 -> do
19161900 if wrd1 .&. wordMask == wordPat
19171901 then do
1918− r <- done fs1
1902+ b <- done fs1
19191903 let next = SplitOnSeqWordInit s
1920− skip $ SplitOnSeqYield r next
1904+ skip $ SplitOnSeqYield b next
19211905 else go SPEC wrd1 s fs1
1922− FL.Done r ->
1906+ FL.Done b ->
19231907 let next = SplitOnSeqWordInit s
1924− in skip $ SplitOnSeqYield r next
1908+ in skip $ SplitOnSeqYield b next
19251909 Skip s -> go SPEC wrd s fs
19261910 Stop -> skip $ SplitOnSeqWordDone patLen fs wrd
19271911
@@ -1963,16 +1947,16 @@ splitOnSeq patArr (Fold fstep initial done) (Stream step state) =
19631947 Yield x s -> do
19641948 old <- liftIO $ peek rh
19651949 let cksum1 = deltaCksum cksum old x
1966− sfs <- fstep fs old
1967− case sfs of
1950+ r <- fstep fs old
1951+ case r of
19681952 FL.Partial fs1 -> do
19691953 rh1 <- liftIO (RB.unsafeInsert rb rh x)
1970− if (cksum1 == patHash)
1954+ if cksum1 == patHash
19711955 then skip $ SplitOnSeqKRCheck fs1 s rb rh1
19721956 else go SPEC fs1 s rh1 cksum1
1973− FL.Done r ->
1957+ FL.Done b ->
19741958 let next = SplitOnSeqKRInit 0 s rb (RB.startOf rb)
1975− in skip $ SplitOnSeqYield r next
1959+ in skip $ SplitOnSeqYield b next
19761960 Skip s -> go SPEC fs s rh cksum
19771961 Stop -> skip $ SplitOnSeqKRDone patLen fs rb rh
19781962
@@ -2015,13 +1999,13 @@ splitOnSeq patArr (Fold fstep initial done) (Stream step state) =
20151999 stepOuter _ (SplitOnSeqKRDone n fs rb rh) = do
20162000 old <- liftIO $ peek rh
20172001 let rh1 = RB.advance rb rh
2018− sfs <- fstep fs old
2019− case sfs of
2002+ r <- fstep fs old
2003+ case r of
20202004 FL.Partial fs1 -> skip $ SplitOnSeqKRDone (n - 1) fs1 rb rh1
2021− FL.Done r -> do
2005+ FL.Done b -> do
20222006 fs1 <- initial
20232007 let next = SplitOnSeqKRDone (n - 1) fs1 rb rh1
2024− skip $ SplitOnSeqYield r next
2008+ skip $ SplitOnSeqYield b next
20252009
20262010{-# ANN type SplitOnSuffixSeqState Fuse #-}
20272011data SplitOnSuffixSeqState rb rh ck w fs s b x =
@@ -2078,22 +2062,22 @@ splitOnSuffixSeq withSep patArr (Fold fstep initial done) (Stream step state) =
20782062 processYieldSingle pat x s fs =
20792063 if pat == x
20802064 then do
2081− sfs <- if withSep then fstep fs x else return $ FL.Partial fs
2082− case sfs of
2065+ r <- if withSep then fstep fs x else return $ FL.Partial fs
2066+ case r of
20832067 FL.Partial fs1 -> do
2084− r <- done fs1
2068+ b <- done fs1
20852069 let next = SplitOnSuffixSeqSingleInit s pat
2086− skip $ SplitOnSuffixSeqYield r next
2087− FL.Done r ->
2070+ skip $ SplitOnSuffixSeqYield b next
2071+ FL.Done b ->
20882072 let next = SplitOnSuffixSeqSingleInit s pat
2089− in skip $ SplitOnSuffixSeqYield r next
2073+ in skip $ SplitOnSuffixSeqYield b next
20902074 else do
2091− sfs <- fstep fs x
2092− case sfs of
2075+ r <- fstep fs x
2076+ case r of
20932077 FL.Partial fs1 -> skip $ SplitOnSuffixSeqSingle fs1 s pat
2094− FL.Done r ->
2078+ FL.Done b ->
20952079 let next = SplitOnSuffixSeqSingleInit s pat
2096− in skip $ SplitOnSuffixSeqYield r next
2080+ in skip $ SplitOnSuffixSeqYield b next
20972081
20982082 -- For Rabin-Karp search
20992083 k = 2891336453 :: Word32
@@ -2135,13 +2119,13 @@ splitOnSuffixSeq withSep patArr (Fold fstep initial done) (Stream step state) =
21352119 case res of
21362120 Yield x s -> do
21372121 acc <- initial
2138− sfs <- fstep acc x
2139− case sfs of
2140− FL.Partial acc1 -> do
2141− r <- done acc1
2142− skip $ SplitOnSuffixSeqYield r (SplitOnSuffixSeqEmpty s)
2143− FL.Done r ->
2144− skip $ SplitOnSuffixSeqYield r (SplitOnSuffixSeqEmpty s)
2122+ r <- fstep acc x
2123+ case r of
2124+ FL.Partial fs -> do
2125+ b <- done fs
2126+ skip $ SplitOnSuffixSeqYield b (SplitOnSuffixSeqEmpty s)
2127+ FL.Done b ->
2128+ skip $ SplitOnSuffixSeqYield b (SplitOnSuffixSeqEmpty s)
21452129 Skip s -> skip (SplitOnSuffixSeqEmpty s)
21462130 Stop -> return Stop
21472131
@@ -2180,13 +2164,13 @@ splitOnSuffixSeq withSep patArr (Fold fstep initial done) (Stream step state) =
21802164 skip $ SplitOnSuffixSeqYield r SplitOnSuffixSeqDone
21812165 stepOuter _ (SplitOnSuffixSeqWordDone n fs wrd) = do
21822166 let old = elemMask .&. (wrd `shiftR` (elemBits * (n - 1)))
2183− sfs <- fstep fs (toEnum $ fromIntegral old)
2184− case sfs of
2167+ r <- fstep fs (toEnum $ fromIntegral old)
2168+ case r of
21852169 FL.Partial fs1 -> skip $ SplitOnSuffixSeqWordDone (n - 1) fs1 wrd
2186− FL.Done r -> do
2170+ FL.Done b -> do
21872171 fs1 <- initial
21882172 let next = SplitOnSuffixSeqWordDone (n - 1) fs1 wrd
2189− skip $ SplitOnSuffixSeqYield r next
2173+ skip $ SplitOnSuffixSeqYield b next
21902174
21912175 stepOuter gst (SplitOnSuffixSeqWordInit st0) = do
21922176 res <- step (adaptState gst) st0
@@ -2194,12 +2178,12 @@ splitOnSuffixSeq withSep patArr (Fold fstep initial done) (Stream step state) =
21942178 Yield x s -> do
21952179 fs <- initial
21962180 let wrd = addToWord 0 x
2197− sfs <- if withSep then fstep fs x else return $ FL.Partial fs
2198− case sfs of
2181+ r <- if withSep then fstep fs x else return $ FL.Partial fs
2182+ case r of
21992183 FL.Partial fs1 -> go SPEC 1 wrd s fs1
2200− FL.Done r -> do
2184+ FL.Done b -> do
22012185 let next = SplitOnSuffixSeqWordInit s
2202− in skip $ SplitOnSuffixSeqYield r next
2186+ in skip $ SplitOnSuffixSeqYield b next
22032187 Skip s -> skip (SplitOnSuffixSeqWordInit s)
22042188 Stop -> return Stop
22052189
@@ -2211,20 +2195,20 @@ splitOnSuffixSeq withSep patArr (Fold fstep initial done) (Stream step state) =
22112195 case res of
22122196 Yield x s -> do
22132197 let wrd1 = addToWord wrd x
2214− sfs <- if withSep then fstep fs x else return $ FL.Partial fs
2215− case sfs of
2198+ r <- if withSep then fstep fs x else return $ FL.Partial fs
2199+ case r of
22162200 FL.Partial fs1 ->
22172201 if idx /= maxIndex
22182202 then go SPEC (idx + 1) wrd1 s fs1
22192203 else if wrd1 .&. wordMask /= wordPat
22202204 then skip $ SplitOnSuffixSeqWordLoop wrd1 s fs1
22212205 else do
2222− r <- done fs
2206+ b <- done fs
22232207 let next = SplitOnSuffixSeqWordInit s
2224− skip $ SplitOnSuffixSeqYield r next
2225− FL.Done r ->
2208+ skip $ SplitOnSuffixSeqYield b next
2209+ FL.Done b ->
22262210 let next = SplitOnSuffixSeqWordInit s
2227− in skip $ SplitOnSuffixSeqYield r next
2211+ in skip $ SplitOnSuffixSeqYield b next
22282212 Skip s -> go SPEC idx wrd s fs
22292213 Stop -> skip $ SplitOnSuffixSeqWordDone idx fs wrd
22302214
@@ -2241,21 +2225,21 @@ splitOnSuffixSeq withSep patArr (Fold fstep initial done) (Stream step state) =
22412225 let wrd1 = addToWord wrd x
22422226 old = (wordMask .&. wrd)
22432227 `shiftR` (elemBits * (patLen - 1))
2244− sfs <-
2228+ r <-
22452229 if withSep
22462230 then fstep fs x
22472231 else fstep fs (toEnum $ fromIntegral old)
2248− case sfs of
2232+ case r of
22492233 FL.Partial fs1 ->
22502234 if wrd1 .&. wordMask == wordPat
22512235 then do
2252− r <- done fs1
2236+ b <- done fs1
22532237 let next = SplitOnSuffixSeqWordInit s
2254− skip $ SplitOnSuffixSeqYield r next
2238+ skip $ SplitOnSuffixSeqYield b next
22552239 else go SPEC wrd1 s fs1
2256− FL.Done r ->
2240+ FL.Done b ->
22572241 let next = SplitOnSuffixSeqWordInit s
2258− in skip $ SplitOnSuffixSeqYield r next
2242+ in skip $ SplitOnSuffixSeqYield b next
22592243 Skip s -> go SPEC wrd s fs
22602244 Stop ->
22612245 if wrd .&. wordMask == wordPat
@@ -2276,12 +2260,13 @@ splitOnSuffixSeq withSep patArr (Fold fstep initial done) (Stream step state) =
22762260 Yield x s -> do
22772261 rh1 <- liftIO $ RB.unsafeInsert rb rh0 x
22782262 fs <- initial
2279− sfs <- if withSep then fstep fs x else return $ FL.Partial fs
2280− case sfs of
2281− FL.Partial fs1 -> skip $ SplitOnSuffixSeqKRInit1 fs1 s rb rh1
2282− FL.Done r ->
2263+ r <- if withSep then fstep fs x else return $ FL.Partial fs
2264+ case r of
2265+ FL.Partial fs1 ->
2266+ skip $ SplitOnSuffixSeqKRInit1 fs1 s rb rh1
2267+ FL.Done b ->
22832268 let next = SplitOnSuffixSeqKRInit 0 s rb (RB.startOf rb)
2284− in skip $ SplitOnSuffixSeqYield r next
2269+ in skip $ SplitOnSuffixSeqYield b next
22852270 Skip s -> skip $ SplitOnSuffixSeqKRInit idx0 s rb rh0
22862271 Stop -> return Stop
22872272
@@ -2295,8 +2280,8 @@ splitOnSuffixSeq withSep patArr (Fold fstep initial done) (Stream step state) =
22952280 case res of
22962281 Yield x s -> do
22972282 rh1 <- liftIO (RB.unsafeInsert rb rh x)
2298− sfs <- if withSep then fstep fs x else return $ FL.Partial fs
2299− case sfs of
2283+ r <- if withSep then fstep fs x else return $ FL.Partial fs
2284+ case r of
23002285 FL.Partial fs1 ->
23012286 if idx /= maxIndex
23022287 then go SPEC (idx + 1) rh1 s fs1
@@ -2305,10 +2290,12 @@ splitOnSuffixSeq withSep patArr (Fold fstep initial done) (Stream step state) =
23052290 !ringHash = fold addCksum 0 rb
23062291 in if ringHash == patHash
23072292 then SplitOnSuffixSeqKRCheck fs1 s rb rh1
2308− else SplitOnSuffixSeqKRLoop fs1 s rb rh1 ringHash
2309− FL.Done r ->
2310− let next = SplitOnSuffixSeqKRInit 0 st rb (RB.startOf rb)
2311− in skip $ SplitOnSuffixSeqYield r next
2293+ else SplitOnSuffixSeqKRLoop
2294+ fs1 s rb rh1 ringHash
2295+ FL.Done b ->
2296+ let next = SplitOnSuffixSeqKRInit
2297+ 0 st rb (RB.startOf rb)
2298+ in skip $ SplitOnSuffixSeqYield b next
23122299 Skip s -> go SPEC idx rh s fs
23132300 Stop -> do
23142301 -- do not issue a blank segment when we end at pattern
@@ -2332,15 +2319,16 @@ splitOnSuffixSeq withSep patArr (Fold fstep initial done) (Stream step state) =
23322319 old <- liftIO $ peek rh
23332320 rh1 <- liftIO (RB.unsafeInsert rb rh x)
23342321 let cksum1 = deltaCksum cksum old x
2335− sfs <- if withSep then fstep fs x else fstep fs old
2336− case sfs of
2322+ r <- if withSep then fstep fs x else fstep fs old
2323+ case r of
23372324 FL.Partial fs1 ->
23382325 if (cksum1 /= patHash)
23392326 then go SPEC fs1 s rh1 cksum1
23402327 else skip $ SplitOnSuffixSeqKRCheck fs1 s rb rh1
2341− FL.Done r ->
2342− let next = SplitOnSuffixSeqKRInit 0 st rb (RB.startOf rb)
2343− in skip $ SplitOnSuffixSeqYield r next
2328+ FL.Done b ->
2329+ let next = SplitOnSuffixSeqKRInit
2330+ 0 st rb (RB.startOf rb)
2331+ in skip $ SplitOnSuffixSeqYield b next
23442332 Skip s -> go SPEC fs s rh cksum
23452333 Stop ->
23462334 if RB.unsafeEqArray rb rh patArr
@@ -2365,13 +2353,13 @@ splitOnSuffixSeq withSep patArr (Fold fstep initial done) (Stream step state) =
23652353 stepOuter _ (SplitOnSuffixSeqKRDone n fs rb rh) = do
23662354 old <- liftIO $ peek rh
23672355 let rh1 = RB.advance rb rh
2368− sfs <- fstep fs old
2369− case sfs of
2356+ r <- fstep fs old
2357+ case r of
23702358 FL.Partial fs1 -> skip $ SplitOnSuffixSeqKRDone (n - 1) fs1 rb rh1
2371− FL.Done r -> do
2359+ FL.Done b -> do
23722360 fs1 <- initial
23732361 let next = SplitOnSuffixSeqKRDone (n - 1) fs1 rb rh1
2374− skip $ SplitOnSuffixSeqYield r next
2362+ skip $ SplitOnSuffixSeqYield b next
23752363
23762364{-# ANN type SplitState Fuse #-}
23772365data SplitState s arr
@@ -3765,12 +3753,10 @@ scanlMx' fstep begin done s =
37653753 (begin >>= \x -> x `seq` done x) `consM` postscanlMx' fstep begin done s
37663754
37673755
3768−data PostScanState fsM s a = PostScan s !fsM
3756+data PostScanState s f = PostScan s !f
37693757
3770−-- XXX PostScanWith is can be eliminated if we abstract Yield
37713758{-# INLINE_NORMAL postscanOnce #-}
3772−postscanOnce :: Monad m
3773− => FL.Fold m a b -> Stream m a -> Stream m b
3759+postscanOnce :: Monad m => FL.Fold m a b -> Stream m a -> Stream m b
37743760postscanOnce (FL.Fold fstep begin done) (Stream step state) =
37753761 Stream step' (PostScan state begin)
37763762
@@ -3780,15 +3766,14 @@ postscanOnce (FL.Fold fstep begin done) (Stream step state) =
37803766 step' gst (PostScan st acc) = do
37813767 r <- step (adaptState gst) st
37823768 case r of
3783− -- XXX Move Yield to a common function
37843769 Yield x s -> do
37853770 old <- acc
3786− y <- fstep old x
3787− case y of
3788− FL.Partial sres -> do
3789− !v <- done sres
3790− return $ Yield v $ PostScan s (return sres)
3791− FL.Done _ -> return $ Stop
3771+ res <- fstep old x
3772+ case res of
3773+ FL.Partial fs -> do
3774+ !v <- done fs
3775+ return $ Yield v $ PostScan s (return fs)
3776+ FL.Done _ -> return Stop
37923777 Skip s -> return $ Skip $ PostScan s acc
37933778 Stop -> return Stop
37943779
@@ -4027,17 +4012,17 @@ tap (Fold fstep initial extract) (Stream step state) = Stream step' TapInit
40274012 step' gst (Tapping acc st) = do
40284013 r <- step gst st
40294014 case r of
4030− -- XXX Abstract Yield?
40314015 Yield x s -> do
4032− acc1 <- fstep acc x
4016+ res <- fstep acc x
40334017 return
4034− $ case acc1 of
4035− FL.Partial sres -> Yield x (Tapping sres s)
4036− FL.Done _ -> Yield x (TapDone s)
4018+ $ Yield x
4019+ $ case res of
4020+ FL.Partial fs -> Tapping fs s
4021+ FL.Done _ -> TapDone s
40374022 Skip s -> return $ Skip (Tapping acc s)
40384023 Stop -> do
40394024 void $ extract acc
4040− return $ Stop
4025+ return Stop
40414026 step' gst (TapDone st) = do
40424027 r <- step gst st
40434028 return
@@ -4067,21 +4052,22 @@ tapOffsetEvery offset n (Fold fstep initial extract) (Stream step state) =
40674052 step' gst (TapOffTapping acc st count) = do
40684053 r <- step gst st
40694054 case r of
4070− Yield x s ->
4071− if count <= 0
4072− -- XXX Abstract the then branch?
4073− then do
4074− acc1 <- fstep acc x
4075− return
4076− $ case acc1 of
4077− FL.Partial sres ->
4078− Yield x $ TapOffTapping sres s (n - 1)
4079− FL.Done _ -> Yield x (TapOffDone s)
4080− else return $ Yield x $ TapOffTapping acc s (count - 1)
4055+ Yield x s -> do
4056+ next <-
4057+ if count <= 0
4058+ then do
4059+ res <- fstep acc x
4060+ return
4061+ $ case res of
4062+ FL.Partial sres ->
4063+ TapOffTapping sres s (n - 1)
4064+ FL.Done _ -> TapOffDone s
4065+ else return $ TapOffTapping acc s (count - 1)
4066+ return $ Yield x next
40814067 Skip s -> return $ Skip (TapOffTapping acc s count)
40824068 Stop -> do
40834069 void $ extract acc
4084− return $ Stop
4070+ return Stop
40854071 step' gst (TapOffDone st) = do
40864072 r <- step gst st
40874073 return