on master help sign in
Change splitWith to serialWith in folds and parsers
commitdf38c4a8a44c00ba7c5e3b43b8058694d4457c13 authorRanjeet Kumar Ranjan <ranjeetr@composewell.com> dateThu 4 Mar 2021, 17:58 (6 years ago) parent0414fe8
11 files changed+51−51
benchmark/Streamly/Benchmark/Data/Fold.hs+2−2 benchmark/Streamly/Benchmark/Data/Parser.hs+5−5 benchmark/Streamly/Benchmark/Data/Parser/ParserD.hs+4−4 src/Streamly/Internal/Data/Fold.hs+2−2 src/Streamly/Internal/Data/Fold/Types.hs+5−5 src/Streamly/Internal/Data/Parser.hs+10−10 src/Streamly/Internal/Data/Parser/ParserD.hs+1−1 src/Streamly/Internal/Data/Parser/ParserD/Types.hs+9−9 src/Streamly/Internal/Data/Parser/ParserK/Types.hs+2−2 test/Streamly/Test/Data/Parser.hs+4−4 test/Streamly/Test/Data/Parser/ParserD.hs+7−7
benchmark/Streamly/Benchmark/Data/Fold.hs+2−2
@@ -94,7 +94,7 @@ many = IP.fold (FL.many FL.drain (FL.take 1 FL.drain))
9494splitAllAny :: Monad m => Int -> SerialT m Int -> m (Bool, Bool)
9595splitAllAny value =
9696 IP.fold
97− (FL.splitWith (,)
97+ (FL.serialWith (,)
9898 (FL.all (<= (value `div` 2)))
9999 (FL.any (> value))
100100 )
@@ -276,7 +276,7 @@ o_1_space_serial_composition :: Int -> [Benchmark]
276276o_1_space_serial_composition value =
277277 [ bgroup
278278 "composition"
279− [ benchIOSink value "splitWith (all, any)" $ splitAllAny value
279+ [ benchIOSink value "serialWith (all, any)" $ splitAllAny value
280280 , benchIOSink value "tee (all, any)" $ teeAllAny value
281281 , benchIOSink value "many drain (take 1)" many
282282 , benchIOSink value "tee (sum, length)" teeSumLength
benchmark/Streamly/Benchmark/Data/Parser.hs+5−5
@@ -166,12 +166,12 @@ splitApAfter value =
166166 <* PR.drainWhile (<= value)
167167 )
168168
169−{-# INLINE splitWith #-}
170−splitWith :: MonadCatch m
169+{-# INLINE serialWith #-}
170+serialWith :: MonadCatch m
171171 => Int -> SerialT m Int -> m ((), ())
172−splitWith value =
172+serialWith value =
173173 IP.parse
174− (PR.splitWith (,)
174+ (PR.serialWith (,)
175175 (PR.drainWhile (<= (value `div` 2)))
176176 (PR.drainWhile (<= value))
177177 )
@@ -335,7 +335,7 @@ o_1_space_serial value =
335335 , benchIOSink value "splitAp" $ splitAp value
336336 , benchIOSink value "splitApBefore" $ splitApBefore value
337337 , benchIOSink value "splitApAfter" $ splitApAfter value
338− , benchIOSink value "splitWith" $ splitWith value
338+ , benchIOSink value "serialWith" $ serialWith value
339339 , benchIOSink value "sliceSepByP" $ sliceSepByP value
340340 , benchIOSink value "many" many
341341 , benchIOSink value "many (wordBy even)" $ manyWordByEven
benchmark/Streamly/Benchmark/Data/Parser/ParserD.hs+4−4
@@ -127,10 +127,10 @@ manyTill value =
127127 pcond = PR.satisfy (== value)
128128 in IP.parseD (PR.manyTill FL.length p pcond)
129129
130−{-# INLINE splitWith #-}
131−splitWith :: MonadThrow m
130+{-# INLINE serialWith #-}
131+serialWith :: MonadThrow m
132132 => Int -> SerialT m Int -> m ((), ())
133−splitWith value =
133+serialWith value =
134134 IP.parseD
135135 ((,)
136136 <$> drainWhile (<= (value `div` 2))
@@ -293,7 +293,7 @@ o_1_space_serial value =
293293 , benchIOSink value "groupBy" $ groupBy
294294 , benchIOSink value "groupByRolling" $ groupByRolling
295295 , benchIOSink value "wordBy" $ wordBy value
296− , benchIOSink value "splitWith" $ splitWith value
296+ , benchIOSink value "serialWith" $ serialWith value
297297 , benchIOSink value "many" many
298298 , benchIOSink value "many (wordBy even)" $ manyWordByEven
299299 , benchIOSink value "some" some
src/Streamly/Internal/Data/Fold.hs+2−2
@@ -971,7 +971,7 @@ or = any (== True)
971971-- >>> splitAt_ 4 [1,2,3]
972972-- ([1,2,3],[])
973973--
974−-- > splitAt n f1 f2 = splitWith (,) (take n f1) f2
974+-- > splitAt n f1 f2 = serialWith (,) (take n f1) f2
975975--
976976-- /Pre-release/
977977
@@ -982,7 +982,7 @@ splitAt
982982 -> Fold m a b
983983 -> Fold m a c
984984 -> Fold m a (b, c)
985−splitAt n fld = splitWith (,) (take n fld)
985+splitAt n fld = serialWith (,) (take n fld)
986986
987987------------------------------------------------------------------------------
988988-- Element Aware APIs
src/Streamly/Internal/Data/Fold/Types.hs+5−5
@@ -210,7 +210,7 @@ module Streamly.Internal.Data.Fold.Types
210210 , longest
211211
212212 -- * Serial Application
213− , splitWith
213+ , serialWith
214214 , split_
215215
216216 -- * Nested Application
@@ -513,13 +513,13 @@ data SeqFoldState sl f sr = SeqFoldL !sl | SeqFoldR !f !sr
513513--
514514-- Note: This is a folding dual of appending streams using
515515-- 'Streamly.Prelude.serial', it splits the streams using two folds and zips
516−-- the results. This has the same caveats as ParseD's @splitWith@
516+-- the results. This has the same caveats as ParseD's @serialWith@
517517--
518518-- /Pre-release/
519519--
520−{-# INLINE splitWith #-}
521−splitWith :: Monad m => (a -> b -> c) -> Fold m x a -> Fold m x b -> Fold m x c
522−splitWith func (Fold stepL initialL extractL) (Fold stepR initialR extractR) =
520+{-# INLINE serialWith #-}
521+serialWith :: Monad m => (a -> b -> c) -> Fold m x a -> Fold m x b -> Fold m x c
522+serialWith func (Fold stepL initialL extractL) (Fold stepR initialR extractR) =
523523 Fold step initial extract
524524
525525 where
src/Streamly/Internal/Data/Parser.hs+10−10
@@ -106,7 +106,7 @@ module Streamly.Internal.Data.Parser
106106 -- * Binary Combinators
107107
108108 -- ** Sequential Applicative
109− , splitWith
109+ , serialWith
110110 , split_
111111
112112 -- ** Parallel Applicatives
@@ -708,7 +708,7 @@ eqBy cmp = K.toParserK . D.eqBy cmp
708708-- This implementation is strict in the second argument, therefore, the
709709-- following will fail:
710710--
711−-- >>> Stream.parse (Parser.splitWith const (Parser.satisfy (> 0)) undefined) $ Stream.fromList [1]
711+-- >>> Stream.parse (Parser.serialWith const (Parser.satisfy (> 0)) undefined) $ Stream.fromList [1]
712712-- *** Exception: Prelude.undefined
713713-- ...
714714--
@@ -717,31 +717,31 @@ eqBy cmp = K.toParserK . D.eqBy cmp
717717-- operations and can be faster than 'Applicative' instance for small number
718718-- (less than 8) of compositions.
719719--
720−-- Many combinators can be expressed using @splitWith@ and other parser
720+-- Many combinators can be expressed using @serialWith@ and other parser
721721-- primitives. Some common idioms are described below,
722722--
723723-- @
724724-- span :: (a -> Bool) -> Fold m a b -> Fold m a b -> Parser m a b
725−-- span pred f1 f2 = splitWith (,) ('takeWhile' pred f1) ('fromFold' f2)
725+-- span pred f1 f2 = serialWith (,) ('takeWhile' pred f1) ('fromFold' f2)
726726-- @
727727--
728728-- @
729729-- spanBy :: (a -> a -> Bool) -> Fold m a b -> Fold m a b -> Parser m a b
730−-- spanBy eq f1 f2 = splitWith (,) ('groupBy' eq f1) ('fromFold' f2)
730+-- spanBy eq f1 f2 = serialWith (,) ('groupBy' eq f1) ('fromFold' f2)
731731-- @
732732--
733733-- @
734734-- spanByRolling :: (a -> a -> Bool) -> Fold m a b -> Fold m a b -> Parser m a b
735−-- spanByRolling eq f1 f2 = splitWith (,) ('groupByRolling' eq f1) ('fromFold' f2)
735+-- spanByRolling eq f1 f2 = serialWith (,) ('groupByRolling' eq f1) ('fromFold' f2)
736736-- @
737737--
738738-- /Pre-release/
739739--
740−{-# INLINE splitWith #-}
741−splitWith :: MonadCatch m
740+{-# INLINE serialWith #-}
741+serialWith :: MonadCatch m
742742 => (a -> b -> c) -> Parser m x a -> Parser m x b -> Parser m x c
743−splitWith f p1 p2 =
744− K.toParserK $ D.splitWith f (K.fromParserK p1) (K.fromParserK p2)
743+serialWith f p1 p2 =
744+ K.toParserK $ D.serialWith f (K.fromParserK p1) (K.fromParserK p2)
745745
746746-- | Sequential parser application ignoring the output of the first parser.
747747-- Apply two parsers sequentially to an input stream. The input is provided to
src/Streamly/Internal/Data/Parser/ParserD.hs+1−1
@@ -79,7 +79,7 @@ module Streamly.Internal.Data.Parser.ParserD
7979 -- * Binary Combinators
8080
8181 -- ** Sequential Applicative
82− , splitWith
82+ , serialWith
8383 , split_
8484
8585 -- ** Parallel Applicatives
src/Streamly/Internal/Data/Parser/ParserD/Types.hs+9−9
@@ -118,7 +118,7 @@ module Streamly.Internal.Data.Parser.ParserD.Types
118118
119119 , yield
120120 , yieldM
121− , splitWith
121+ , serialWith
122122 , split_
123123
124124 , die
@@ -325,9 +325,9 @@ yieldM b = Parser undefined (IDone <$> b) undefined
325325{-# ANN type SeqParseState Fuse #-}
326326data SeqParseState sl f sr = SeqParseL sl | SeqParseR f sr
327327
328−-- | See 'Streamly.Internal.Data.Parser.splitWith'.
328+-- | See 'Streamly.Internal.Data.Parser.serialWith'.
329329--
330−-- Note: this implementation of splitWith is fast because of stream fusion but
330+-- Note: this implementation of serialWith is fast because of stream fusion but
331331-- has quadratic time complexity, because each composition adds a new branch
332332-- that each subsequent parse's input element has to go through, therefore, it
333333-- cannot scale to a large number of compositions. After around 100
@@ -336,10 +336,10 @@ data SeqParseState sl f sr = SeqParseL sl | SeqParseR f sr
336336--
337337-- /Pre-release/
338338--
339−{-# INLINE splitWith #-}
340−splitWith :: MonadThrow m
339+{-# INLINE serialWith #-}
340+serialWith :: MonadThrow m
341341 => (a -> b -> c) -> Parser m x a -> Parser m x b -> Parser m x c
342−splitWith func (Parser stepL initialL extractL)
342+serialWith func (Parser stepL initialL extractL)
343343 (Parser stepR initialR extractR) =
344344 Parser step initial extract
345345
@@ -457,7 +457,7 @@ noErrorUnsafeSplitWith func (Parser stepL initialL extractL)
457457{-# ANN type SeqAState Fuse #-}
458458data SeqAState sl sr = SeqAL sl | SeqAR sr
459459
460−-- This turns out to be slightly faster than splitWith
460+-- This turns out to be slightly faster than serialWith
461461-- | See 'Streamly.Internal.Data.Parser.split_'.
462462--
463463-- /Pre-release/
@@ -513,13 +513,13 @@ split_ (Parser stepL initialL extractL) (Parser stepR initialR extractR) =
513513 IDone rR -> return rR
514514 IError err -> throwM $ ParseError err
515515
516−-- | 'Applicative' form of 'splitWith'.
516+-- | 'Applicative' form of 'serialWith'.
517517instance MonadThrow m => Applicative (Parser m a) where
518518 {-# INLINE pure #-}
519519 pure = yield
520520
521521 {-# INLINE (<*>) #-}
522− (<*>) = splitWith id
522+ (<*>) = serialWith id
523523
524524 {-# INLINE (*>) #-}
525525 (*>) = split_
src/Streamly/Internal/Data/Parser/ParserK/Types.hs+2−2
@@ -293,8 +293,8 @@ yield b = MkParser $ \lo st yieldk -> yieldk st (Done lo b)
293293yieldM :: Monad m => m b -> Parser m a b
294294yieldM eff = MkParser $ \lo st yieldk -> eff >>= \b -> yieldk st (Done lo b)
295295
296−-- | 'Applicative' form of 'Streamly.Internal.Data.Parser.splitWith'. Note that
297−-- this operation does not fuse, use 'Streamly.Internal.Data.Parser.splitWith'
296+-- | 'Applicative' form of 'Streamly.Internal.Data.Parser.serialWith'. Note that
297+-- this operation does not fuse, use 'Streamly.Internal.Data.Parser.serialWith'
298298-- when fusion is important.
299299--
300300instance Monad m => Applicative (Parser m a) where
test/Streamly/Test/Data/Parser.hs+4−4
@@ -408,7 +408,7 @@ wordBy =
408408-- splitWithPass :: Property
409409-- splitWithPass =
410410-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
411−-- case S.parse (P.splitWith (,) (P.satisfy (== 0)) (P.satisfy (== 1))) (S.fromList ls) of
411+-- case S.parse (P.serialWith (,) (P.satisfy (== 0)) (P.satisfy (== 1))) (S.fromList ls) of
412412-- Right (result_first, result_second) -> case ls of
413413-- 0 : 1 : _ -> (result_first == 0) && (result_second == 1)
414414-- _ -> False
@@ -418,19 +418,19 @@ wordBy =
418418
419419-- splitWithFailLeft :: Property
420420-- splitWithFailLeft =
421−-- property (case S.parse (P.splitWith (,) (P.die "die") (P.yield (1 :: Int))) (S.fromList [1 :: Int]) of
421+-- property (case S.parse (P.serialWith (,) (P.die "die") (P.yield (1 :: Int))) (S.fromList [1 :: Int]) of
422422-- Right _ -> False
423423-- Left _ -> True)
424424
425425-- splitWithFailRight :: Property
426426-- splitWithFailRight =
427−-- property (case S.parse (P.splitWith (,) (P.yield (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
427+-- property (case S.parse (P.serialWith (,) (P.yield (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
428428-- Right _ -> False
429429-- Left _ -> True)
430430
431431-- splitWithFailBoth :: Property
432432-- splitWithFailBoth =
433−-- property (case S.parse (P.splitWith (,) (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
433+-- property (case S.parse (P.serialWith (,) (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
434434-- Right _ -> False
435435-- Left _ -> True)
436436
test/Streamly/Test/Data/Parser/ParserD.hs+7−7
@@ -383,10 +383,10 @@ wordBy =
383383 in if wrds == [] then [""] else wrds
384384
385385
386−splitWith :: Property
387−splitWith =
386+serialWith :: Property
387+serialWith =
388388 forAll (listOf (chooseInt (0, 1))) $ \ls ->
389− case S.parseD (P.splitWith (,) (P.satisfy (== 0)) (P.satisfy (== 1))) (S.fromList ls) of
389+ case S.parseD (P.serialWith (,) (P.satisfy (== 0)) (P.satisfy (== 1))) (S.fromList ls) of
390390 Right (result_first, result_second) -> case ls of
391391 0 : 1 : _ -> (result_first == 0) && (result_second == 1)
392392 _ -> False
@@ -396,19 +396,19 @@ splitWith =
396396
397397splitWithFailLeft :: Property
398398splitWithFailLeft =
399− property (case S.parseD (P.splitWith (,) (P.die "die") (P.yield (1 :: Int))) (S.fromList [1 :: Int]) of
399+ property (case S.parseD (P.serialWith (,) (P.die "die") (P.yield (1 :: Int))) (S.fromList [1 :: Int]) of
400400 Right _ -> False
401401 Left _ -> True)
402402
403403splitWithFailRight :: Property
404404splitWithFailRight =
405− property (case S.parseD (P.splitWith (,) (P.yield (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
405+ property (case S.parseD (P.serialWith (,) (P.yield (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
406406 Right _ -> False
407407 Left _ -> True)
408408
409409splitWithFailBoth :: Property
410410splitWithFailBoth =
411− property (case S.parseD (P.splitWith (,) (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
411+ property (case S.parseD (P.serialWith (,) (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
412412 Right _ -> False
413413 Left _ -> True)
414414
@@ -749,7 +749,7 @@ main =
749749 prop "groupByRolling" groupByRolling
750750 prop "P.sliceSepByMax = Prelude.take n (Prelude.takeWhile (not . predicate)" sliceSepByMax
751751 prop "many (P.wordBy ' ') = words'" wordBy
752− prop "parse 0, then 1, else fail" splitWith
752+ prop "parse 0, then 1, else fail" serialWith
753753 prop "fail due to die as left parser" splitWithFailLeft
754754 prop "fail due to die as right parser" splitWithFailRight
755755 prop "fail due to die as both parsers" splitWithFailBoth