on master help sign in
Introduce and expose chunkEq and chunkEqBy
commitdaabcb84b78fbea3f05a426d8a74ed93b3e8f9c4 authorAdithya Kumar <adithya@composewell.com> dateWed 19 Jul 2023, 11:02 (3 years ago) parent6a4d26b
3 files changed+45
benchmark/Streamly/Benchmark/Data/Parser.hs+10 core/src/Streamly/Data/Parser.hs+2 core/src/Streamly/Internal/Data/Parser/ParserD.hs+33
benchmark/Streamly/Benchmark/Data/Parser.hs+10
@@ -158,6 +158,14 @@ takeFramedByEsc_ _ = Stream.parse parser
158158listEqBy :: Int -> Stream IO Int -> IO (Either ParseError [Int])
159159listEqBy len = Stream.parse (PR.listEqBy (==) [1 .. len])
160160
161+{-# INLINE chunkEqBy #-}
162+chunkEqBy :: Int -> Stream IO Int -> IO (Either ParseError [Int])
163+chunkEqBy len = Stream.parse (PR.chunkEqBy (==) [1 .. len])
164+
165+{-# INLINE chunkEq #-}
166+chunkEq :: Int -> Stream IO Int -> IO (Either ParseError [Int])
167+chunkEq len = Stream.parse (PR.chunkEq [1 .. len])
168+
161169{-# INLINE streamEqBy #-}
162170streamEqBy :: Int -> Stream IO Int -> IO (Either ParseError ())
163171streamEqBy len = Stream.parse (PR.streamEqBy (==) (Stream.enumerateFromTo 1 len))
@@ -831,6 +839,8 @@ o_n_heap_serial value =
831839 , benchIOSink value "manyAlt" manyAlt
832840 , benchIOSink value "someAlt" someAlt
833841 , benchIOSink value "listEqBy" (listEqBy value)
842+ , benchIOSink value "chunkEqBy" (chunkEqBy value)
843+ , benchIOSink value "chunkEq" (chunkEq value)
834844 ]
835845
836846-- accumulate results in a list in IO
core/src/Streamly/Data/Parser.hs+2
@@ -60,6 +60,8 @@ module Streamly.Data.Parser
6060 , streamEqBy
6161 , listEqBy
6262 , listEq
63+ , chunkEqBy
64+ , chunkEq
6365
6466 -- * Combinators
6567 -- Mapping on output
core/src/Streamly/Internal/Data/Parser/ParserD.hs+33
@@ -78,6 +78,8 @@ module Streamly.Internal.Data.Parser.ParserD
7878 -- ** Exact match
7979 , listEq
8080 , listEqBy
81+ , chunkEq
82+ , chunkEqBy
8183 , streamEqBy
8284 , subsequenceBy
8385
@@ -2203,6 +2205,37 @@ streamEqBy cmp stream = streamEqByInternal cmp stream *> fromPure ()
22032205listEq :: (Monad m, Eq a) => [a] -> Parser a m [a]
22042206listEq = listEqBy (==)
22052207
2208+-- | Match the given sequence of elements using the given comparison function.
2209+-- Returns the original sequence if successful.
2210+--
2211+-- Definition:
2212+--
2213+-- >>> chunkEqBy cmp xs = Parser.streamEqBy cmp (Stream.fromFoldable xs) *> Parser.fromPure xs
2214+--
2215+-- Examples:
2216+--
2217+-- >>> Stream.parse (Parser.chunkEqBy (==) "string") $ Stream.fromList "string"
2218+-- Right "string"
2219+--
2220+-- >>> Stream.parse (Parser.chunkEqBy (==) "mismatch") $ Stream.fromList "match"
2221+-- Left (ParseError "streamEqBy: mismtach occurred")
2222+--
2223+{-# INLINE chunkEqBy #-}
2224+chunkEqBy
2225+ :: (Foldable f, Monad m) => (a -> a -> Bool) -> f a -> Parser a m (f a)
2226+chunkEqBy cmp xs = streamEqByInternal cmp (D.fromFoldable xs) *> fromPure xs
2227+
2228+-- | Match the input sequence with the supplied Foldable and return it if
2229+-- successful.
2230+--
2231+-- >>> chunkEq = Parser.chunkEqBy (==)
2232+--
2233+{-# INLINE chunkEq #-}
2234+chunkEq
2235+ :: (Eq a, Foldable f, Monad m)
2236+ => f a -> Parser a m (f a)
2237+chunkEq = chunkEqBy (==)
2238+
22062239-- | Match if the input stream is a subsequence of the argument stream i.e. all
22072240-- the elements of the input stream occur, in order, in the argument stream.
22082241-- The elements do not have to occur consecutively. A sequence is considered a