rename splitParse/parseMany, concatParse/parseIterate

This commit is contained in:
Harendra Kumar 2020-04-25 03:07:00 +05:30
parent 1889b4eef6
commit 633c55eb31
5 changed files with 56 additions and 63 deletions

View File

@ -212,12 +212,12 @@ main = do
, mkBench "sumChunksOf 1" href $ do
Handles inh _ <- readIORef href
BFS.chunksOfSum 1 inh
, mkBench "sumChunksOf (single chunk) (splitParse)" href $ do
, mkBench "sumChunksOf (single chunk) (parseMany)" href $ do
Handles inh _ <- readIORef href
BFS.splitParseChunksOfSum fileSize inh
, mkBench "sumChunksOf 1 (splitParse)" href $ do
BFS.parseManyChunksOfSum fileSize inh
, mkBench "sumChunksOf 1 (parseMany)" href $ do
Handles inh _ <- readIORef href
BFS.splitParseChunksOfSum 1 inh
BFS.parseManyChunksOfSum 1 inh
, mkBench "arraysOf 1" href $ do
Handles inh _ <- readIORef href
@ -268,9 +268,9 @@ main = do
, mkBench "splitOnSuffix \\n (line count)" href $ do
Handles inh _ <- readIORef href
BFS.splitOnSuffix inh
, mkBench "splitOn \\n (line count) (splitParse)" href $ do
, mkBench "splitOn \\n (line count) (parseMany)" href $ do
Handles inh _ <- readIORef href
BFS.splitParseSepBy inh
BFS.parseManySepBy inh
, mkBench "wordsBy isSpace (word count)" href $ do
Handles inh _ <- readIORef href
BFS.wordsBy inh

View File

