@@ -103,6 +103,8 @@ module Streamly.Internal.Data.Stream.StreamD
103 , foldlx'
103 , foldlx'
104 , foldlMx'
104 , foldlMx'
105 , foldOnce
105 , foldOnce
107 , parselMx'
109 , parselMx'
108 , parseMany
110 , parseMany
@@ -157,16 +159,12 @@ module Streamly.Internal.Data.Stream.StreamD
157 , interpose
159 , interpose
159 -- ** Grouping
161 -- ** Grouping
161 , groupsOf2
162 , groupsOf2
162 , groupsBy
163 , groupsBy
163 , groupsRollingBy
164 , groupsRollingBy
165 -- ** Splitting
166 -- ** Splitting
168 , wordsBy
167 , wordsBy
171 , splitOnSeq
169 , splitOnSeq
172 , splitOnSuffixSeq
170 , splitOnSuffixSeq
@@ -1519,11 +1517,6 @@ reverse' m =
1519-- Grouping/Splitting
1517-- Grouping/Splitting
1520------------------------------------------------------------------------------
1518------------------------------------------------------------------------------
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)
1527{-# INLINE_NORMAL groupsBy #-}
1520{-# INLINE_NORMAL groupsBy #-}
1528groupsBy :: Monad m
1521groupsBy :: Monad m
1529 => (a -> a -> Bool)
1522 => (a -> a -> Bool)
@@ -1540,11 +1533,11 @@ groupsBy cmp f (Stream step state) = Stream (stepOuter f) (Just state, Nothing)
1540 case res of
1533 case res of
1541 Yield x s -> do
1534 Yield x s -> do
1542 fs <- initial
1535 fs <- initial
1543− sfs <- fstep fs x
1536+ r <- fstep fs x
1544− case sfs of
1537+ case r of
1545 FL.Partial fs1 -> go SPEC x s fs1
1538 FL.Partial fs1 -> go SPEC x s fs1
1546− FL.Done fb -> return $ Yield fb (Just s, Just x)
1539+ FL.Done b -> return $ Yield b (Just s, Just x)
1547− Skip s -> return $ Skip $ (Just s, Nothing)
1540+ Skip s -> return $ Skip (Just s, Nothing)
1548 Stop -> return Stop
1541 Stop -> return Stop
@@ -1555,19 +1548,19 @@ groupsBy cmp f (Stream step state) = Stream (stepOuter f) (Just state, Nothing)
1555 Yield x s -> do
1548 Yield x s -> do
1556 if cmp x prev
1549 if cmp x prev
1558− sfs <- fstep acc x
1551+ r <- fstep acc x
1559− case sfs of
1552+ case r of
1560 FL.Partial fs1 -> go SPEC prev s fs1
1553 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)
1562 else done acc >>= \r -> return $ Yield r (Just s, Just x)
1555 else done acc >>= \r -> return $ Yield r (Just s, Just x)
1563 Skip s -> go SPEC prev s acc
1556 Skip s -> go SPEC prev s acc
1564 Stop -> done acc >>= \r -> return $ Yield r (Nothing, Nothing)
1557 Stop -> done acc >>= \r -> return $ Yield r (Nothing, Nothing)
1565 stepOuter (Fold fstep initial done) gst (Just st, Just prev) = do
1558 stepOuter (Fold fstep initial done) gst (Just st, Just prev) = do
1566 fs <- initial
1559 fs <- initial
1567− sfs <- fstep fs prev
1560+ r <- fstep fs prev
1568− case sfs of
1561+ case r of
1569 FL.Partial fs1 -> go SPEC st fs1
1562 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)
@@ -1578,10 +1571,10 @@ groupsBy cmp f (Stream step state) = Stream (stepOuter f) (Just state, Nothing)
1578 Yield x s -> do
1571 Yield x s -> do
1579 if cmp x prev
1572 if cmp x prev
1581− sfs <- fstep acc x
1574+ r <- fstep acc x
1582− case sfs of
1575+ case r of
1583 FL.Partial fs1 -> go SPEC s fs1
1576 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)
1585 else done acc >>= \r -> return $ Yield r (Just s, Just x)
1578 else done acc >>= \r -> return $ Yield r (Just s, Just x)
1586 Skip s -> go SPEC s acc
1579 Skip s -> go SPEC s acc
1587 Stop -> done acc >>= \r -> return $ Yield r (Nothing, Nothing)
1580 Stop -> done acc >>= \r -> return $ Yield r (Nothing, Nothing)
@@ -1604,10 +1597,10 @@ groupsRollingBy cmp f (Stream step state) =
1604 case res of
1597 case res of
1605 Yield x s -> do
1598 Yield x s -> do
1606 fs <- initial
1599 fs <- initial
1607− sfs <- fstep fs x
1600+ r <- fstep fs x
1608− case sfs of
1601+ case r of
1609 FL.Partial fs1 -> go SPEC x s fs1
1602 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)
1611 Skip s -> return $ Skip $ (Just s, Nothing)
1604 Skip s -> return $ Skip $ (Just s, Nothing)
1612 Stop -> return Stop
1605 Stop -> return Stop
@@ -1619,19 +1612,19 @@ groupsRollingBy cmp f (Stream step state) =
1619 Yield x s -> do
1612 Yield x s -> do
1620 if cmp prev x
1613 if cmp prev x
1622− sfs <- fstep acc x
1615+ r <- fstep acc x
1623− case sfs of
1616+ case r of
1624 FL.Partial fs1 -> go SPEC x s fs1
1617 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)
1626 else done acc >>= \r -> return $ Yield r (Just s, Just x)
1619 else done acc >>= \r -> return $ Yield r (Just s, Just x)
1627 Skip s -> go SPEC prev s acc
1620 Skip s -> go SPEC prev s acc
1628 Stop -> done acc >>= \r -> return $ Yield r (Nothing, Nothing)
1621 Stop -> done acc >>= \r -> return $ Yield r (Nothing, Nothing)
1629 stepOuter (Fold fstep initial done) gst (Just st, Just prev') = do
1622 stepOuter (Fold fstep initial done) gst (Just st, Just prev') = do
1630 fs <- initial
1623 fs <- initial
1631− sfs <- fstep fs prev'
1624+ r <- fstep fs prev'
1632− case sfs of
1625+ case r of
1633 FL.Partial fs1 -> go SPEC prev' st fs1
1626 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)
@@ -1641,24 +1634,15 @@ groupsRollingBy cmp f (Stream step state) =
1641 Yield x s -> do
1634 Yield x s -> do
1642 if cmp prevv x
1635 if cmp prevv x
1644− sfs <- fstep acc x
1637+ r <- fstep acc x
1645− case sfs of
1638+ case r of
1646 FL.Partial fs1 -> go SPEC x s fs1
1639 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)
1648 else done acc >>= \r -> return $ Yield r (Just s, Just x)
1641 else done acc >>= \r -> return $ Yield r (Just s, Just x)
1649 Skip s -> go SPEC prevv s acc
1642 Skip s -> go SPEC prevv s acc
1650 Stop -> done acc >>= \r -> return $ Yield r (Nothing, Nothing)
1643 Stop -> done acc >>= \r -> return $ Yield r (Nothing, Nothing)
1651 stepOuter _ _ (Nothing, _) = return Stop
1644 stepOuter _ _ (Nothing, _) = return Stop
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)
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)
1662data WordsByState s = WordsByJust s | WordsByNothing
1646data WordsByState s = WordsByJust s | WordsByNothing
1664{-# INLINE_NORMAL wordsBy #-}
1648{-# INLINE_NORMAL wordsBy #-}
@@ -1677,10 +1661,10 @@ wordsBy predicate f (Stream step state) =
1677 then return $ Skip (WordsByJust s)
1661 then return $ Skip (WordsByJust s)
1679 fs <- initial
1663 fs <- initial
1680− sfs <- fstep fs x
1664+ r <- fstep fs x
1681− case sfs of
1665+ case r of
1682 FL.Partial fs1 -> go SPEC s fs1
1666 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)
1685 Skip s -> return $ Skip $ WordsByJust s
1669 Skip s -> return $ Skip $ WordsByJust s
1686 Stop -> return Stop
1670 Stop -> return Stop
@@ -1694,10 +1678,10 @@ wordsBy predicate f (Stream step state) =
1694 if predicate x
1678 if predicate x
1695 then done acc >>= \r -> return $ Yield r (WordsByJust s)
1679 then done acc >>= \r -> return $ Yield r (WordsByJust s)
1697− sfs <- fstep acc x
1681+ r <- fstep acc x
1698− case sfs of
1682+ case r of
1699 FL.Partial fs1 -> go SPEC s fs1
1683 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)
1701 Skip s -> go SPEC s acc
1685 Skip s -> go SPEC s acc
1702 Stop -> done acc >>= \r -> return $ Yield r WordsByNothing
1686 Stop -> done acc >>= \r -> return $ Yield r WordsByNothing
@@ -1808,13 +1792,13 @@ splitOnSeq patArr (Fold fstep initial done) (Stream step state) =
1808 res <- step (adaptState gst) st
1792 res <- step (adaptState gst) st
1809 case res of
1793 case res of
1810 Yield x s -> do
1794 Yield x s -> do
1811− acc <- initial
1795+ fs <- initial
1812− sfs <- fstep acc x
1796+ r <- fstep fs x
1813− case sfs of
1797+ case r of
1814− FL.Partial acc1 -> do
1798+ FL.Partial fs1 -> do
1815− r <- done acc1
1799+ b <- done fs1
1816− skip $ SplitOnSeqYield r (SplitOnSeqEmpty s)
1800+ skip $ SplitOnSeqYield b (SplitOnSeqEmpty s)
1817− FL.Done fb -> skip $ SplitOnSeqYield fb (SplitOnSeqEmpty s)
1801+ FL.Done b -> skip $ SplitOnSeqYield b (SplitOnSeqEmpty s)
1819 Skip s -> return $ Skip (SplitOnSeqEmpty s)
1803 Skip s -> return $ Skip (SplitOnSeqEmpty s)
1820 Stop -> return Stop
1804 Stop -> return Stop
@@ -1839,12 +1823,12 @@ splitOnSeq patArr (Fold fstep initial done) (Stream step state) =
1839 fs1 <- initial
1823 fs1 <- initial
1840 return $ Skip $ SplitOnSeqYield r (SplitOnSeqSingle fs1 s pat)
1824 return $ Skip $ SplitOnSeqYield r (SplitOnSeqSingle fs1 s pat)
1842− sfs <- fstep fs x
1826+ r <- fstep fs x
1843− case sfs of
1827+ case r of
1844− FL.Partial fs1 -> skip $ (SplitOnSeqSingle fs1 s pat)
1828+ FL.Partial fs1 -> skip $ SplitOnSeqSingle fs1 s pat
1845− FL.Done fb -> do
1829+ FL.Done b -> do
1846 fs1 <- initial
1830 fs1 <- initial
1847− skip $ SplitOnSeqYield fb (SplitOnSeqSingle fs1 s pat)
1831+ skip $ SplitOnSeqYield b (SplitOnSeqSingle fs1 s pat)
1848 Skip s -> return $ Skip $ SplitOnSeqSingle fs s pat
1832 Skip s -> return $ Skip $ SplitOnSeqSingle fs s pat
1849 Stop -> do
1833 Stop -> do
1850 r <- done fs
1834 r <- done fs
@@ -1859,13 +1843,13 @@ splitOnSeq patArr (Fold fstep initial done) (Stream step state) =
1859 skip $ SplitOnSeqYield r SplitOnSeqDone
1843 skip $ SplitOnSeqYield r SplitOnSeqDone
1860 stepOuter _ (SplitOnSeqWordDone n fs wrd) = do
1844 stepOuter _ (SplitOnSeqWordDone n fs wrd) = do
1861 let old = elemMask .&. (wrd `shiftR` (elemBits * (n - 1)))
1845 let old = elemMask .&. (wrd `shiftR` (elemBits * (n - 1)))
1862− sfs <- fstep fs (toEnum $ fromIntegral old)
1846+ r <- fstep fs (toEnum $ fromIntegral old)
1863− case sfs of
1847+ case r of
1864 FL.Partial fs1 -> skip $ SplitOnSeqWordDone (n - 1) fs1 wrd
1848 FL.Partial fs1 -> skip $ SplitOnSeqWordDone (n - 1) fs1 wrd
1865− FL.Done r -> do
1849+ FL.Done b -> do
1866 fs1 <- initial
1850 fs1 <- initial
1867 let next = SplitOnSeqWordDone (n - 1) fs1 wrd
1851 let next = SplitOnSeqWordDone (n - 1) fs1 wrd
1868− skip $ SplitOnSeqYield r next
1852+ skip $ SplitOnSeqYield b next
1870 stepOuter gst (SplitOnSeqWordInit st0) =
1854 stepOuter gst (SplitOnSeqWordInit st0) =
1871 go SPEC 0 0 st0
1855 go SPEC 0 0 st0
@@ -1910,18 +1894,18 @@ splitOnSeq patArr (Fold fstep initial done) (Stream step state) =
1910 let wrd1 = addToWord wrd x
1894 let wrd1 = addToWord wrd x
1911 old = (wordMask .&. wrd)
1895 old = (wordMask .&. wrd)
1912 `shiftR` (elemBits * (patLen - 1))
1896 `shiftR` (elemBits * (patLen - 1))
1913− sfs <- fstep fs (toEnum $ fromIntegral old)
1897+ r <- fstep fs (toEnum $ fromIntegral old)
1914− case sfs of
1898+ case r of
1915 FL.Partial fs1 -> do
1899 FL.Partial fs1 -> do
1916 if wrd1 .&. wordMask == wordPat
1900 if wrd1 .&. wordMask == wordPat
1918− r <- done fs1
1902+ b <- done fs1
1919 let next = SplitOnSeqWordInit s
1903 let next = SplitOnSeqWordInit s
1920− skip $ SplitOnSeqYield r next
1904+ skip $ SplitOnSeqYield b next
1921 else go SPEC wrd1 s fs1
1905 else go SPEC wrd1 s fs1
1922− FL.Done r ->
1906+ FL.Done b ->
1923 let next = SplitOnSeqWordInit s
1907 let next = SplitOnSeqWordInit s
1924− in skip $ SplitOnSeqYield r next
1908+ in skip $ SplitOnSeqYield b next
1925 Skip s -> go SPEC wrd s fs
1909 Skip s -> go SPEC wrd s fs
1926 Stop -> skip $ SplitOnSeqWordDone patLen fs wrd
1910 Stop -> skip $ SplitOnSeqWordDone patLen fs wrd
@@ -1963,16 +1947,16 @@ splitOnSeq patArr (Fold fstep initial done) (Stream step state) =
1963 Yield x s -> do
1947 Yield x s -> do
1964 old <- liftIO $ peek rh
1948 old <- liftIO $ peek rh
1965 let cksum1 = deltaCksum cksum old x
1949 let cksum1 = deltaCksum cksum old x
1966− sfs <- fstep fs old
1950+ r <- fstep fs old
1967− case sfs of
1951+ case r of
1968 FL.Partial fs1 -> do
1952 FL.Partial fs1 -> do
1969 rh1 <- liftIO (RB.unsafeInsert rb rh x)
1953 rh1 <- liftIO (RB.unsafeInsert rb rh x)
1970− if (cksum1 == patHash)
1954+ if cksum1 == patHash
1971 then skip $ SplitOnSeqKRCheck fs1 s rb rh1
1955 then skip $ SplitOnSeqKRCheck fs1 s rb rh1
1972 else go SPEC fs1 s rh1 cksum1
1956 else go SPEC fs1 s rh1 cksum1
1973− FL.Done r ->
1957+ FL.Done b ->
1974 let next = SplitOnSeqKRInit 0 s rb (RB.startOf rb)
1958 let next = SplitOnSeqKRInit 0 s rb (RB.startOf rb)
1975− in skip $ SplitOnSeqYield r next
1959+ in skip $ SplitOnSeqYield b next
1976 Skip s -> go SPEC fs s rh cksum
1960 Skip s -> go SPEC fs s rh cksum
1977 Stop -> skip $ SplitOnSeqKRDone patLen fs rb rh
1961 Stop -> skip $ SplitOnSeqKRDone patLen fs rb rh
@@ -2015,13 +1999,13 @@ splitOnSeq patArr (Fold fstep initial done) (Stream step state) =
2015 stepOuter _ (SplitOnSeqKRDone n fs rb rh) = do
1999 stepOuter _ (SplitOnSeqKRDone n fs rb rh) = do
2016 old <- liftIO $ peek rh
2000 old <- liftIO $ peek rh
2017 let rh1 = RB.advance rb rh
2001 let rh1 = RB.advance rb rh
2018− sfs <- fstep fs old
2002+ r <- fstep fs old
2019− case sfs of
2003+ case r of
2020 FL.Partial fs1 -> skip $ SplitOnSeqKRDone (n - 1) fs1 rb rh1
2004 FL.Partial fs1 -> skip $ SplitOnSeqKRDone (n - 1) fs1 rb rh1
2021− FL.Done r -> do
2005+ FL.Done b -> do
2022 fs1 <- initial
2006 fs1 <- initial
2023 let next = SplitOnSeqKRDone (n - 1) fs1 rb rh1
2007 let next = SplitOnSeqKRDone (n - 1) fs1 rb rh1
2024− skip $ SplitOnSeqYield r next
2008+ skip $ SplitOnSeqYield b next
2026{-# ANN type SplitOnSuffixSeqState Fuse #-}
2010{-# ANN type SplitOnSuffixSeqState Fuse #-}
2027data SplitOnSuffixSeqState rb rh ck w fs s b x =
2011data SplitOnSuffixSeqState rb rh ck w fs s b x =
@@ -2078,22 +2062,22 @@ splitOnSuffixSeq withSep patArr (Fold fstep initial done) (Stream step state) =
2078 processYieldSingle pat x s fs =
2062 processYieldSingle pat x s fs =
2079 if pat == x
2063 if pat == x
2081− sfs <- if withSep then fstep fs x else return $ FL.Partial fs
2065+ r <- if withSep then fstep fs x else return $ FL.Partial fs
2082− case sfs of
2066+ case r of
2083 FL.Partial fs1 -> do
2067 FL.Partial fs1 -> do
2084− r <- done fs1
2068+ b <- done fs1
2085 let next = SplitOnSuffixSeqSingleInit s pat
2069 let next = SplitOnSuffixSeqSingleInit s pat
2086− skip $ SplitOnSuffixSeqYield r next
2070+ skip $ SplitOnSuffixSeqYield b next
2087− FL.Done r ->
2071+ FL.Done b ->
2088 let next = SplitOnSuffixSeqSingleInit s pat
2072 let next = SplitOnSuffixSeqSingleInit s pat
2089− in skip $ SplitOnSuffixSeqYield r next
2073+ in skip $ SplitOnSuffixSeqYield b next
2091− sfs <- fstep fs x
2075+ r <- fstep fs x
2092− case sfs of
2076+ case r of
2093 FL.Partial fs1 -> skip $ SplitOnSuffixSeqSingle fs1 s pat
2077 FL.Partial fs1 -> skip $ SplitOnSuffixSeqSingle fs1 s pat
2094− FL.Done r ->
2078+ FL.Done b ->
2095 let next = SplitOnSuffixSeqSingleInit s pat
2079 let next = SplitOnSuffixSeqSingleInit s pat
2096− in skip $ SplitOnSuffixSeqYield r next
2080+ in skip $ SplitOnSuffixSeqYield b next
2098 -- For Rabin-Karp search
2082 -- For Rabin-Karp search
2099 k = 2891336453 :: Word32
2083 k = 2891336453 :: Word32
@@ -2135,13 +2119,13 @@ splitOnSuffixSeq withSep patArr (Fold fstep initial done) (Stream step state) =
2135 case res of
2119 case res of
2136 Yield x s -> do
2120 Yield x s -> do
2137 acc <- initial
2121 acc <- initial
2138− sfs <- fstep acc x
2122+ r <- fstep acc x
2139− case sfs of
2123+ case r of
2140− FL.Partial acc1 -> do
2124+ FL.Partial fs -> do
2141− r <- done acc1
2125+ b <- done fs
2142− skip $ SplitOnSuffixSeqYield r (SplitOnSuffixSeqEmpty s)
2126+ skip $ SplitOnSuffixSeqYield b (SplitOnSuffixSeqEmpty s)
2143− FL.Done r ->
2127+ FL.Done b ->
2144− skip $ SplitOnSuffixSeqYield r (SplitOnSuffixSeqEmpty s)
2128+ skip $ SplitOnSuffixSeqYield b (SplitOnSuffixSeqEmpty s)
2145 Skip s -> skip (SplitOnSuffixSeqEmpty s)
2129 Skip s -> skip (SplitOnSuffixSeqEmpty s)
2146 Stop -> return Stop
2130 Stop -> return Stop
@@ -2180,13 +2164,13 @@ splitOnSuffixSeq withSep patArr (Fold fstep initial done) (Stream step state) =
2180 skip $ SplitOnSuffixSeqYield r SplitOnSuffixSeqDone
2164 skip $ SplitOnSuffixSeqYield r SplitOnSuffixSeqDone
2181 stepOuter _ (SplitOnSuffixSeqWordDone n fs wrd) = do
2165 stepOuter _ (SplitOnSuffixSeqWordDone n fs wrd) = do
2182 let old = elemMask .&. (wrd `shiftR` (elemBits * (n - 1)))
2166 let old = elemMask .&. (wrd `shiftR` (elemBits * (n - 1)))
2183− sfs <- fstep fs (toEnum $ fromIntegral old)
2167+ r <- fstep fs (toEnum $ fromIntegral old)
2184− case sfs of
2168+ case r of
2185 FL.Partial fs1 -> skip $ SplitOnSuffixSeqWordDone (n - 1) fs1 wrd
2169 FL.Partial fs1 -> skip $ SplitOnSuffixSeqWordDone (n - 1) fs1 wrd
2186− FL.Done r -> do
2170+ FL.Done b -> do
2187 fs1 <- initial
2171 fs1 <- initial
2188 let next = SplitOnSuffixSeqWordDone (n - 1) fs1 wrd
2172 let next = SplitOnSuffixSeqWordDone (n - 1) fs1 wrd
2189− skip $ SplitOnSuffixSeqYield r next
2173+ skip $ SplitOnSuffixSeqYield b next
2191 stepOuter gst (SplitOnSuffixSeqWordInit st0) = do
2175 stepOuter gst (SplitOnSuffixSeqWordInit st0) = do
2192 res <- step (adaptState gst) st0
2176 res <- step (adaptState gst) st0
@@ -2194,12 +2178,12 @@ splitOnSuffixSeq withSep patArr (Fold fstep initial done) (Stream step state) =
2194 Yield x s -> do
2178 Yield x s -> do
2195 fs <- initial
2179 fs <- initial
2196 let wrd = addToWord 0 x
2180 let wrd = addToWord 0 x
2197− sfs <- if withSep then fstep fs x else return $ FL.Partial fs
2181+ r <- if withSep then fstep fs x else return $ FL.Partial fs
2198− case sfs of
2182+ case r of
2199 FL.Partial fs1 -> go SPEC 1 wrd s fs1
2183 FL.Partial fs1 -> go SPEC 1 wrd s fs1
2200− FL.Done r -> do
2184+ FL.Done b -> do
2201 let next = SplitOnSuffixSeqWordInit s
2185 let next = SplitOnSuffixSeqWordInit s
2202− in skip $ SplitOnSuffixSeqYield r next
2186+ in skip $ SplitOnSuffixSeqYield b next
2203 Skip s -> skip (SplitOnSuffixSeqWordInit s)
2187 Skip s -> skip (SplitOnSuffixSeqWordInit s)
2204 Stop -> return Stop
2188 Stop -> return Stop
@@ -2211,20 +2195,20 @@ splitOnSuffixSeq withSep patArr (Fold fstep initial done) (Stream step state) =
2211 case res of
2195 case res of
2212 Yield x s -> do
2196 Yield x s -> do
2213 let wrd1 = addToWord wrd x
2197 let wrd1 = addToWord wrd x
2214− sfs <- if withSep then fstep fs x else return $ FL.Partial fs
2198+ r <- if withSep then fstep fs x else return $ FL.Partial fs
2215− case sfs of
2199+ case r of
2216 FL.Partial fs1 ->
2200 FL.Partial fs1 ->
2217 if idx /= maxIndex
2201 if idx /= maxIndex
2218 then go SPEC (idx + 1) wrd1 s fs1
2202 then go SPEC (idx + 1) wrd1 s fs1
2219 else if wrd1 .&. wordMask /= wordPat
2203 else if wrd1 .&. wordMask /= wordPat
2220 then skip $ SplitOnSuffixSeqWordLoop wrd1 s fs1
2204 then skip $ SplitOnSuffixSeqWordLoop wrd1 s fs1
2222− r <- done fs
2206+ b <- done fs
2223 let next = SplitOnSuffixSeqWordInit s
2207 let next = SplitOnSuffixSeqWordInit s
2224− skip $ SplitOnSuffixSeqYield r next
2208+ skip $ SplitOnSuffixSeqYield b next
2225− FL.Done r ->
2209+ FL.Done b ->
2226 let next = SplitOnSuffixSeqWordInit s
2210 let next = SplitOnSuffixSeqWordInit s
2227− in skip $ SplitOnSuffixSeqYield r next
2211+ in skip $ SplitOnSuffixSeqYield b next
2228 Skip s -> go SPEC idx wrd s fs
2212 Skip s -> go SPEC idx wrd s fs
2229 Stop -> skip $ SplitOnSuffixSeqWordDone idx fs wrd
2213 Stop -> skip $ SplitOnSuffixSeqWordDone idx fs wrd
@@ -2241,21 +2225,21 @@ splitOnSuffixSeq withSep patArr (Fold fstep initial done) (Stream step state) =
2241 let wrd1 = addToWord wrd x
2225 let wrd1 = addToWord wrd x
2242 old = (wordMask .&. wrd)
2226 old = (wordMask .&. wrd)
2243 `shiftR` (elemBits * (patLen - 1))
2227 `shiftR` (elemBits * (patLen - 1))
2245 if withSep
2229 if withSep
2246 then fstep fs x
2230 then fstep fs x
2247 else fstep fs (toEnum $ fromIntegral old)
2231 else fstep fs (toEnum $ fromIntegral old)
2248− case sfs of
2232+ case r of
2249 FL.Partial fs1 ->
2233 FL.Partial fs1 ->
2250 if wrd1 .&. wordMask == wordPat
2234 if wrd1 .&. wordMask == wordPat
2252− r <- done fs1
2236+ b <- done fs1
2253 let next = SplitOnSuffixSeqWordInit s
2237 let next = SplitOnSuffixSeqWordInit s
2254− skip $ SplitOnSuffixSeqYield r next
2238+ skip $ SplitOnSuffixSeqYield b next
2255 else go SPEC wrd1 s fs1
2239 else go SPEC wrd1 s fs1
2256− FL.Done r ->
2240+ FL.Done b ->
2257 let next = SplitOnSuffixSeqWordInit s
2241 let next = SplitOnSuffixSeqWordInit s
2258− in skip $ SplitOnSuffixSeqYield r next
2242+ in skip $ SplitOnSuffixSeqYield b next
2259 Skip s -> go SPEC wrd s fs
2243 Skip s -> go SPEC wrd s fs
2261 if wrd .&. wordMask == wordPat
2245 if wrd .&. wordMask == wordPat
@@ -2276,12 +2260,13 @@ splitOnSuffixSeq withSep patArr (Fold fstep initial done) (Stream step state) =
2276 Yield x s -> do
2260 Yield x s -> do
2277 rh1 <- liftIO $ RB.unsafeInsert rb rh0 x
2261 rh1 <- liftIO $ RB.unsafeInsert rb rh0 x
2278 fs <- initial
2262 fs <- initial
2279− sfs <- if withSep then fstep fs x else return $ FL.Partial fs
2263+ r <- if withSep then fstep fs x else return $ FL.Partial fs
2280− case sfs of
2264+ case r of
2281− FL.Partial fs1 -> skip $ SplitOnSuffixSeqKRInit1 fs1 s rb rh1
2266+ skip $ SplitOnSuffixSeqKRInit1 fs1 s rb rh1
2282− FL.Done r ->
2267+ FL.Done b ->
2283 let next = SplitOnSuffixSeqKRInit 0 s rb (RB.startOf rb)
2268 let next = SplitOnSuffixSeqKRInit 0 s rb (RB.startOf rb)
2284− in skip $ SplitOnSuffixSeqYield r next
2269+ in skip $ SplitOnSuffixSeqYield b next
2285 Skip s -> skip $ SplitOnSuffixSeqKRInit idx0 s rb rh0
2270 Skip s -> skip $ SplitOnSuffixSeqKRInit idx0 s rb rh0
2286 Stop -> return Stop
2271 Stop -> return Stop
@@ -2295,8 +2280,8 @@ splitOnSuffixSeq withSep patArr (Fold fstep initial done) (Stream step state) =
2295 case res of
2280 case res of
2296 Yield x s -> do
2281 Yield x s -> do
2297 rh1 <- liftIO (RB.unsafeInsert rb rh x)
2282 rh1 <- liftIO (RB.unsafeInsert rb rh x)
2298− sfs <- if withSep then fstep fs x else return $ FL.Partial fs
2283+ r <- if withSep then fstep fs x else return $ FL.Partial fs
2299− case sfs of
2284+ case r of
2300 FL.Partial fs1 ->
2285 FL.Partial fs1 ->
2301 if idx /= maxIndex
2286 if idx /= maxIndex
2302 then go SPEC (idx + 1) rh1 s fs1
2287 then go SPEC (idx + 1) rh1 s fs1
@@ -2305,10 +2290,12 @@ splitOnSuffixSeq withSep patArr (Fold fstep initial done) (Stream step state) =
2305 !ringHash = fold addCksum 0 rb
2290 !ringHash = fold addCksum 0 rb
2306 in if ringHash == patHash
2291 in if ringHash == patHash
2307 then SplitOnSuffixSeqKRCheck fs1 s rb rh1
2292 then SplitOnSuffixSeqKRCheck fs1 s rb rh1
2308− else SplitOnSuffixSeqKRLoop fs1 s rb rh1 ringHash
2293+ else SplitOnSuffixSeqKRLoop
2294+ fs1 s rb rh1 ringHash
2309− FL.Done r ->
2295+ FL.Done b ->
2310− let next = SplitOnSuffixSeqKRInit 0 st rb (RB.startOf rb)
2296+ let next = SplitOnSuffixSeqKRInit
2297+ 0 st rb (RB.startOf rb)
2311− in skip $ SplitOnSuffixSeqYield r next
2298+ in skip $ SplitOnSuffixSeqYield b next
2312 Skip s -> go SPEC idx rh s fs
2299 Skip s -> go SPEC idx rh s fs
2313 Stop -> do
2300 Stop -> do
2314 -- do not issue a blank segment when we end at pattern
2301 -- 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) =
2332 old <- liftIO $ peek rh
2319 old <- liftIO $ peek rh
2333 rh1 <- liftIO (RB.unsafeInsert rb rh x)
2320 rh1 <- liftIO (RB.unsafeInsert rb rh x)
2334 let cksum1 = deltaCksum cksum old x
2321 let cksum1 = deltaCksum cksum old x
2335− sfs <- if withSep then fstep fs x else fstep fs old
2322+ r <- if withSep then fstep fs x else fstep fs old
2336− case sfs of
2323+ case r of
2337 FL.Partial fs1 ->
2324 FL.Partial fs1 ->
2338 if (cksum1 /= patHash)
2325 if (cksum1 /= patHash)
2339 then go SPEC fs1 s rh1 cksum1
2326 then go SPEC fs1 s rh1 cksum1
2340 else skip $ SplitOnSuffixSeqKRCheck fs1 s rb rh1
2327 else skip $ SplitOnSuffixSeqKRCheck fs1 s rb rh1
2341− FL.Done r ->
2328+ FL.Done b ->
2342− let next = SplitOnSuffixSeqKRInit 0 st rb (RB.startOf rb)
2329+ let next = SplitOnSuffixSeqKRInit
2330+ 0 st rb (RB.startOf rb)
2343− in skip $ SplitOnSuffixSeqYield r next
2331+ in skip $ SplitOnSuffixSeqYield b next
2344 Skip s -> go SPEC fs s rh cksum
2332 Skip s -> go SPEC fs s rh cksum
2346 if RB.unsafeEqArray rb rh patArr
2334 if RB.unsafeEqArray rb rh patArr
@@ -2365,13 +2353,13 @@ splitOnSuffixSeq withSep patArr (Fold fstep initial done) (Stream step state) =
2365 stepOuter _ (SplitOnSuffixSeqKRDone n fs rb rh) = do
2353 stepOuter _ (SplitOnSuffixSeqKRDone n fs rb rh) = do
2366 old <- liftIO $ peek rh
2354 old <- liftIO $ peek rh
2367 let rh1 = RB.advance rb rh
2355 let rh1 = RB.advance rb rh
2368− sfs <- fstep fs old
2356+ r <- fstep fs old
2369− case sfs of
2357+ case r of
2370 FL.Partial fs1 -> skip $ SplitOnSuffixSeqKRDone (n - 1) fs1 rb rh1
2358 FL.Partial fs1 -> skip $ SplitOnSuffixSeqKRDone (n - 1) fs1 rb rh1
2371− FL.Done r -> do
2359+ FL.Done b -> do
2372 fs1 <- initial
2360 fs1 <- initial
2373 let next = SplitOnSuffixSeqKRDone (n - 1) fs1 rb rh1
2361 let next = SplitOnSuffixSeqKRDone (n - 1) fs1 rb rh1
2374− skip $ SplitOnSuffixSeqYield r next
2362+ skip $ SplitOnSuffixSeqYield b next
2376{-# ANN type SplitState Fuse #-}
2364{-# ANN type SplitState Fuse #-}
2377data SplitState s arr
2365data SplitState s arr
@@ -3765,12 +3753,10 @@ scanlMx' fstep begin done s =
3765 (begin >>= \x -> x `seq` done x) `consM` postscanlMx' fstep begin done s
3753 (begin >>= \x -> x `seq` done x) `consM` postscanlMx' fstep begin done s
3768−data PostScanState fsM s a = PostScan s !fsM
3756+data PostScanState s f = PostScan s !f
3770−-- XXX PostScanWith is can be eliminated if we abstract Yield
3771{-# INLINE_NORMAL postscanOnce #-}
3758{-# 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
3774postscanOnce (FL.Fold fstep begin done) (Stream step state) =
3760postscanOnce (FL.Fold fstep begin done) (Stream step state) =
3775 Stream step' (PostScan state begin)
3761 Stream step' (PostScan state begin)
@@ -3780,15 +3766,14 @@ postscanOnce (FL.Fold fstep begin done) (Stream step state) =
3780 step' gst (PostScan st acc) = do
3766 step' gst (PostScan st acc) = do
3781 r <- step (adaptState gst) st
3767 r <- step (adaptState gst) st
3782 case r of
3768 case r of
3783− -- XXX Move Yield to a common function
3784 Yield x s -> do
3769 Yield x s -> do
3785 old <- acc
3770 old <- acc
3786− y <- fstep old x
3771+ res <- fstep old x
3787− case y of
3772+ case res of
3788− FL.Partial sres -> do
3773+ FL.Partial fs -> do
3789− !v <- done sres
3774+ !v <- done fs
3790− return $ Yield v $ PostScan s (return sres)
3775+ return $ Yield v $ PostScan s (return fs)
3791− FL.Done _ -> return $ Stop
3776+ FL.Done _ -> return Stop
3792 Skip s -> return $ Skip $ PostScan s acc
3777 Skip s -> return $ Skip $ PostScan s acc
3793 Stop -> return Stop
3778 Stop -> return Stop
@@ -4027,17 +4012,17 @@ tap (Fold fstep initial extract) (Stream step state) = Stream step' TapInit
4027 step' gst (Tapping acc st) = do
4012 step' gst (Tapping acc st) = do
4028 r <- step gst st
4013 r <- step gst st
4029 case r of
4014 case r of
4030− -- XXX Abstract Yield?
4031 Yield x s -> do
4015 Yield x s -> do
4032− acc1 <- fstep acc x
4016+ res <- fstep acc x
4034− $ case acc1 of
4019+ $ case res of
4035− FL.Partial sres -> Yield x (Tapping sres s)
4020+ FL.Partial fs -> Tapping fs s
4036− FL.Done _ -> Yield x (TapDone s)
4021+ FL.Done _ -> TapDone s
4037 Skip s -> return $ Skip (Tapping acc s)
4022 Skip s -> return $ Skip (Tapping acc s)
4038 Stop -> do
4023 Stop -> do
4039 void $ extract acc
4024 void $ extract acc
4040− return $ Stop
4025+ return Stop
4041 step' gst (TapDone st) = do
4026 step' gst (TapDone st) = do
4042 r <- step gst st
4027 r <- step gst st
@@ -4067,21 +4052,22 @@ tapOffsetEvery offset n (Fold fstep initial extract) (Stream step state) =
4067 step' gst (TapOffTapping acc st count) = do
4052 step' gst (TapOffTapping acc st count) = do
4068 r <- step gst st
4053 r <- step gst st
4069 case r of
4054 case r of
4070− Yield x s ->
4055+ Yield x s -> do
4071− if count <= 0
4057+ if count <= 0
4072− -- XXX Abstract the then branch?
4058+ then do
4074− acc1 <- fstep acc x
4059+ res <- fstep acc x
4076− $ case acc1 of
4061+ $ case res of
4077− FL.Partial sres ->
4062+ FL.Partial sres ->
4078− Yield x $ TapOffTapping sres s (n - 1)
4063+ TapOffTapping sres s (n - 1)
4079− FL.Done _ -> Yield x (TapOffDone s)
4064+ FL.Done _ -> TapOffDone s
4080− else return $ Yield x $ TapOffTapping acc s (count - 1)
4065+ else return $ TapOffTapping acc s (count - 1)
4066+ return $ Yield x next
4081 Skip s -> return $ Skip (TapOffTapping acc s count)
4067 Skip s -> return $ Skip (TapOffTapping acc s count)
4082 Stop -> do
4068 Stop -> do
4083 void $ extract acc
4069 void $ extract acc
4084− return $ Stop
4070+ return Stop
4085 step' gst (TapOffDone st) = do
4071 step' gst (TapOffDone st) = do
4086 r <- step gst st
4072 r <- step gst st