on master help sign in
Fix & improve doc for Parser(D).groupBy
commitd601012817af068deb4790f16f20b22bb4c04a79 authorAdithya Kumar <adithya@composewell.com> dateSun 24 Jan 2021, 11:01 (6 years ago) parent401f504
2 files changed+23−13
src/Streamly/Internal/Data/Parser.hs+21−11 src/Streamly/Internal/Data/Parser/ParserD.hs+2−2
src/Streamly/Internal/Data/Parser.hs+21−11
@@ -576,25 +576,35 @@ escapedFrameBy _begin _end _escape _p = undefined
576576wordBy :: MonadCatch m => (a -> Bool) -> Fold m a b -> Parser m a b
577577wordBy f = K.toParserK . D.wordBy f
578578
579−-- | @groupBy cmp f $ S.fromList [a,b,c,...]@ assigns the element @a@ to the
580−-- first group, then if @a \`cmp` b@ is 'True' @b@ is also assigned to the same
581−-- group. If @a \`cmp` c@ is 'True' then @c@ is also assigned to the same
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
584−-- in the output stream.
579+-- | Given an input stream @[a,b,c,...]@ and a comparison function @cmp@, the
580+-- parser assigns the element @a@ to the first group, then if @a \`cmp` b@ is
581+-- 'True' @b@ is also assigned to the same group. If @a \`cmp` c@ is 'True'
582+-- then @c@ is also assigned to the same group and so on. When the comparison
583+-- fails the parser is terminated. Each group is folded using the 'Fold' @f@ and
584+-- the result of the fold is the result of the parser.
585585--
586586-- * Stops - when the comparison fails.
587587-- * Fails - never.
588588--
589−-- @
590−-- S.groupsBy cmp f = S.parseMany (PR.groupBy cmp f)
591−-- @
589+-- > runGroupsBy eq =
590+-- > Stream.toList
591+-- > . Stream.parseMany (groupBy eq Fold.toList)
592+-- > . Stream.fromList
592593--
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/
594604--
595605{-# INLINABLE groupBy #-}
596606groupBy :: 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
598608
599609-- | Unlike 'groupBy' this combinator performs a rolling comparison of two
600610-- 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
597597--
598598{-# INLINE groupBy #-}
599599groupBy :: 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
601601
602602 where
603603
@@ -618,7 +618,7 @@ groupBy cmp (Fold fstep finitial fextract) = Parser step initial extract
618618
619619 step (GroupByInit s) a = grouper s a a
620620 step (GroupByGrouping a0 s) a =
621− if cmp a0 a
621+ if eq a0 a
622622 then grouper s a0 a
623623 else Done 1 <$> fextract s
624624