on master help sign in
Rename serialWith to splitWith
commit0d3c63aeae1d1d5187a481728a1594aad08f71ce authorAdithya Kumar <adithya@composewell.com> dateFri 4 Nov 2022, 14:58 (4 years ago) parent5efab7d
15 files changed+72−65
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 core/src/Streamly/Data/Fold.hs+9−3 core/src/Streamly/Internal/Data/Array/Fold.hs+7−7 core/src/Streamly/Internal/Data/Fold.hs+3−3 core/src/Streamly/Internal/Data/Fold/Type.hs+6−6 core/src/Streamly/Internal/Data/Parser.hs+10−10 core/src/Streamly/Internal/Data/Parser/ParserD.hs+1−1 core/src/Streamly/Internal/Data/Parser/ParserD/Type.hs+10−10 core/src/Streamly/Internal/Data/Parser/ParserK/Type.hs+2−2 docs/User/ProjectRelated/Changelog.md+1 test/Streamly/Test/Data/Fold.hs+1−1 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
@@ -121,7 +121,7 @@ many = Stream.fold (FL.many (FL.take 1 FL.drain) FL.drain)
121121splitAllAny :: Monad m => Int -> Stream m Int -> m (Bool, Bool)
122122splitAllAny value =
123123 Stream.fold
124− (FL.serialWith (,)
124+ (FL.splitWith (,)
125125 (FL.all (<= (value `div` 2)))
126126 (FL.any (> value))
127127 )
@@ -399,7 +399,7 @@ o_1_space_serial_composition value =
399399 , benchIOSink value "scanMaybe even" $ scanMaybe value
400400 , benchIOSink value "scanMaybe even, odd" $ scanMaybe2 value
401401 , benchIOSink value "foldBreak (recursive)" foldBreak
402− , benchIOSink value "serialWith (all, any)" $ splitAllAny value
402+ , benchIOSink value "splitWith (all, any)" $ splitAllAny value
403403 , benchIOSink value "serial_ (all, any)" $ serial_ value
404404 , benchIOSink value "tee (all, any)" $ teeAllAny value
405405 , benchIOSink value "many drain (take 1)" many
benchmark/Streamly/Benchmark/Data/Parser.hs+5−5
@@ -260,12 +260,12 @@ splitApAfter value =
260260 <* PR.dropWhile (<= value)
261261 )
262262
263−{-# INLINE serialWith #-}
264−serialWith :: MonadThrow m
263+{-# INLINE splitWith #-}
264+splitWith :: MonadThrow m
265265 => Int -> Stream m Int -> m ((), ())
266−serialWith value =
266+splitWith value =
267267 Stream.parse
268− (PR.serialWith (,)
268+ (PR.splitWith (,)
269269 (PR.dropWhile (<= (value `div` 2)))
270270 (PR.dropWhile (<= value))
271271 )
@@ -463,7 +463,7 @@ o_1_space_serial value =
463463 , benchIOSink value "splitAp" $ splitAp value
464464 , benchIOSink value "splitApBefore" $ splitApBefore value
465465 , benchIOSink value "splitApAfter" $ splitApAfter value
466− , benchIOSink value "serialWith" $ serialWith value
466+ , benchIOSink value "splitWith" $ splitWith value
467467 , benchIOSink value "takeEndBy_" $ takeEndBy_ value
468468 , benchIOSink value "many" many
469469 , benchIOSink value "many (wordBy even)" $ manyWordByEven
benchmark/Streamly/Benchmark/Data/Parser/ParserD.hs+4−4
@@ -147,10 +147,10 @@ manyTill value =
147147 pcond = PR.satisfy (== value)
148148 in Stream.parseD (PR.manyTill p pcond Fold.length)
149149
150−{-# INLINE serialWith #-}
151−serialWith :: MonadThrow m
150+{-# INLINE splitWith #-}
151+splitWith :: MonadThrow m
152152 => Int -> Stream m Int -> m ((), ())
153−serialWith value =
153+splitWith value =
154154 Stream.parseD
155155 ((,)
156156 <$> drainWhile (<= (value `div` 2))
@@ -348,7 +348,7 @@ o_1_space_serial value =
348348 , benchIOSink value "groupBy" $ groupBy
349349 , benchIOSink value "groupByRolling" $ groupByRolling
350350 , benchIOSink value "wordBy" $ wordBy value
351− , benchIOSink value "serialWith" $ serialWith value
351+ , benchIOSink value "splitWith" $ splitWith value
352352 , benchIOSink value "many" many
353353 , benchIOSink value "many (wordBy even)" $ manyWordByEven
354354 , benchIOSink value "some" some
core/src/Streamly/Data/Fold.hs+9−3
@@ -70,7 +70,7 @@
7070-- first fold consumes the input until it terminates and the second fold
7171-- consumes the rest of the input until it terminates:
7272--
73−-- >>> f = Fold.serialWith (,) (Fold.take 8 Fold.toList) (Fold.takeEndBy (== '\n') Fold.toList)
73+-- >>> f = Fold.splitWith (,) (Fold.take 8 Fold.toList) (Fold.takeEndBy (== '\n') Fold.toList)
7474-- >>> Stream.fold f $ Stream.fromList "header: hello\n"
7575-- ("header: ","hello\n")
7676--
@@ -94,7 +94,7 @@
9494-- We can often use streams or folds to achieve the same goal. However, streams
9595-- are more efficient in composition of producers (e.g.
9696-- 'Data.Stream.append' or 'Data.Stream.mergeBy') whereas folds are
97−-- more efficient in composition of consumers (e.g. 'serialWith', 'partition'
97+-- more efficient in composition of consumers (e.g. 'splitWith', 'partition'
9898-- or 'teeWith').
9999--
100100-- Streams are producers, transformations on streams happen on the output side:
@@ -265,7 +265,7 @@ module Streamly.Data.Fold
265265 , takeEndBy
266266
267267 -- ** Serial Append
268− , serialWith
268+ , splitWith
269269
270270 -- ** Parallel Distribution
271271 -- | For applicative composition using distribution see
@@ -307,6 +307,7 @@ module Streamly.Data.Fold
307307 , mapM
308308 , variance
309309 , stdDev
310+ , serialWith
310311 )
311312where
312313
@@ -320,6 +321,11 @@ import Prelude
320321
321322import Streamly.Internal.Data.Fold
322323
324+{-# DEPRECATED serialWith "Please use \"splitWith\" instead" #-}
325+{-# INLINE serialWith #-}
326+serialWith :: Monad m => (a -> b -> c) -> Fold m x a -> Fold m x b -> Fold m x c
327+serialWith = splitWith
328+
323329-- $setup
324330-- >>> import Data.Function ((&))
325331-- >>> import qualified Streamly.Data.Fold as Fold
core/src/Streamly/Internal/Data/Array/Fold.hs+7−7
@@ -44,7 +44,7 @@ module Streamly.Internal.Data.Array.Fold
4444 -- * Applicative
4545 , fromPure
4646 , fromEffect
47− , serialWith
47+ , splitWith
4848
4949 -- * Monad
5050 , concatMap
@@ -255,20 +255,20 @@ serial_ (ArrayFold p1) (ArrayFold p2) =
255255-- results using the supplied function.
256256--
257257-- /Pre-release/
258−{-# INLINE serialWith #-}
259−serialWith :: MonadThrow m
258+{-# INLINE splitWith #-}
259+splitWith :: MonadThrow m
260260 => (a -> b -> c) -> ArrayFold m x a -> ArrayFold m x b -> ArrayFold m x c
261−serialWith f (ArrayFold p1) (ArrayFold p2) =
261+splitWith f (ArrayFold p1) (ArrayFold p2) =
262262 ArrayFold $ ParserD.noErrorUnsafeSplitWith f p1 p2
263263
264−-- | 'Applicative' form of 'serialWith'.
265−-- > (<*>) = serialWith id
264+-- | 'Applicative' form of 'splitWith'.
265+-- > (<*>) = splitWith id
266266instance MonadThrow m => Applicative (ArrayFold m a) where
267267 {-# INLINE pure #-}
268268 pure = fromPure
269269
270270 {-# INLINE (<*>) #-}
271− (<*>) = serialWith id
271+ (<*>) = splitWith id
272272
273273 {-# INLINE (*>) #-}
274274 (*>) = serial_
core/src/Streamly/Internal/Data/Fold.hs+3−3
@@ -237,7 +237,7 @@ module Streamly.Internal.Data.Fold
237237 -}
238238
239239 -- ** Serial Append
240− , serialWith
240+ , splitWith
241241 , serial_
242242 -- , tail
243243 -- , init
@@ -1464,7 +1464,7 @@ or = any (== True)
14641464-- >>> splitAt_ 4 [1,2,3]
14651465-- ([1,2,3],[])
14661466--
1467−-- > splitAt n f1 f2 = Fold.serialWith (,) (Fold.take n f1) f2
1467+-- > splitAt n f1 f2 = Fold.splitWith (,) (Fold.take n f1) f2
14681468--
14691469-- /Internal/
14701470
@@ -1475,7 +1475,7 @@ splitAt
14751475 -> Fold m a b
14761476 -> Fold m a c
14771477 -> Fold m a (b, c)
1478−splitAt n fld = serialWith (,) (take n fld)
1478+splitAt n fld = splitWith (,) (take n fld)
14791479
14801480------------------------------------------------------------------------------
14811481-- Element Aware APIs
core/src/Streamly/Internal/Data/Fold/Type.hs+6−6
@@ -375,7 +375,7 @@ module Streamly.Internal.Data.Fold.Type
375375 , dropping
376376
377377 -- ** Sequential application
378− , serialWith -- rename to "append"
378+ , splitWith -- rename to "append"
379379 , serial_
380380
381381 -- ** Repeated Application (Splitting)
@@ -758,7 +758,7 @@ data SeqFoldState sl f sr = SeqFoldL !sl | SeqFoldR !f !sr
758758--
759759-- >>> header = Fold.take 8 Fold.toList
760760-- >>> line = Fold.takeEndBy (== '\n') Fold.toList
761−-- >>> f = Fold.serialWith (,) header line
761+-- >>> f = Fold.splitWith (,) header line
762762-- >>> Stream.fold f $ Stream.fromList "header: hello\n"
763763-- ("header: ","hello\n")
764764--
@@ -773,10 +773,10 @@ data SeqFoldState sl f sr = SeqFoldL !sl | SeqFoldR !f !sr
773773--
774774-- /Time: O(n^2) where n is the number of compositions./
775775--
776−{-# INLINE serialWith #-}
777−serialWith :: Monad m =>
776+{-# INLINE splitWith #-}
777+splitWith :: Monad m =>
778778 (a -> b -> c) -> Fold m x a -> Fold m x b -> Fold m x c
779−serialWith func (Fold stepL initialL extractL) (Fold stepR initialR extractR) =
779+splitWith func (Fold stepL initialL extractL) (Fold stepR initialR extractR) =
780780 Fold step initial extract
781781
782782 where
@@ -810,7 +810,7 @@ data SeqFoldState_ sl sr = SeqFoldL_ !sl | SeqFoldR_ !sr
810810-- discarding the result of the first.
811811--
812812-- This was written in the hope that it might be faster than implementing it
813−-- using serialWith, but the current benchmarks show that it has the same
813+-- using splitWith, but the current benchmarks show that it has the same
814814-- performance. So do not expose it unless some benchmark shows benefit.
815815--
816816{-# INLINE serial_ #-}
core/src/Streamly/Internal/Data/Parser.hs+10−10
@@ -150,7 +150,7 @@ module Streamly.Internal.Data.Parser
150150 -- * Binary Combinators
151151
152152 -- ** Sequential Applicative
153− , serialWith
153+ , splitWith
154154 , split_
155155
156156{-
@@ -1083,7 +1083,7 @@ regexPCRE = undefined
10831083-- This implementation is strict in the second argument, therefore, the
10841084-- following will fail:
10851085--
1086−-- >>> Stream.parse (Parser.serialWith const (Parser.satisfy (> 0)) undefined) $ Stream.fromList [1]
1086+-- >>> Stream.parse (Parser.splitWith const (Parser.satisfy (> 0)) undefined) $ Stream.fromList [1]
10871087-- *** Exception: Prelude.undefined
10881088-- ...
10891089--
@@ -1092,31 +1092,31 @@ regexPCRE = undefined
10921092-- operations and can be faster than 'Applicative' instance for small number
10931093-- (less than 8) of compositions.
10941094--
1095−-- Many combinators can be expressed using @serialWith@ and other parser
1095+-- Many combinators can be expressed using @splitWith@ and other parser
10961096-- primitives. Some common idioms are described below,
10971097--
10981098-- @
10991099-- span :: (a -> Bool) -> Fold m a b -> Fold m a b -> Parser a m b
1100−-- span pred f1 f2 = serialWith (,) ('takeWhile' pred f1) ('fromFold' f2)
1100+-- span pred f1 f2 = splitWith (,) ('takeWhile' pred f1) ('fromFold' f2)
11011101-- @
11021102--
11031103-- @
11041104-- spanBy :: (a -> a -> Bool) -> Fold m a b -> Fold m a b -> Parser a m b
1105−-- spanBy eq f1 f2 = serialWith (,) ('groupBy' eq f1) ('fromFold' f2)
1105+-- spanBy eq f1 f2 = splitWith (,) ('groupBy' eq f1) ('fromFold' f2)
11061106-- @
11071107--
11081108-- @
11091109-- spanByRolling :: (a -> a -> Bool) -> Fold m a b -> Fold m a b -> Parser a m b
1110−-- spanByRolling eq f1 f2 = serialWith (,) ('groupByRolling' eq f1) ('fromFold' f2)
1110+-- spanByRolling eq f1 f2 = splitWith (,) ('groupByRolling' eq f1) ('fromFold' f2)
11111111-- @
11121112--
11131113-- /Pre-release/
11141114--
1115−{-# INLINE serialWith #-}
1116−serialWith :: Monad m
1115+{-# INLINE splitWith #-}
1116+splitWith :: Monad m
11171117 => (a -> b -> c) -> Parser x m a -> Parser x m b -> Parser x m c
1118−serialWith f p1 p2 =
1119− D.toParserK $ D.serialWith f (D.fromParserK p1) (D.fromParserK p2)
1118+splitWith f p1 p2 =
1119+ D.toParserK $ D.splitWith f (D.fromParserK p1) (D.fromParserK p2)
11201120
11211121-- | Sequential parser application ignoring the output of the first parser.
11221122-- Apply two parsers sequentially to an input stream. The input is provided to
core/src/Streamly/Internal/Data/Parser/ParserD.hs+1−1
@@ -106,7 +106,7 @@ module Streamly.Internal.Data.Parser.ParserD
106106 -- * Binary Combinators
107107
108108 -- ** Sequential Applicative
109− , serialWith
109+ , splitWith
110110 , split_
111111
112112 {-
core/src/Streamly/Internal/Data/Parser/ParserD/Type.hs+10−10
@@ -183,7 +183,7 @@ module Streamly.Internal.Data.Parser.ParserD.Type
183183
184184 , fromPure
185185 , fromEffect
186− , serialWith
186+ , splitWith
187187 , split_
188188
189189 , die
@@ -671,9 +671,9 @@ fromEffect b = Parser undefined (IDone <$> b) undefined
671671{-# ANN type SeqParseState Fuse #-}
672672data SeqParseState sl f sr = SeqParseL sl | SeqParseR f sr
673673
674−-- | See 'Streamly.Internal.Data.Parser.serialWith'.
674+-- | See 'Streamly.Internal.Data.Parser.splitWith'.
675675--
676−-- Note: this implementation of serialWith is fast because of stream fusion but
676+-- Note: this implementation of splitWith is fast because of stream fusion but
677677-- has quadratic time complexity, because each composition adds a new branch
678678-- that each subsequent parse's input element has to go through, therefore, it
679679-- cannot scale to a large number of compositions. After around 100
@@ -682,10 +682,10 @@ data SeqParseState sl f sr = SeqParseL sl | SeqParseR f sr
682682--
683683-- /Pre-release/
684684--
685−{-# INLINE serialWith #-}
686−serialWith :: Monad m
685+{-# INLINE splitWith #-}
686+splitWith :: Monad m
687687 => (a -> b -> c) -> Parser x m a -> Parser x m b -> Parser x m c
688−serialWith func (Parser stepL initialL extractL)
688+splitWith func (Parser stepL initialL extractL)
689689 (Parser stepR initialR extractR) =
690690 Parser step initial extract
691691
@@ -748,7 +748,7 @@ serialWith func (Parser stepL initialL extractL)
748748 IDone bR -> return $ Done n $ func bL bR
749749 IError err -> return $ Error err
750750 Error err -> return $ Error err
751− Partial _ _ -> error "Bug: serialWith extract 'Partial'"
751+ Partial _ _ -> error "Bug: splitWith extract 'Partial'"
752752 Continue n s -> return $ Continue n (SeqParseL s)
753753
754754-------------------------------------------------------------------------------
@@ -827,7 +827,7 @@ noErrorUnsafeSplitWith func (Parser stepL initialL extractL)
827827{-# ANN type SeqAState Fuse #-}
828828data SeqAState sl sr = SeqAL sl | SeqAR sr
829829
830−-- This turns out to be slightly faster than serialWith
830+-- This turns out to be slightly faster than splitWith
831831-- | See 'Streamly.Internal.Data.Parser.split_'.
832832--
833833-- /Pre-release/
@@ -940,13 +940,13 @@ noErrorUnsafeSplit_
940940 Partial _ _ -> error "split_: Partial"
941941 Continue n s -> return $ Continue n (SeqAL s)
942942
943−-- | 'Applicative' form of 'serialWith'.
943+-- | 'Applicative' form of 'splitWith'.
944944instance Monad m => Applicative (Parser a m) where
945945 {-# INLINE pure #-}
946946 pure = fromPure
947947
948948 {-# INLINE (<*>) #-}
949− (<*>) = serialWith id
949+ (<*>) = splitWith id
950950
951951 {-# INLINE (*>) #-}
952952 (*>) = split_
core/src/Streamly/Internal/Data/Parser/ParserK/Type.hs+2−2
@@ -139,8 +139,8 @@ fromPure b = MkParser $ \n st k -> k st (Success n b)
139139fromEffect :: Monad m => m b -> Parser a m b
140140fromEffect eff = MkParser $ \n st k -> eff >>= \b -> k st (Success n b)
141141
142−-- | 'Applicative' form of 'Streamly.Internal.Data.Parser.serialWith'. Note that
143−-- this operation does not fuse, use 'Streamly.Internal.Data.Parser.serialWith'
142+-- | 'Applicative' form of 'Streamly.Internal.Data.Parser.splitWith'. Note that
143+-- this operation does not fuse, use 'Streamly.Internal.Data.Parser.splitWith'
144144-- when fusion is important.
145145--
146146instance Monad m => Applicative (Parser a m) where
docs/User/ProjectRelated/Changelog.md+1
@@ -25,6 +25,7 @@
2525* In `Streamly.Data.Fold`:
2626 * `variance`, and `stdDev` have been deprecated. Please use the
2727 `streamly-statistics` package instead.
28+ * `serialWith` is deprecated. Use `splitWith` instead.
2829* `Streamly.Data.Array.Foreign` is deprecated. Use `Streamly.Data.Array`
2930 instead.
3031* The type of `replicateM` in `Streamly.Data.Unfold` is changed.
test/Streamly/Test/Data/Fold.hs+1−1
@@ -784,7 +784,7 @@ main = hspec $ do
784784 prop "takeEndByOrMax" takeEndByOrMax
785785
786786 -- Appending
787− -- serialWith
787+ -- splitWith
788788
789789 -- Distributing
790790 -- tee
test/Streamly/Test/Data/Parser.hs+4−4
@@ -537,7 +537,7 @@ parseManyWordQuotedBy =
537537-- splitWithPass :: Property
538538-- splitWithPass =
539539-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
540−-- case S.parse (P.serialWith (,) (P.satisfy (== 0)) (P.satisfy (== 1))) (S.fromList ls) of
540+-- case S.parse (P.splitWith (,) (P.satisfy (== 0)) (P.satisfy (== 1))) (S.fromList ls) of
541541-- Right (result_first, result_second) -> case ls of
542542-- 0 : 1 : _ -> (result_first == 0) && (result_second == 1)
543543-- _ -> False
@@ -547,19 +547,19 @@ parseManyWordQuotedBy =
547547
548548-- splitWithFailLeft :: Property
549549-- splitWithFailLeft =
550−-- property (case S.parse (P.serialWith (,) (P.die "die") (P.fromPure (1 :: Int))) (S.fromList [1 :: Int]) of
550+-- property (case S.parse (P.splitWith (,) (P.die "die") (P.fromPure (1 :: Int))) (S.fromList [1 :: Int]) of
551551-- Right _ -> False
552552-- Left _ -> True)
553553
554554-- splitWithFailRight :: Property
555555-- splitWithFailRight =
556−-- property (case S.parse (P.serialWith (,) (P.fromPure (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
556+-- property (case S.parse (P.splitWith (,) (P.fromPure (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
557557-- Right _ -> False
558558-- Left _ -> True)
559559
560560-- splitWithFailBoth :: Property
561561-- splitWithFailBoth =
562−-- property (case S.parse (P.serialWith (,) (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
562+-- property (case S.parse (P.splitWith (,) (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
563563-- Right _ -> False
564564-- Left _ -> True)
565565
test/Streamly/Test/Data/Parser/ParserD.hs+7−7
@@ -392,10 +392,10 @@ wordBy =
392392 in if wrds == [] && length lst > 0 then [""] else wrds
393393
394394
395−serialWith :: Property
396−serialWith =
395+splitWith :: Property
396+splitWith =
397397 forAll (listOf (chooseInt (0, 1))) $ \ls ->
398− case S.parseD (P.serialWith (,) (P.satisfy (== 0)) (P.satisfy (== 1))) (S.fromList ls) of
398+ case S.parseD (P.splitWith (,) (P.satisfy (== 0)) (P.satisfy (== 1))) (S.fromList ls) of
399399 Right (result_first, result_second) -> case ls of
400400 0 : 1 : _ -> (result_first == 0) && (result_second == 1)
401401 _ -> False
@@ -405,19 +405,19 @@ serialWith =
405405
406406splitWithFailLeft :: Property
407407splitWithFailLeft =
408− property (case S.parseD (P.serialWith (,) (P.die "die") (P.fromPure (1 :: Int))) (S.fromList [1 :: Int]) of
408+ property (case S.parseD (P.splitWith (,) (P.die "die") (P.fromPure (1 :: Int))) (S.fromList [1 :: Int]) of
409409 Right _ -> False
410410 Left _ -> True)
411411
412412splitWithFailRight :: Property
413413splitWithFailRight =
414− property (case S.parseD (P.serialWith (,) (P.fromPure (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
414+ property (case S.parseD (P.splitWith (,) (P.fromPure (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
415415 Right _ -> False
416416 Left _ -> True)
417417
418418splitWithFailBoth :: Property
419419splitWithFailBoth =
420− property (case S.parseD (P.serialWith (,) (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
420+ property (case S.parseD (P.splitWith (,) (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
421421 Right _ -> False
422422 Left _ -> True)
423423
@@ -794,7 +794,7 @@ main =
794794 prop "groupByRolling" groupByRolling
795795 prop "P.takeEndByOrMax = Prelude.take n (Prelude.takeWhile (not . predicate)" takeEndByOrMax
796796 prop "many (P.wordBy ' ') = words'" wordBy
797− prop "parse 0, then 1, else fail" serialWith
797+ prop "parse 0, then 1, else fail" splitWith
798798 prop "fail due to die as left parser" splitWithFailLeft
799799 prop "fail due to die as right parser" splitWithFailRight
800800 prop "fail due to die as both parsers" splitWithFailBoth