@ -177,20 +177,20 @@ choice value =
-- Stream transformation
-------------------------------------------------------------------------------
{-# INLINE splitParse #-}
splitParse :: MonadCatch m => SerialT m Int -> m ()
splitParse =
{-# INLINE parseMany #-}
parseMany :: MonadCatch m => SerialT m Int -> m ()
parseMany =
S.drain
. S.map getSum
. IP.splitParse (PR.take 2 FL.mconcat)
. IP.parseMany (PR.take 2 FL.mconcat)
. S.map Sum
{-# INLINE concatParse #-}
concatParse :: MonadCatch m => SerialT m Int -> m ()
concatParse =
{-# INLINE parseIterate #-}
parseIterate :: MonadCatch m => SerialT m Int -> m ()
parseIterate =
S.drain
. S.map getSum
. IP.concatParse (\b -> (PR.take 2 (FL.mconcatTo b))) (Sum 0)
. IP.parseIterate (\b -> (PR.take 2 (FL.mconcatTo b))) (Sum 0)
. S.map Sum
-------------------------------------------------------------------------------
@ -211,8 +211,8 @@ o_1_space_serial_parse value =
, benchIOSink value "teeFst (all,any)" $ teeFstAllAny value
, benchIOSink value "shortest (all,any)" $ shortestAllAny value
, benchIOSink value "longest (all,any)" $ longestAllAny value
, benchIOSink value "splitParse" splitParse
, benchIOSink value "concatParse" concatParse
, benchIOSink value "parseMany" parseMany
, benchIOSink value "parseIterate" parseIterate
]
o_n_heap_serial_parse :: Int -> [Benchmark]

View File

@ -57,12 +57,12 @@ module Streamly.Benchmark.FileIO.Stream
, decodeUtf8Lax
, copyCodecUtf8Lenient
, chunksOfSum
, splitParseChunksOfSum
, parseManyChunksOfSum
, chunksOf
, chunksOfD
, splitOn
, splitOnSuffix
, splitParseSepBy
, parseManySepBy
, wordsBy
, splitOnSeq
, splitOnSeqUtf8
@ -445,10 +445,10 @@ inspect $ 'chunksOfD `hasNoType` ''AT.FlattenState
inspect $ 'chunksOfD `hasNoType` ''D.ConcatMapUState
#endif
{-# INLINE splitParseChunksOfSum #-}
splitParseChunksOfSum :: Int -> Handle -> IO Int
splitParseChunksOfSum n inh =
S.length $ IP.splitParse (PR.take n FL.sum) (S.unfold FH.read inh)
{-# INLINE parseManyChunksOfSum #-}
parseManyChunksOfSum :: Int -> Handle -> IO Int
parseManyChunksOfSum n inh =
S.length $ IP.parseMany (PR.take n FL.sum) (S.unfold FH.read inh)
{-# INLINE linesUnlinesCopy #-}
linesUnlinesCopy :: Handle -> Handle -> IO ()
@ -609,11 +609,11 @@ inspect $ 'splitOnSuffix `hasNoType` ''D.ConcatMapUState
#endif
-- | Split on line feed.
{-# INLINE splitParseSepBy #-}
splitParseSepBy :: Handle -> IO Int
splitParseSepBy inh =
(S.length $ IP.splitParse (PR.sliceSepBy (== lf) FL.drain)
(S.unfold FH.read inh)) -- >>= print
{-# INLINE parseManySepBy #-}
parseManySepBy :: Handle -> IO Int
parseManySepBy inh =
(S.length $ IP.parseMany (PR.sliceSepBy (== lf) FL.drain)
(S.unfold FH.read inh)) -- >>= print
-- | Words by space
{-# INLINE wordsBy #-}

View File

@ -129,8 +129,8 @@ module Streamly.Internal.Data.Stream.StreamD
, runFold
, parselMx'
, splitParse
, concatParse
, parseMany
, parseIterate
-- ** Specialized Folds
, tap
@ -955,7 +955,7 @@ foldlS fstep begin (Stream step state) = Stream step' (Left (state, begin))
------------------------------------------------------------------------------
-- Inlined definition. Without the inline "serially/parser/take" benchmark
-- degrades and splitParse does not fuse. Even using "inline" at the callsite
-- degrades and parseMany does not fuse. Even using "inline" at the callsite
-- does not help.
{-# INLINE splitAt #-}
splitAt :: Int -> [a] -> ([a],[a])
@ -1038,13 +1038,13 @@ data ParseChunksState x inpBuf st pst =
| ParseChunksBuf inpBuf st inpBuf pst
| ParseChunksYield x (ParseChunksState x inpBuf st pst)
{-# INLINE_NORMAL splitParse #-}
splitParse
{-# INLINE_NORMAL parseMany #-}
parseMany
:: MonadThrow m
=> PRD.Parser m a b
-> Stream m a
-> Stream m b
splitParse (PRD.Parser pstep initial extract) (Stream step state) =
parseMany (PRD.Parser pstep initial extract) (Stream step state) =
Stream stepOuter (ParseChunksInit [] state)
where
@ -1130,14 +1130,14 @@ data ConcatParseState x inpBuf st p =
| ConcatParseBuf inpBuf st inpBuf p
| ConcatParseYield x (ConcatParseState x inpBuf st p)
{-# INLINE_NORMAL concatParse #-}
concatParse
{-# INLINE_NORMAL parseIterate #-}
parseIterate
:: MonadThrow m
=> (b -> PRD.Parser m a b)
-> b
-> Stream m a
-> Stream m b
concatParse func seed (Stream step state) =
parseIterate func seed (Stream step state) =
Stream stepOuter (ConcatParseInit [] state (func seed))
where

View File

@ -269,9 +269,9 @@ module Streamly.Internal.Prelude
, reverse'
-- ** Parsing
, splitParse
, splitParseTill
, concatParse
, parseMany
, parseManyTill
, parseIterate
-- ** Trimming
, take
@ -3142,13 +3142,8 @@ concatMapTreeYieldLeavesWith combine f = concatMapLoopWith combine f yield
------------------------------------------------------------------------------
-- Splitting operations that take a predicate and a Fold can be
-- expressed using splitParse. Operations like chunksOf, intervalsOf, split*,
-- can be expressed using splitParse when used with an appropriate Parse.
--
-- The name splitParse is supposed to be opposite of concatMap. Alternative
-- names could be "many", "parseMany" or "splitMany"? If we are going to have a
-- separate combinators for Fold and Parser then having fold/parse in the name
-- allows the distinction.
-- expressed using parseMany. Operations like chunksOf, intervalsOf, split*,
-- can be expressed using parseMany when used with an appropriate Parser.
--
-- XXX We need takeGE/takeBetween to implement "some" using "many".
@ -3158,24 +3153,24 @@ concatMapTreeYieldLeavesWith combine f = concatMapLoopWith combine f yield
-- This is the streaming equivalent of the 'Streamly.Internal.Data.Parser.many'
-- parse combinator.
--
-- >>> S.toList $ S.splitParse (PR.take 2 $ PR.fromFold FL.sum) $ S.fromList [1..10]
-- >>> S.toList $ S.parseMany (PR.take 2 $ PR.fromFold FL.sum) $ S.fromList [1..10]
-- > [3,7,11,15,19]
--
-- >>> S.toList $ S.splitParse (PR.line FL.toList) $ S.fromList "hello\nworld"
-- >>> S.toList $ S.parseMany (PR.line FL.toList) $ S.fromList "hello\nworld"
-- > ["hello\n","world"]
--
-- /Internal
--
{-# INLINE splitParse #-}
splitParse
{-# INLINE parseMany #-}
parseMany
:: (IsStream t, MonadThrow m)
=> Parser m a b
-> t m a
-> t m b
splitParse p m =
D.fromStreamD $ D.splitParse (PRD.fromParserK p) (D.toStreamD m)
parseMany p m =
D.fromStreamD $ D.parseMany (PRD.fromParserK p) (D.toStreamD m)
-- | @splitParseTill collect test stream@ tries the parser @test@ on the input,
-- | @parseManyTill collect test stream@ tries the parser @test@ on the input,
-- if @test@ fails it backtracks and tries @collect@, after @collect@ succeeds
-- @test@ is tried again and so on. The parser stops when @test@ succeeds. The
-- output of @test@ is discarded and the output of @collect@ is emitted in the
@ -3183,22 +3178,20 @@ splitParse p m =
--
-- /Unimplemented/
--
{-# INLINE splitParseTill #-}
splitParseTill ::
{-# INLINE parseManyTill #-}
parseManyTill ::
-- (IsStream t, MonadThrow m) =>
Parser m a b
-> Parser m a x
-> t m a
-> t m b
splitParseTill = undefined
parseManyTill = undefined
-- Rename to iterateParse?
--
-- | Iterate a parser generating function on a stream. The initial value @b@ is
-- used to generate the first parser, the parser is applied on the stream and
-- the result is used to generate the next parser and so on.
--
-- >>> S.toList $ S.map getSum $ S.concatParse (\b -> PR.take 2 (FL.mconcatTo b)) 0 $ S.map Sum $ S.fromList [1..10]
-- >>> S.toList $ S.map getSum $ S.parseIterate (\b -> PR.take 2 (FL.mconcatTo b)) 0 $ S.map Sum $ S.fromList [1..10]
-- > [3,10,21,36,55,55]
--
-- This is the streaming equivalent of monad like sequenced application of
@ -3206,15 +3199,15 @@ splitParseTill = undefined
--
-- /Internal/
--
{-# INLINE concatParse #-}
concatParse
{-# INLINE parseIterate #-}
parseIterate
:: (IsStream t, MonadThrow m)
=> (b -> Parser m a b)
-> b
-> t m a
-> t m b
concatParse f i m = D.fromStreamD $
D.concatParse (\b -> PRD.fromParserK $ f b) i (D.toStreamD m)
parseIterate f i m = D.fromStreamD $
D.parseIterate (\b -> PRD.fromParserK $ f b) i (D.toStreamD m)
------------------------------------------------------------------------------
-- Grouping/Splitting