on master help sign in
rename splitParse/parseMany, concatParse/parseIterate
commit633c55eb31784017954f1aa14d7c44a2a0721772 authorHarendra Kumar <harendra@composewell.com> dateSat 25 Apr 2020, 03:07 (6 years ago) parent1889b4e
5 files changed+56−63
benchmark/FileIO.hs+6−6 benchmark/Streamly/Benchmark/Data/Parser.hs+10−10 benchmark/Streamly/Benchmark/FileIO/Stream.hs+11−11 src/Streamly/Internal/Data/Stream/StreamD.hs+9−9 src/Streamly/Internal/Prelude.hs+20−27
benchmark/FileIO.hs+6−6
@@ -212,12 +212,12 @@ main = do
212212 , mkBench "sumChunksOf 1" href $ do
213213 Handles inh _ <- readIORef href
214214 BFS.chunksOfSum 1 inh
215− , mkBench "sumChunksOf (single chunk) (splitParse)" href $ do
215+ , mkBench "sumChunksOf (single chunk) (parseMany)" href $ do
216216 Handles inh _ <- readIORef href
217− BFS.splitParseChunksOfSum fileSize inh
218− , mkBench "sumChunksOf 1 (splitParse)" href $ do
217+ BFS.parseManyChunksOfSum fileSize inh
218+ , mkBench "sumChunksOf 1 (parseMany)" href $ do
219219 Handles inh _ <- readIORef href
220− BFS.splitParseChunksOfSum 1 inh
220+ BFS.parseManyChunksOfSum 1 inh
221221
222222 , mkBench "arraysOf 1" href $ do
223223 Handles inh _ <- readIORef href
@@ -268,9 +268,9 @@ main = do
268268 , mkBench "splitOnSuffix \\n (line count)" href $ do
269269 Handles inh _ <- readIORef href
270270 BFS.splitOnSuffix inh
271− , mkBench "splitOn \\n (line count) (splitParse)" href $ do
271+ , mkBench "splitOn \\n (line count) (parseMany)" href $ do
272272 Handles inh _ <- readIORef href
273− BFS.splitParseSepBy inh
273+ BFS.parseManySepBy inh
274274 , mkBench "wordsBy isSpace (word count)" href $ do
275275 Handles inh _ <- readIORef href
276276 BFS.wordsBy inh
benchmark/Streamly/Benchmark/Data/Parser.hs+10−10
@@ -177,20 +177,20 @@ choice value =
177177-- Stream transformation
178178-------------------------------------------------------------------------------
179179
180−{-# INLINE splitParse #-}
181−splitParse :: MonadCatch m => SerialT m Int -> m ()
182−splitParse =
180+{-# INLINE parseMany #-}
181+parseMany :: MonadCatch m => SerialT m Int -> m ()
182+parseMany =
183183 S.drain
184184 . S.map getSum
185− . IP.splitParse (PR.take 2 FL.mconcat)
185+ . IP.parseMany (PR.take 2 FL.mconcat)
186186 . S.map Sum
187187
188−{-# INLINE concatParse #-}
189−concatParse :: MonadCatch m => SerialT m Int -> m ()
190−concatParse =
188+{-# INLINE parseIterate #-}
189+parseIterate :: MonadCatch m => SerialT m Int -> m ()
190+parseIterate =
191191 S.drain
192192 . S.map getSum
193− . IP.concatParse (\b -> (PR.take 2 (FL.mconcatTo b))) (Sum 0)
193+ . IP.parseIterate (\b -> (PR.take 2 (FL.mconcatTo b))) (Sum 0)
194194 . S.map Sum
195195
196196-------------------------------------------------------------------------------
@@ -211,8 +211,8 @@ o_1_space_serial_parse value =
211211 , benchIOSink value "teeFst (all,any)" $ teeFstAllAny value
212212 , benchIOSink value "shortest (all,any)" $ shortestAllAny value
213213 , benchIOSink value "longest (all,any)" $ longestAllAny value
214− , benchIOSink value "splitParse" splitParse
215− , benchIOSink value "concatParse" concatParse
214+ , benchIOSink value "parseMany" parseMany
215+ , benchIOSink value "parseIterate" parseIterate
216216 ]
217217
218218o_n_heap_serial_parse :: Int -> [Benchmark]
benchmark/Streamly/Benchmark/FileIO/Stream.hs+11−11
@@ -57,12 +57,12 @@ module Streamly.Benchmark.FileIO.Stream
5757 , decodeUtf8Lax
5858 , copyCodecUtf8Lenient
5959 , chunksOfSum
60− , splitParseChunksOfSum
60+ , parseManyChunksOfSum
6161 , chunksOf
6262 , chunksOfD
6363 , splitOn
6464 , splitOnSuffix
65− , splitParseSepBy
65+ , parseManySepBy
6666 , wordsBy
6767 , splitOnSeq
6868 , splitOnSeqUtf8
@@ -445,10 +445,10 @@ inspect $ 'chunksOfD `hasNoType` ''AT.FlattenState
445445inspect $ 'chunksOfD `hasNoType` ''D.ConcatMapUState
446446#endif
447447
448−{-# INLINE splitParseChunksOfSum #-}
449−splitParseChunksOfSum :: Int -> Handle -> IO Int
450−splitParseChunksOfSum n inh =
451− S.length $ IP.splitParse (PR.take n FL.sum) (S.unfold FH.read inh)
448+{-# INLINE parseManyChunksOfSum #-}
449+parseManyChunksOfSum :: Int -> Handle -> IO Int
450+parseManyChunksOfSum n inh =
451+ S.length $ IP.parseMany (PR.take n FL.sum) (S.unfold FH.read inh)
452452
453453{-# INLINE linesUnlinesCopy #-}
454454linesUnlinesCopy :: Handle -> Handle -> IO ()
@@ -609,11 +609,11 @@ inspect $ 'splitOnSuffix `hasNoType` ''D.ConcatMapUState
609609#endif
610610
611611-- | Split on line feed.
612−{-# INLINE splitParseSepBy #-}
613−splitParseSepBy :: Handle -> IO Int
614−splitParseSepBy inh =
615− (S.length $ IP.splitParse (PR.sliceSepBy (== lf) FL.drain)
616− (S.unfold FH.read inh)) -- >>= print
612+{-# INLINE parseManySepBy #-}
613+parseManySepBy :: Handle -> IO Int
614+parseManySepBy inh =
615+ (S.length $ IP.parseMany (PR.sliceSepBy (== lf) FL.drain)
616+ (S.unfold FH.read inh)) -- >>= print
617617
618618-- | Words by space
619619{-# INLINE wordsBy #-}
src/Streamly/Internal/Data/Stream/StreamD.hs+9−9
@@ -129,8 +129,8 @@ module Streamly.Internal.Data.Stream.StreamD
129129 , runFold
130130
131131 , parselMx'
132− , splitParse
133− , concatParse
132+ , parseMany
133+ , parseIterate
134134
135135 -- ** Specialized Folds
136136 , tap
@@ -955,7 +955,7 @@ foldlS fstep begin (Stream step state) = Stream step' (Left (state, begin))
955955------------------------------------------------------------------------------
956956
957957-- Inlined definition. Without the inline "serially/parser/take" benchmark
958−-- degrades and splitParse does not fuse. Even using "inline" at the callsite
958+-- degrades and parseMany does not fuse. Even using "inline" at the callsite
959959-- does not help.
960960{-# INLINE splitAt #-}
961961splitAt :: Int -> [a] -> ([a],[a])
@@ -1038,13 +1038,13 @@ data ParseChunksState x inpBuf st pst =
10381038 | ParseChunksBuf inpBuf st inpBuf pst
10391039 | ParseChunksYield x (ParseChunksState x inpBuf st pst)
10401040
1041−{-# INLINE_NORMAL splitParse #-}
1042−splitParse
1041+{-# INLINE_NORMAL parseMany #-}
1042+parseMany
10431043 :: MonadThrow m
10441044 => PRD.Parser m a b
10451045 -> Stream m a
10461046 -> Stream m b
1047−splitParse (PRD.Parser pstep initial extract) (Stream step state) =
1047+parseMany (PRD.Parser pstep initial extract) (Stream step state) =
10481048 Stream stepOuter (ParseChunksInit [] state)
10491049
10501050 where
@@ -1130,14 +1130,14 @@ data ConcatParseState x inpBuf st p =
11301130 | ConcatParseBuf inpBuf st inpBuf p
11311131 | ConcatParseYield x (ConcatParseState x inpBuf st p)
11321132
1133−{-# INLINE_NORMAL concatParse #-}
1134−concatParse
1133+{-# INLINE_NORMAL parseIterate #-}
1134+parseIterate
11351135 :: MonadThrow m
11361136 => (b -> PRD.Parser m a b)
11371137 -> b
11381138 -> Stream m a
11391139 -> Stream m b
1140−concatParse func seed (Stream step state) =
1140+parseIterate func seed (Stream step state) =
11411141 Stream stepOuter (ConcatParseInit [] state (func seed))
11421142
11431143 where
src/Streamly/Internal/Prelude.hs+20−27
@@ -269,9 +269,9 @@ module Streamly.Internal.Prelude
269269 , reverse'
270270
271271 -- ** Parsing
272− , splitParse
273− , splitParseTill
274− , concatParse
272+ , parseMany
273+ , parseManyTill
274+ , parseIterate
275275
276276 -- ** Trimming
277277 , take
@@ -3142,13 +3142,8 @@ concatMapTreeYieldLeavesWith combine f = concatMapLoopWith combine f yield
31423142------------------------------------------------------------------------------
31433143
31443144-- Splitting operations that take a predicate and a Fold can be
3145−-- expressed using splitParse. Operations like chunksOf, intervalsOf, split*,
3146−-- can be expressed using splitParse when used with an appropriate Parse.
3147−--
3148−-- The name splitParse is supposed to be opposite of concatMap. Alternative
3149−-- names could be "many", "parseMany" or "splitMany"? If we are going to have a
3150−-- separate combinators for Fold and Parser then having fold/parse in the name
3151−-- allows the distinction.
3145+-- expressed using parseMany. Operations like chunksOf, intervalsOf, split*,
3146+-- can be expressed using parseMany when used with an appropriate Parser.
31523147--
31533148-- XXX We need takeGE/takeBetween to implement "some" using "many".
31543149
@@ -3158,24 +3153,24 @@ concatMapTreeYieldLeavesWith combine f = concatMapLoopWith combine f yield
31583153-- This is the streaming equivalent of the 'Streamly.Internal.Data.Parser.many'
31593154-- parse combinator.
31603155--
3161−-- >>> S.toList $ S.splitParse (PR.take 2 $ PR.fromFold FL.sum) $ S.fromList [1..10]
3156+-- >>> S.toList $ S.parseMany (PR.take 2 $ PR.fromFold FL.sum) $ S.fromList [1..10]
31623157-- > [3,7,11,15,19]
31633158--
3164−-- >>> S.toList $ S.splitParse (PR.line FL.toList) $ S.fromList "hello\nworld"
3159+-- >>> S.toList $ S.parseMany (PR.line FL.toList) $ S.fromList "hello\nworld"
31653160-- > ["hello\n","world"]
31663161--
31673162-- /Internal
31683163--
3169−{-# INLINE splitParse #-}
3170−splitParse
3164+{-# INLINE parseMany #-}
3165+parseMany
31713166 :: (IsStream t, MonadThrow m)
31723167 => Parser m a b
31733168 -> t m a
31743169 -> t m b
3175−splitParse p m =
3176− D.fromStreamD $ D.splitParse (PRD.fromParserK p) (D.toStreamD m)
3170+parseMany p m =
3171+ D.fromStreamD $ D.parseMany (PRD.fromParserK p) (D.toStreamD m)
31773172
3178−-- | @splitParseTill collect test stream@ tries the parser @test@ on the input,
3173+-- | @parseManyTill collect test stream@ tries the parser @test@ on the input,
31793174-- if @test@ fails it backtracks and tries @collect@, after @collect@ succeeds
31803175-- @test@ is tried again and so on. The parser stops when @test@ succeeds. The
31813176-- output of @test@ is discarded and the output of @collect@ is emitted in the
@@ -3183,22 +3178,20 @@ splitParse p m =
31833178--
31843179-- /Unimplemented/
31853180--
3186−{-# INLINE splitParseTill #-}
3187−splitParseTill ::
3181+{-# INLINE parseManyTill #-}
3182+parseManyTill ::
31883183 -- (IsStream t, MonadThrow m) =>
31893184 Parser m a b
31903185 -> Parser m a x
31913186 -> t m a
31923187 -> t m b
3193−splitParseTill = undefined
3188+parseManyTill = undefined
31943189
3195−-- Rename to iterateParse?
3196−--
31973190-- | Iterate a parser generating function on a stream. The initial value @b@ is
31983191-- used to generate the first parser, the parser is applied on the stream and
31993192-- the result is used to generate the next parser and so on.
32003193--
3201−-- >>> S.toList $ S.map getSum $ S.concatParse (\b -> PR.take 2 (FL.mconcatTo b)) 0 $ S.map Sum $ S.fromList [1..10]
3194+-- >>> S.toList $ S.map getSum $ S.parseIterate (\b -> PR.take 2 (FL.mconcatTo b)) 0 $ S.map Sum $ S.fromList [1..10]
32023195-- > [3,10,21,36,55,55]
32033196--
32043197-- This is the streaming equivalent of monad like sequenced application of
@@ -3206,15 +3199,15 @@ splitParseTill = undefined
32063199--
32073200-- /Internal/
32083201--
3209−{-# INLINE concatParse #-}
3210−concatParse
3202+{-# INLINE parseIterate #-}
3203+parseIterate
32113204 :: (IsStream t, MonadThrow m)
32123205 => (b -> Parser m a b)
32133206 -> b
32143207 -> t m a
32153208 -> t m b
3216−concatParse f i m = D.fromStreamD $
3217− D.concatParse (\b -> PRD.fromParserK $ f b) i (D.toStreamD m)
3209+parseIterate f i m = D.fromStreamD $
3210+ D.parseIterate (\b -> PRD.fromParserK $ f b) i (D.toStreamD m)
32183211
32193212------------------------------------------------------------------------------
32203213-- Grouping/Splitting