on master help sign in
Change splitWith to serialWith in folds and parsers
df38c4aRanjeet Kumar Ranjan
benchmark/Streamly/Benchmark/Data/Fold.hs+2−2
@@ -94,7 +94,7 @@ many = IP.fold (FL.many FL.drain (FL.take 1 FL.drain))
94splitAllAny :: Monad m => Int -> SerialT m Int -> m (Bool, Bool)
94splitAllAny :: Monad m => Int -> SerialT m Int -> m (Bool, Bool)
95splitAllAny value =
95splitAllAny value =
96 IP.fold
96 IP.fold
97− (FL.splitWith (,)
97+ (FL.serialWith (,)
98 (FL.all (<= (value `div` 2)))
98 (FL.all (<= (value `div` 2)))
99 (FL.any (> value))
99 (FL.any (> value))
100 )
100 )
@@ -276,7 +276,7 @@ o_1_space_serial_composition :: Int -> [Benchmark]
276o_1_space_serial_composition value =
276o_1_space_serial_composition value =
277 [ bgroup
277 [ bgroup
278 "composition"
278 "composition"
279− [ benchIOSink value "splitWith (all, any)" $ splitAllAny value
279+ [ benchIOSink value "serialWith (all, any)" $ splitAllAny value
280 , benchIOSink value "tee (all, any)" $ teeAllAny value
280 , benchIOSink value "tee (all, any)" $ teeAllAny value
281 , benchIOSink value "many drain (take 1)" many
281 , benchIOSink value "many drain (take 1)" many
282 , benchIOSink value "tee (sum, length)" teeSumLength
282 , benchIOSink value "tee (sum, length)" teeSumLength
benchmark/Streamly/Benchmark/Data/Parser.hs+5−5
@@ -166,12 +166,12 @@ splitApAfter value =
166 <* PR.drainWhile (<= value)
166 <* PR.drainWhile (<= value)
167 )
167 )
168
168
169−{-# INLINE splitWith #-}
169+{-# INLINE serialWith #-}
170−splitWith :: MonadCatch m
170+serialWith :: MonadCatch m
171 => Int -> SerialT m Int -> m ((), ())
171 => Int -> SerialT m Int -> m ((), ())
172−splitWith value =
172+serialWith value =
173 IP.parse
173 IP.parse
174− (PR.splitWith (,)
174+ (PR.serialWith (,)
175 (PR.drainWhile (<= (value `div` 2)))
175 (PR.drainWhile (<= (value `div` 2)))
176 (PR.drainWhile (<= value))
176 (PR.drainWhile (<= value))
177 )
177 )
@@ -335,7 +335,7 @@ o_1_space_serial value =
335 , benchIOSink value "splitAp" $ splitAp value
335 , benchIOSink value "splitAp" $ splitAp value
336 , benchIOSink value "splitApBefore" $ splitApBefore value
336 , benchIOSink value "splitApBefore" $ splitApBefore value
337 , benchIOSink value "splitApAfter" $ splitApAfter value
337 , benchIOSink value "splitApAfter" $ splitApAfter value
338− , benchIOSink value "splitWith" $ splitWith value
338+ , benchIOSink value "serialWith" $ serialWith value
339 , benchIOSink value "sliceSepByP" $ sliceSepByP value
339 , benchIOSink value "sliceSepByP" $ sliceSepByP value
340 , benchIOSink value "many" many
340 , benchIOSink value "many" many
341 , benchIOSink value "many (wordBy even)" $ manyWordByEven
341 , benchIOSink value "many (wordBy even)" $ manyWordByEven
benchmark/Streamly/Benchmark/Data/Parser/ParserD.hs+4−4
@@ -127,10 +127,10 @@ manyTill value =
127 pcond = PR.satisfy (== value)
127 pcond = PR.satisfy (== value)
128 in IP.parseD (PR.manyTill FL.length p pcond)
128 in IP.parseD (PR.manyTill FL.length p pcond)
129
129
130−{-# INLINE splitWith #-}
130+{-# INLINE serialWith #-}
131−splitWith :: MonadThrow m
131+serialWith :: MonadThrow m
132 => Int -> SerialT m Int -> m ((), ())
132 => Int -> SerialT m Int -> m ((), ())
133−splitWith value =
133+serialWith value =
134 IP.parseD
134 IP.parseD
135 ((,)
135 ((,)
136 <$> drainWhile (<= (value `div` 2))
136 <$> drainWhile (<= (value `div` 2))
@@ -293,7 +293,7 @@ o_1_space_serial value =
293 , benchIOSink value "groupBy" $ groupBy
293 , benchIOSink value "groupBy" $ groupBy
294 , benchIOSink value "groupByRolling" $ groupByRolling
294 , benchIOSink value "groupByRolling" $ groupByRolling
295 , benchIOSink value "wordBy" $ wordBy value
295 , benchIOSink value "wordBy" $ wordBy value
296− , benchIOSink value "splitWith" $ splitWith value
296+ , benchIOSink value "serialWith" $ serialWith value
297 , benchIOSink value "many" many
297 , benchIOSink value "many" many
298 , benchIOSink value "many (wordBy even)" $ manyWordByEven
298 , benchIOSink value "many (wordBy even)" $ manyWordByEven
299 , benchIOSink value "some" some
299 , benchIOSink value "some" some
src/Streamly/Internal/Data/Fold.hs+2−2
@@ -971,7 +971,7 @@ or = any (== True)
971-- >>> splitAt_ 4 [1,2,3]
971-- >>> splitAt_ 4 [1,2,3]
972-- ([1,2,3],[])
972-- ([1,2,3],[])
973--
973--
974−-- > splitAt n f1 f2 = splitWith (,) (take n f1) f2
974+-- > splitAt n f1 f2 = serialWith (,) (take n f1) f2
975--
975--
976-- /Pre-release/
976-- /Pre-release/
977
977
@@ -982,7 +982,7 @@ splitAt
982 -> Fold m a b
982 -> Fold m a b
983 -> Fold m a c
983 -> Fold m a c
984 -> Fold m a (b, c)
984 -> Fold m a (b, c)
985−splitAt n fld = splitWith (,) (take n fld)
985+splitAt n fld = serialWith (,) (take n fld)
986
986
987------------------------------------------------------------------------------
987------------------------------------------------------------------------------
988-- Element Aware APIs
988-- Element Aware APIs
src/Streamly/Internal/Data/Fold/Types.hs+5−5
@@ -210,7 +210,7 @@ module Streamly.Internal.Data.Fold.Types
210 , longest
210 , longest
211
211
212 -- * Serial Application
212 -- * Serial Application
213− , splitWith
213+ , serialWith
214 , split_
214 , split_
215
215
216 -- * Nested Application
216 -- * Nested Application
@@ -513,13 +513,13 @@ data SeqFoldState sl f sr = SeqFoldL !sl | SeqFoldR !f !sr
513--
513--
514-- Note: This is a folding dual of appending streams using
514-- Note: This is a folding dual of appending streams using
515-- 'Streamly.Prelude.serial', it splits the streams using two folds and zips
515-- '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@
517--
517--
518-- /Pre-release/
518-- /Pre-release/
519--
519--
520−{-# INLINE splitWith #-}
520+{-# INLINE serialWith #-}
521−splitWith :: Monad m => (a -> b -> c) -> Fold m x a -> Fold m x b -> Fold m x c
521+serialWith :: 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) =
522+serialWith func (Fold stepL initialL extractL) (Fold stepR initialR extractR) =
523 Fold step initial extract
523 Fold step initial extract
524
524
525 where
525 where
src/Streamly/Internal/Data/Parser.hs+10−10
@@ -106,7 +106,7 @@ module Streamly.Internal.Data.Parser
106 -- * Binary Combinators
106 -- * Binary Combinators
107
107
108 -- ** Sequential Applicative
108 -- ** Sequential Applicative
109− , splitWith
109+ , serialWith
110 , split_
110 , split_
111
111
112 -- ** Parallel Applicatives
112 -- ** Parallel Applicatives
@@ -708,7 +708,7 @@ eqBy cmp = K.toParserK . D.eqBy cmp
708-- This implementation is strict in the second argument, therefore, the
708-- This implementation is strict in the second argument, therefore, the
709-- following will fail:
709-- following will fail:
710--
710--
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]
712-- *** Exception: Prelude.undefined
712-- *** Exception: Prelude.undefined
713-- ...
713-- ...
714--
714--
@@ -717,31 +717,31 @@ eqBy cmp = K.toParserK . D.eqBy cmp
717-- operations and can be faster than 'Applicative' instance for small number
717-- operations and can be faster than 'Applicative' instance for small number
718-- (less than 8) of compositions.
718-- (less than 8) of compositions.
719--
719--
720−-- Many combinators can be expressed using @splitWith@ and other parser
720+-- Many combinators can be expressed using @serialWith@ and other parser
721-- primitives. Some common idioms are described below,
721-- primitives. Some common idioms are described below,
722--
722--
723-- @
723-- @
724-- span :: (a -> Bool) -> Fold m a b -> Fold m a b -> Parser m a b
724-- 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)
726-- @
726-- @
727--
727--
728-- @
728-- @
729-- spanBy :: (a -> a -> Bool) -> Fold m a b -> Fold m a b -> Parser m a b
729-- 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)
731-- @
731-- @
732--
732--
733-- @
733-- @
734-- spanByRolling :: (a -> a -> Bool) -> Fold m a b -> Fold m a b -> Parser m a b
734-- 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)
736-- @
736-- @
737--
737--
738-- /Pre-release/
738-- /Pre-release/
739--
739--
740−{-# INLINE splitWith #-}
740+{-# INLINE serialWith #-}
741−splitWith :: MonadCatch m
741+serialWith :: MonadCatch m
742 => (a -> b -> c) -> Parser m x a -> Parser m x b -> Parser m x c
742 => (a -> b -> c) -> Parser m x a -> Parser m x b -> Parser m x c
743−splitWith f p1 p2 =
743+serialWith f p1 p2 =
744− K.toParserK $ D.splitWith f (K.fromParserK p1) (K.fromParserK p2)
744+ K.toParserK $ D.serialWith f (K.fromParserK p1) (K.fromParserK p2)
745
745
746-- | Sequential parser application ignoring the output of the first parser.
746-- | Sequential parser application ignoring the output of the first parser.
747-- Apply two parsers sequentially to an input stream. The input is provided to
747-- 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
79 -- * Binary Combinators
79 -- * Binary Combinators
80
80
81 -- ** Sequential Applicative
81 -- ** Sequential Applicative
82− , splitWith
82+ , serialWith
83 , split_
83 , split_
84
84
85 -- ** Parallel Applicatives
85 -- ** Parallel Applicatives
src/Streamly/Internal/Data/Parser/ParserD/Types.hs+9−9
@@ -118,7 +118,7 @@ module Streamly.Internal.Data.Parser.ParserD.Types
118
118
119 , yield
119 , yield
120 , yieldM
120 , yieldM
121− , splitWith
121+ , serialWith
122 , split_
122 , split_
123
123
124 , die
124 , die
@@ -325,9 +325,9 @@ yieldM b = Parser undefined (IDone <$> b) undefined
325{-# ANN type SeqParseState Fuse #-}
325{-# ANN type SeqParseState Fuse #-}
326data SeqParseState sl f sr = SeqParseL sl | SeqParseR f sr
326data SeqParseState sl f sr = SeqParseL sl | SeqParseR f sr
327
327
328−-- | See 'Streamly.Internal.Data.Parser.splitWith'.
328+-- | See 'Streamly.Internal.Data.Parser.serialWith'.
329--
329--
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
331-- has quadratic time complexity, because each composition adds a new branch
331-- has quadratic time complexity, because each composition adds a new branch
332-- that each subsequent parse's input element has to go through, therefore, it
332-- that each subsequent parse's input element has to go through, therefore, it
333-- cannot scale to a large number of compositions. After around 100
333-- 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
336--
336--
337-- /Pre-release/
337-- /Pre-release/
338--
338--
339−{-# INLINE splitWith #-}
339+{-# INLINE serialWith #-}
340−splitWith :: MonadThrow m
340+serialWith :: MonadThrow m
341 => (a -> b -> c) -> Parser m x a -> Parser m x b -> Parser m x c
341 => (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)
343 (Parser stepR initialR extractR) =
343 (Parser stepR initialR extractR) =
344 Parser step initial extract
344 Parser step initial extract
345
345
@@ -457,7 +457,7 @@ noErrorUnsafeSplitWith func (Parser stepL initialL extractL)
457{-# ANN type SeqAState Fuse #-}
457{-# ANN type SeqAState Fuse #-}
458data SeqAState sl sr = SeqAL sl | SeqAR sr
458data SeqAState sl sr = SeqAL sl | SeqAR sr
459
459
460−-- This turns out to be slightly faster than splitWith
460+-- This turns out to be slightly faster than serialWith
461-- | See 'Streamly.Internal.Data.Parser.split_'.
461-- | See 'Streamly.Internal.Data.Parser.split_'.
462--
462--
463-- /Pre-release/
463-- /Pre-release/
@@ -513,13 +513,13 @@ split_ (Parser stepL initialL extractL) (Parser stepR initialR extractR) =
513 IDone rR -> return rR
513 IDone rR -> return rR
514 IError err -> throwM $ ParseError err
514 IError err -> throwM $ ParseError err
515
515
516−-- | 'Applicative' form of 'splitWith'.
516+-- | 'Applicative' form of 'serialWith'.
517instance MonadThrow m => Applicative (Parser m a) where
517instance MonadThrow m => Applicative (Parser m a) where
518 {-# INLINE pure #-}
518 {-# INLINE pure #-}
519 pure = yield
519 pure = yield
520
520
521 {-# INLINE (<*>) #-}
521 {-# INLINE (<*>) #-}
522− (<*>) = splitWith id
522+ (<*>) = serialWith id
523
523
524 {-# INLINE (*>) #-}
524 {-# INLINE (*>) #-}
525 (*>) = split_
525 (*>) = 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)
293yieldM :: Monad m => m b -> Parser m a b
293yieldM :: Monad m => m b -> Parser m a b
294yieldM eff = MkParser $ \lo st yieldk -> eff >>= \b -> yieldk st (Done lo b)
294yieldM eff = MkParser $ \lo st yieldk -> eff >>= \b -> yieldk st (Done lo b)
295
295
296−-- | 'Applicative' form of 'Streamly.Internal.Data.Parser.splitWith'. Note that
296+-- | 'Applicative' form of 'Streamly.Internal.Data.Parser.serialWith'. Note that
297−-- this operation does not fuse, use 'Streamly.Internal.Data.Parser.splitWith'
297+-- this operation does not fuse, use 'Streamly.Internal.Data.Parser.serialWith'
298-- when fusion is important.
298-- when fusion is important.
299--
299--
300instance Monad m => Applicative (Parser m a) where
300instance Monad m => Applicative (Parser m a) where
test/Streamly/Test/Data/Parser.hs+4−4
@@ -408,7 +408,7 @@ wordBy =
408-- splitWithPass :: Property
408-- splitWithPass :: Property
409-- splitWithPass =
409-- splitWithPass =
410-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
410-- 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
412-- Right (result_first, result_second) -> case ls of
412-- Right (result_first, result_second) -> case ls of
413-- 0 : 1 : _ -> (result_first == 0) && (result_second == 1)
413-- 0 : 1 : _ -> (result_first == 0) && (result_second == 1)
414-- _ -> False
414-- _ -> False
@@ -418,19 +418,19 @@ wordBy =
418
418
419-- splitWithFailLeft :: Property
419-- splitWithFailLeft :: Property
420-- splitWithFailLeft =
420-- 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
422-- Right _ -> False
422-- Right _ -> False
423-- Left _ -> True)
423-- Left _ -> True)
424
424
425-- splitWithFailRight :: Property
425-- splitWithFailRight :: Property
426-- splitWithFailRight =
426-- 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
428-- Right _ -> False
428-- Right _ -> False
429-- Left _ -> True)
429-- Left _ -> True)
430
430
431-- splitWithFailBoth :: Property
431-- splitWithFailBoth :: Property
432-- splitWithFailBoth =
432-- 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
434-- Right _ -> False
434-- Right _ -> False
435-- Left _ -> True)
435-- Left _ -> True)
436
436
test/Streamly/Test/Data/Parser/ParserD.hs+7−7
@@ -383,10 +383,10 @@ wordBy =
383 in if wrds == [] then [""] else wrds
383 in if wrds == [] then [""] else wrds
384
384
385
385
386−splitWith :: Property
386+serialWith :: Property
387−splitWith =
387+serialWith =
388 forAll (listOf (chooseInt (0, 1))) $ \ls ->
388 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
390 Right (result_first, result_second) -> case ls of
390 Right (result_first, result_second) -> case ls of
391 0 : 1 : _ -> (result_first == 0) && (result_second == 1)
391 0 : 1 : _ -> (result_first == 0) && (result_second == 1)
392 _ -> False
392 _ -> False
@@ -396,19 +396,19 @@ splitWith =
396
396
397splitWithFailLeft :: Property
397splitWithFailLeft :: Property
398splitWithFailLeft =
398splitWithFailLeft =
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
400 Right _ -> False
400 Right _ -> False
401 Left _ -> True)
401 Left _ -> True)
402
402
403splitWithFailRight :: Property
403splitWithFailRight :: Property
404splitWithFailRight =
404splitWithFailRight =
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
406 Right _ -> False
406 Right _ -> False
407 Left _ -> True)
407 Left _ -> True)
408
408
409splitWithFailBoth :: Property
409splitWithFailBoth :: Property
410splitWithFailBoth =
410splitWithFailBoth =
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
412 Right _ -> False
412 Right _ -> False
413 Left _ -> True)
413 Left _ -> True)
414
414
@@ -749,7 +749,7 @@ main =
749 prop "groupByRolling" groupByRolling
749 prop "groupByRolling" groupByRolling
750 prop "P.sliceSepByMax = Prelude.take n (Prelude.takeWhile (not . predicate)" sliceSepByMax
750 prop "P.sliceSepByMax = Prelude.take n (Prelude.takeWhile (not . predicate)" sliceSepByMax
751 prop "many (P.wordBy ' ') = words'" wordBy
751 prop "many (P.wordBy ' ') = words'" wordBy
752− prop "parse 0, then 1, else fail" splitWith
752+ prop "parse 0, then 1, else fail" serialWith
753 prop "fail due to die as left parser" splitWithFailLeft
753 prop "fail due to die as left parser" splitWithFailLeft
754 prop "fail due to die as right parser" splitWithFailRight
754 prop "fail due to die as right parser" splitWithFailRight
755 prop "fail due to die as both parsers" splitWithFailBoth
755 prop "fail due to die as both parsers" splitWithFailBoth