on master help sign in
Add matchBy in Data.Parser and benchmarks for matchBy
fa417ebAdithya Kumar
benchmark/Streamly/Benchmark/Data/Parser.hs+5
@@ -157,6 +157,10 @@ takeFramedByEsc_ _ = Stream.parse parser
157eqBy :: Int -> Stream IO Int -> IO ()
157eqBy :: Int -> Stream IO Int -> IO ()
158eqBy len = Stream.parse (PR.eqBy (==) [1 .. len])
158eqBy len = Stream.parse (PR.eqBy (==) [1 .. len])
159
159
160+{-# INLINE matchBy #-}
161+matchBy :: Int -> Stream IO Int -> IO ()
162+matchBy len = Stream.parse (PR.matchBy (==) (Stream.enumerateFromTo 1 len))
163+
160{-# INLINE takeWhile #-}
164{-# INLINE takeWhile #-}
161takeWhile :: MonadThrow m => Int -> Stream m Int -> m ()
165takeWhile :: MonadThrow m => Int -> Stream m Int -> m ()
162takeWhile value = Stream.parse (PR.takeWhile (<= value) Fold.drain)
166takeWhile value = Stream.parse (PR.takeWhile (<= value) Fold.drain)
@@ -478,6 +482,7 @@ o_1_space_serial value =
478 , benchIOSink value "parseMany (groupBy (<))" (parseManyGroupBy (<))
482 , benchIOSink value "parseMany (groupBy (<))" (parseManyGroupBy (<))
479 , benchIOSink value "parseMany (groupBy (==))" (parseManyGroupBy (==))
483 , benchIOSink value "parseMany (groupBy (==))" (parseManyGroupBy (==))
480 , benchIOSink value "eqBy" (eqBy value)
484 , benchIOSink value "eqBy" (eqBy value)
485+ , benchIOSink value "matchBy" (matchBy value)
481 ]
486 ]
482
487
483o_1_space_filesystem :: BenchEnv -> [Benchmark]
488o_1_space_filesystem :: BenchEnv -> [Benchmark]
benchmark/Streamly/Benchmark/Data/Parser/ParserD.hs+5
@@ -70,6 +70,10 @@ benchIOSink value name f =
70eqBy :: Int -> Stream IO Int -> IO ()
70eqBy :: Int -> Stream IO Int -> IO ()
71eqBy len = Stream.parseD (PR.eqBy (==) [1 .. len])
71eqBy len = Stream.parseD (PR.eqBy (==) [1 .. len])
72
72
73+{-# INLINE matchBy #-}
74+matchBy :: Int -> Stream IO Int -> IO ()
75+matchBy len = Stream.parseD (PR.matchBy (==) (D.enumerateFromToIntegral 1 len))
76+
73{-# INLINE drainWhile #-}
77{-# INLINE drainWhile #-}
74drainWhile :: MonadThrow m => (a -> Bool) -> PR.Parser m a ()
78drainWhile :: MonadThrow m => (a -> Bool) -> PR.Parser m a ()
75drainWhile p = PR.takeWhile p Fold.drain
79drainWhile p = PR.takeWhile p Fold.drain
@@ -356,6 +360,7 @@ o_1_space_serial value =
356 -}
360 -}
357 , benchIOSink value "sequenceParser" sequenceParser
361 , benchIOSink value "sequenceParser" sequenceParser
358 , benchIOSink value "eqBy" (eqBy value)
362 , benchIOSink value "eqBy" (eqBy value)
363+ , benchIOSink value "matchBy" (matchBy value)
359 ]
364 ]
360
365
361o_1_space_serial_spanning :: Int -> [Benchmark]
366o_1_space_serial_spanning :: Int -> [Benchmark]
core/src/Streamly/Internal/Data/Parser.hs+6−2
@@ -103,6 +103,7 @@ module Streamly.Internal.Data.Parser
103 -- Grab a sequence of input elements by inspecting them
103 -- Grab a sequence of input elements by inspecting them
104 -- ** Exact match
104 -- ** Exact match
105 , eqBy
105 , eqBy
106+ , matchBy
106 , list
107 , list
107
108
108 -- ** By predicate
109 -- ** By predicate
@@ -1024,8 +1025,6 @@ groupByRollingEither :: Monad m =>
1024 (a -> a -> Bool) -> Fold m a b -> Fold m a c -> Parser m a (Either b c)
1025 (a -> a -> Bool) -> Fold m a b -> Fold m a c -> Parser m a (Either b c)
1025groupByRollingEither eq f1 = D.toParserK . D.groupByRollingEither eq f1
1026groupByRollingEither eq f1 = D.toParserK . D.groupByRollingEither eq f1
1026
1027
1027−-- XXX Use a stream instead of a list so that we can use any container type.
1028−
1029-- | Match the given sequence of elements using the given comparison function.
1028-- | Match the given sequence of elements using the given comparison function.
1030--
1029--
1031-- >>> Stream.parse (Parser.eqBy (==) "string") $ Stream.fromList "string"
1030-- >>> Stream.parse (Parser.eqBy (==) "string") $ Stream.fromList "string"
@@ -1039,6 +1038,11 @@ groupByRollingEither eq f1 = D.toParserK . D.groupByRollingEither eq f1
1039eqBy :: Monad m => (a -> a -> Bool) -> [a] -> Parser m a ()
1038eqBy :: Monad m => (a -> a -> Bool) -> [a] -> Parser m a ()
1040eqBy cmp = D.toParserK . D.eqBy cmp
1039eqBy cmp = D.toParserK . D.eqBy cmp
1041
1040
1041+-- | Like eqBy but uses a stream instead of a list
1042+{-# INLINE matchBy #-}
1043+matchBy :: Monad m => (a -> a -> Bool) -> Stream m a -> Parser m a ()
1044+matchBy cmp = D.toParserK . D.matchBy cmp . Stream.toStreamD
1045+
1042-- | Match the input sequence with the supplied list and return it if
1046-- | Match the input sequence with the supplied list and return it if
1043-- successful.
1047-- successful.
1044--
1048--