on master help sign in
Fix & improve doc for Parser(D).groupBy
d601012Adithya Kumar
src/Streamly/Internal/Data/Parser.hs+21−11
@@ -576,25 +576,35 @@ escapedFrameBy _begin _end _escape _p = undefined
576wordBy :: MonadCatch m => (a -> Bool) -> Fold m a b -> Parser m a b
576wordBy :: MonadCatch m => (a -> Bool) -> Fold m a b -> Parser m a b
577wordBy f = K.toParserK . D.wordBy f
577wordBy f = K.toParserK . D.wordBy f
578
578
579+-- | Given an input stream @[a,b,c,...]@ and a comparison function @cmp@, the
579−-- | @groupBy cmp f $ S.fromList [a,b,c,...]@ assigns the element @a@ to the
580+-- parser assigns the element @a@ to the first group, then if @a \`cmp` b@ is
580−-- first group, then if @a \`cmp` b@ is 'True' @b@ is also assigned to the same
581+-- 'True' @b@ is also assigned to the same group. If @a \`cmp` c@ is 'True'
581−-- group. If @a \`cmp` c@ is 'True' then @c@ is also assigned to the same
582+-- then @c@ is also assigned to the same group and so on. When the comparison
582−-- group and so on. When the comparison fails a new group is started. Each
583−-- group is folded using the 'Fold' @f@ and the result of the fold is emitted
583+-- fails the parser is terminated. Each group is folded using the 'Fold' @f@ and
584−-- in the output stream.
584+-- the result of the fold is the result of the parser.
585--
585--
586-- * Stops - when the comparison fails.
586-- * Stops - when the comparison fails.
587-- * Fails - never.
587-- * Fails - never.
588--
588--
589−-- @
589+-- > runGroupsBy eq =
590+-- > Stream.toList
590−-- S.groupsBy cmp f = S.parseMany (PR.groupBy cmp f)
591+-- > . Stream.parseMany (groupBy eq Fold.toList)
591−-- @
592+-- > . Stream.fromList
592--
593--
593−-- /Unimplemented/
594+-- >>> runGroupsBy (<) []
595+-- []
596+--
597+-- >>> runGroupsBy (<) [1]
598+-- [[1]]
599+--
600+-- >>> runGroupsBy (<) [3, 5, 4, 1, 2, 0]
601+-- [[3, 5, 4], [1, 2], [0]]
602+--
603+-- /Internal/
594--
604--
595{-# INLINABLE groupBy #-}
605{-# INLINABLE groupBy #-}
596groupBy :: MonadCatch m => (a -> a -> Bool) -> Fold m a b -> Parser m a b
606groupBy :: MonadCatch m => (a -> a -> Bool) -> Fold m a b -> Parser m a b
597−groupBy cmp = K.toParserK . D.groupBy cmp
607+groupBy eq = K.toParserK . D.groupBy eq
598
608
599-- | Unlike 'groupBy' this combinator performs a rolling comparison of two
609-- | Unlike 'groupBy' this combinator performs a rolling comparison of two
600-- successive elements in the input stream. Assuming the input stream to the
610-- successive elements in the input stream. Assuming the input stream to the
src/Streamly/Internal/Data/Parser/ParserD.hs+2−2
@@ -597,7 +597,7 @@ data GroupByState a s
597--
597--
598{-# INLINE groupBy #-}
598{-# INLINE groupBy #-}
599groupBy :: Monad m => (a -> a -> Bool) -> Fold m a b -> Parser m a b
599groupBy :: Monad m => (a -> a -> Bool) -> Fold m a b -> Parser m a b
600−groupBy cmp (Fold fstep finitial fextract) = Parser step initial extract
600+groupBy eq (Fold fstep finitial fextract) = Parser step initial extract
601
601
602 where
602 where
603
603
@@ -618,7 +618,7 @@ groupBy cmp (Fold fstep finitial fextract) = Parser step initial extract
618
618
619 step (GroupByInit s) a = grouper s a a
619 step (GroupByInit s) a = grouper s a a
620 step (GroupByGrouping a0 s) a =
620 step (GroupByGrouping a0 s) a =
621− if cmp a0 a
621+ if eq a0 a
622 then grouper s a0 a
622 then grouper s a0 a
623 else Done 1 <$> fextract s
623 else Done 1 <$> fextract s
624
624