on master help sign in
Add matchBy in Data.Parser and benchmarks for matchBy
commitfa417eb03b95fa1e335002309e6b770983bedda3 authorAdithya Kumar <adithya@composewell.com> dateTue 4 Oct 2022, 18:17 (4 years ago) parente5930e2
3 files changed+16−2
benchmark/Streamly/Benchmark/Data/Parser.hs+5 benchmark/Streamly/Benchmark/Data/Parser/ParserD.hs+5 core/src/Streamly/Internal/Data/Parser.hs+6−2
benchmark/Streamly/Benchmark/Data/Parser.hs+5
@@ -157,6 +157,10 @@ takeFramedByEsc_ _ = Stream.parse parser
157157eqBy :: Int -> Stream IO Int -> IO ()
158158eqBy len = Stream.parse (PR.eqBy (==) [1 .. len])
159159
160+{-# INLINE matchBy #-}
161+matchBy :: Int -> Stream IO Int -> IO ()
162+matchBy len = Stream.parse (PR.matchBy (==) (Stream.enumerateFromTo 1 len))
163+
160164{-# INLINE takeWhile #-}
161165takeWhile :: MonadThrow m => Int -> Stream m Int -> m ()
162166takeWhile value = Stream.parse (PR.takeWhile (<= value) Fold.drain)
@@ -478,6 +482,7 @@ o_1_space_serial value =
478482 , benchIOSink value "parseMany (groupBy (<))" (parseManyGroupBy (<))
479483 , benchIOSink value "parseMany (groupBy (==))" (parseManyGroupBy (==))
480484 , benchIOSink value "eqBy" (eqBy value)
485+ , benchIOSink value "matchBy" (matchBy value)
481486 ]
482487
483488o_1_space_filesystem :: BenchEnv -> [Benchmark]
benchmark/Streamly/Benchmark/Data/Parser/ParserD.hs+5
@@ -70,6 +70,10 @@ benchIOSink value name f =
7070eqBy :: Int -> Stream IO Int -> IO ()
7171eqBy len = Stream.parseD (PR.eqBy (==) [1 .. len])
7272
73+{-# INLINE matchBy #-}
74+matchBy :: Int -> Stream IO Int -> IO ()
75+matchBy len = Stream.parseD (PR.matchBy (==) (D.enumerateFromToIntegral 1 len))
76+
7377{-# INLINE drainWhile #-}
7478drainWhile :: MonadThrow m => (a -> Bool) -> PR.Parser m a ()
7579drainWhile p = PR.takeWhile p Fold.drain
@@ -356,6 +360,7 @@ o_1_space_serial value =
356360 -}
357361 , benchIOSink value "sequenceParser" sequenceParser
358362 , benchIOSink value "eqBy" (eqBy value)
363+ , benchIOSink value "matchBy" (matchBy value)
359364 ]
360365
361366o_1_space_serial_spanning :: Int -> [Benchmark]
core/src/Streamly/Internal/Data/Parser.hs+6−2
@@ -103,6 +103,7 @@ module Streamly.Internal.Data.Parser
103103 -- Grab a sequence of input elements by inspecting them
104104 -- ** Exact match
105105 , eqBy
106+ , matchBy
106107 , list
107108
108109 -- ** By predicate
@@ -1024,8 +1025,6 @@ groupByRollingEither :: Monad m =>
10241025 (a -> a -> Bool) -> Fold m a b -> Fold m a c -> Parser m a (Either b c)
10251026groupByRollingEither eq f1 = D.toParserK . D.groupByRollingEither eq f1
10261027
1027−-- XXX Use a stream instead of a list so that we can use any container type.
1028−
10291028-- | Match the given sequence of elements using the given comparison function.
10301029--
10311030-- >>> Stream.parse (Parser.eqBy (==) "string") $ Stream.fromList "string"
@@ -1039,6 +1038,11 @@ groupByRollingEither eq f1 = D.toParserK . D.groupByRollingEither eq f1
10391038eqBy :: Monad m => (a -> a -> Bool) -> [a] -> Parser m a ()
10401039eqBy cmp = D.toParserK . D.eqBy cmp
10411040
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+
10421046-- | Match the input sequence with the supplied list and return it if
10431047-- successful.
10441048--