@@ -141,6 +141,7 @@ module Streamly.Internal.Data.Stream.StreamD.Nesting
141 -- | Group segments of a stream and fold. Special case of parsing.
141 -- | Group segments of a stream and fold. Special case of parsing.
142 , groupsOf
142 , groupsOf
143 , groupsBy
143 , groupsBy
144 , groupsRollingBy
145 , groupsRollingBy
146 -- ** Splitting
147 -- ** Splitting
@@ -1896,16 +1897,23 @@ data GroupByState st fs a b
1896 | GroupingYield !b (GroupByState st fs a b)
1897 | GroupingYield !b (GroupByState st fs a b)
1897 | GroupingDone
1898 | GroupingDone
1900+-- | The argument order of the comparison function in `groupsWhile` is
1901+-- different than that of `groupsBy`.
1903+-- In `groupsBy` the comparison function takes the next element as the first
1904+-- argument and the previous element as the second argument. In `groupsWhile`
1905+-- the first argument is the previous element and second argument is the next
1899−{-# INLINE_NORMAL groupsBy #-}
1907+{-# INLINE_NORMAL groupsWhile #-}
1900−groupsBy :: Monad m
1908+groupsWhile :: Monad m
1901 => (a -> a -> Bool)
1909 => (a -> a -> Bool)
1902 -> Fold m a b
1910 -> Fold m a b
1903 -> Stream m a
1911 -> Stream m a
1904 -> Stream m b
1912 -> Stream m b
1906−groupsBy eq fld = parseMany (PRD.groupBy eq fld)
1914+groupsWhile eq fld = parseMany (PRD.groupBy eq fld)
1908−groupsBy cmp (Fold fstep initial done) (Stream step state) =
1916+groupsWhile cmp (Fold fstep initial done) (Stream step state) =
1909 Stream stepOuter (GroupingInit state)
1917 Stream stepOuter (GroupingInit state)
@@ -1936,7 +1944,7 @@ groupsBy cmp (Fold fstep initial done) (Stream step state) =
1936 res <- step (adaptState gst) stt
1944 res <- step (adaptState gst) stt
1937 case res of
1945 case res of
1938 Yield x s -> do
1946 Yield x s -> do
1939− if cmp x prev
1947+ if cmp prev x
1941 r <- fstep acc x
1949 r <- fstep acc x
1942 case r of
1950 case r of
@@ -1966,7 +1974,7 @@ groupsBy cmp (Fold fstep initial done) (Stream step state) =
1966 res <- step (adaptState gst) stt
1974 res <- step (adaptState gst) stt
1967 case res of
1975 case res of
1968 Yield x s -> do
1976 Yield x s -> do
1969− if cmp x prev
1977+ if cmp prev x
1971 r <- fstep acc x
1979 r <- fstep acc x
1972 case r of
1980 case r of
@@ -1977,9 +1985,18 @@ groupsBy cmp (Fold fstep initial done) (Stream step state) =
1977 return $ Yield r (GroupingInitWith s x)
1985 return $ Yield r (GroupingInitWith s x)
1978 Skip s -> go SPEC s acc
1986 Skip s -> go SPEC s acc
1979 Stop -> done acc >>= \r -> return $ Yield r GroupingDone
1987 Stop -> done acc >>= \r -> return $ Yield r GroupingDone
1980− stepOuter _ (GroupingYield _ _) = error "groupsBy: Unreachable"
1988+ stepOuter _ (GroupingYield _ _) = error "groupsWhile: Unreachable"
1981 stepOuter _ GroupingDone = return Stop
1989 stepOuter _ GroupingDone = return Stop
1991+{-# DEPRECATED groupsBy "Please use groupsWhile instead. Please note the change in the argument order of the comparison function." #-}
1992+{-# INLINE_NORMAL groupsBy #-}
1994+ => (a -> a -> Bool)
1998+groupsBy cmp = groupsWhile (flip cmp)
1983{-# INLINE_NORMAL groupsRollingBy #-}
2000{-# INLINE_NORMAL groupsRollingBy #-}
1984groupsRollingBy :: Monad m
2001groupsRollingBy :: Monad m
1985 => (a -> a -> Bool)
2002 => (a -> a -> Bool)
@@ -2045,7 +2062,7 @@ groupsRollingBy cmp (Fold fstep initial done) (Stream step state) =
2048− -- XXX GHC: groupsBy has one less parameter in this go loop and it
2065+ -- XXX GHC: groupsWhile has one less parameter in this go loop and it
2049 -- fuses. However, groupsRollingBy does not fuse, removing the prev
2066 -- fuses. However, groupsRollingBy does not fuse, removing the prev
2050 -- parameter makes it fuse. Something needs to be fixed in GHC. The
2067 -- parameter makes it fuse. Something needs to be fixed in GHC. The
2051 -- workaround for this is noted in the comments below.
2068 -- workaround for this is noted in the comments below.