on master help sign in
Introduce and expose chunkEq and chunkEqBy
daabcb8Adithya Kumar
benchmark/Streamly/Benchmark/Data/Parser.hs+10
@@ -158,6 +158,14 @@ takeFramedByEsc_ _ = Stream.parse parser
158listEqBy :: Int -> Stream IO Int -> IO (Either ParseError [Int])
158listEqBy :: Int -> Stream IO Int -> IO (Either ParseError [Int])
159listEqBy len = Stream.parse (PR.listEqBy (==) [1 .. len])
159listEqBy len = Stream.parse (PR.listEqBy (==) [1 .. len])
160
160
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+
161{-# INLINE streamEqBy #-}
169{-# INLINE streamEqBy #-}
162streamEqBy :: Int -> Stream IO Int -> IO (Either ParseError ())
170streamEqBy :: Int -> Stream IO Int -> IO (Either ParseError ())
163streamEqBy len = Stream.parse (PR.streamEqBy (==) (Stream.enumerateFromTo 1 len))
171streamEqBy len = Stream.parse (PR.streamEqBy (==) (Stream.enumerateFromTo 1 len))
@@ -831,6 +839,8 @@ o_n_heap_serial value =
831 , benchIOSink value "manyAlt" manyAlt
839 , benchIOSink value "manyAlt" manyAlt
832 , benchIOSink value "someAlt" someAlt
840 , benchIOSink value "someAlt" someAlt
833 , benchIOSink value "listEqBy" (listEqBy value)
841 , benchIOSink value "listEqBy" (listEqBy value)
842+ , benchIOSink value "chunkEqBy" (chunkEqBy value)
843+ , benchIOSink value "chunkEq" (chunkEq value)
834 ]
844 ]
835
845
836-- accumulate results in a list in IO
846-- accumulate results in a list in IO
core/src/Streamly/Data/Parser.hs+2
@@ -60,6 +60,8 @@ module Streamly.Data.Parser
60 , streamEqBy
60 , streamEqBy
61 , listEqBy
61 , listEqBy
62 , listEq
62 , listEq
63+ , chunkEqBy
64+ , chunkEq
63
65
64 -- * Combinators
66 -- * Combinators
65 -- Mapping on output
67 -- Mapping on output
core/src/Streamly/Internal/Data/Parser/ParserD.hs+33
@@ -78,6 +78,8 @@ module Streamly.Internal.Data.Parser.ParserD
78 -- ** Exact match
78 -- ** Exact match
79 , listEq
79 , listEq
80 , listEqBy
80 , listEqBy
81+ , chunkEq
82+ , chunkEqBy
81 , streamEqBy
83 , streamEqBy
82 , subsequenceBy
84 , subsequenceBy
83
85
@@ -2203,6 +2205,37 @@ streamEqBy cmp stream = streamEqByInternal cmp stream *> fromPure ()
2203listEq :: (Monad m, Eq a) => [a] -> Parser a m [a]
2205listEq :: (Monad m, Eq a) => [a] -> Parser a m [a]
2204listEq = listEqBy (==)
2206listEq = listEqBy (==)
2205
2207
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+
2206-- | Match if the input stream is a subsequence of the argument stream i.e. all
2239-- | Match if the input stream is a subsequence of the argument stream i.e. all
2207-- the elements of the input stream occur, in order, in the argument stream.
2240-- the elements of the input stream occur, in order, in the argument stream.
2208-- The elements do not have to occur consecutively. A sequence is considered a
2241-- The elements do not have to occur consecutively. A sequence is considered a