on master help sign in
Rename concatPairsWith to mergeMapWith and expose it
commit6bffc845bcaafbdb0591e1e6ed3f84daee683cb5 authorHarendra Kumar <harendra@composewell.com> dateThu 8 Dec 2022, 05:36 (4 years ago) parentd0605c1
5 files changed+44−47
benchmark/Streamly/Benchmark/Data/Stream/Common.hs+6−6 benchmark/Streamly/Benchmark/Data/Stream/Expand.hs+19−19 core/src/Streamly/Data/Stream.hs+5−8 core/src/Streamly/Internal/Data/Stream/Expand.hs+13−13 core/src/Streamly/Internal/Data/Stream/Top.hs+1−1
benchmark/Streamly/Benchmark/Data/Stream/Common.hs+6−6
@@ -42,7 +42,7 @@ module Stream.Common
4242
4343 -- Benchmarking functions
4444 , concatStreamsWith
45− , concatPairsWith
45+ , mergeMapWith
4646 , apDiscardFst
4747 , apDiscardSnd
4848 , apLiftA2
@@ -83,7 +83,7 @@ import qualified Streamly.Internal.Data.Stream.IsStream as Stream
8383import qualified Streamly.Prelude as Stream
8484import Streamly.Benchmark.Prelude
8585 ( composeN, sourceConcatMapId, benchIOSink
86− , concatStreamsWith, concatPairsWith
86+ , concatStreamsWith, mergeMapWith
8787 )
8888#else
8989import qualified Streamly.Internal.Data.Stream as Stream
@@ -206,15 +206,15 @@ concatStreamsWith op outer inner n =
206206 (sourceUnfoldrM inner)
207207 (sourceUnfoldrM outer n)
208208
209−{-# INLINE concatPairsWith #-}
210−concatPairsWith
209+{-# INLINE mergeMapWith #-}
210+mergeMapWith
211211 :: (Stream IO Int -> Stream IO Int -> Stream IO Int)
212212 -> Int
213213 -> Int
214214 -> Int
215215 -> IO ()
216−concatPairsWith op outer inner n =
217− drain $ Stream.concatPairsWith op
216+mergeMapWith op outer inner n =
217+ drain $ Stream.mergeMapWith op
218218 (sourceUnfoldrM inner)
219219 (sourceUnfoldrM outer n)
220220
benchmark/Streamly/Benchmark/Data/Stream/Expand.hs+19−19
@@ -246,15 +246,15 @@ inspect $ hasNoTypeClasses 'concatMapWithAppend
246246inspect $ 'concatMapWithAppend `hasNoType` ''SPEC
247247#endif
248248
249−-- concatPairWith
249+-- mergeMapWith
250250
251−{-# INLINE concatPairWithSerial #-}
252−concatPairWithSerial :: Int -> Int -> Int -> IO ()
253−concatPairWithSerial = concatPairsWith Common.append
251+{-# INLINE mergeMapWithSerial #-}
252+mergeMapWithSerial :: Int -> Int -> Int -> IO ()
253+mergeMapWithSerial = mergeMapWith Common.append
254254
255−{-# INLINE concatPairWithAppend #-}
256−concatPairWithAppend :: Int -> Int -> Int -> IO ()
257−concatPairWithAppend = concatPairsWith Common.append2
255+{-# INLINE mergeMapWithAppend #-}
256+mergeMapWithAppend :: Int -> Int -> Int -> IO ()
257+mergeMapWithAppend = mergeMapWith Common.append2
258258
259259-- unfoldMany
260260
@@ -334,21 +334,21 @@ o_n_space_concat :: Int -> [Benchmark]
334334o_n_space_concat value = sqrtVal `seq`
335335 [ bgroup "concat"
336336 [
337− -------------------concatPairsWith-----------------
337+ -------------------mergeMapWith-----------------
338338
339339 -- Use large number of streams to check scalability
340340
341− benchIOSrc1 "concatPairWithSerial (n of 1)"
342− (concatPairWithSerial value 1)
343− , benchIOSrc1 "concatPairWithSerial (sqrtVal of sqrtVal)"
344− (concatPairWithSerial sqrtVal sqrtVal)
345− , benchIOSrc1 "concatPairWithSerial (2 of n/2)"
346− (concatPairWithSerial 2 (value `div` 2))
347−
348− , benchIOSrc1 "concatPairWithAppend (n of 1)"
349− (concatPairWithAppend value 1)
350− , benchIOSrc1 "concatPairWithAppend (sqrtVal of sqrtVal)"
351− (concatPairWithAppend sqrtVal sqrtVal)
341+ benchIOSrc1 "mergeMapWithSerial (n of 1)"
342+ (mergeMapWithSerial value 1)
343+ , benchIOSrc1 "mergeMapWithSerial (sqrtVal of sqrtVal)"
344+ (mergeMapWithSerial sqrtVal sqrtVal)
345+ , benchIOSrc1 "mergeMapWithSerial (2 of n/2)"
346+ (mergeMapWithSerial 2 (value `div` 2))
347+
348+ , benchIOSrc1 "mergeMapWithAppend (n of 1)"
349+ (mergeMapWithAppend value 1)
350+ , benchIOSrc1 "mergeMapWithAppend (sqrtVal of sqrtVal)"
351+ (mergeMapWithAppend sqrtVal sqrtVal)
352352 ]
353353 ]
354354
core/src/Streamly/Data/Stream.hs+5−8
@@ -359,10 +359,8 @@ module Streamly.Data.Stream
359359
360360 -- ** Merging
361361 -- | Merging of @n@ streams can be performed by combining the streams pair
362− -- wise using
363− -- 'Streamly.Internal.Data.Stream.IsStream.Expand.concatPairsWith' to give
364− -- O(n * log n) time complexity.
365− -- If used with 'concatMapWith' it will have O(n^2) performance.
362+ -- wise using 'mergeMapWith' to give O(n * log n) time complexity. If used
363+ -- with 'concatMapWith' it will have O(n^2) performance.
366364
367365 , mergeBy
368366 , mergeByM
@@ -371,10 +369,8 @@ module Streamly.Data.Stream
371369
372370 -- ** Zipping
373371 -- | Zipping of @n@ streams can be performed by combining the streams pair
374− -- wise using
375− -- 'Streamly.Internal.Data.Stream.IsStream.Expand.concatPairsWith' with
376− -- O(n * log n) time complexity.
377− -- If used with 'concatMapWith' it will have O(n^2) performance.
372+ -- wise using 'mergeMapWith' with O(n * log n) time complexity. If used
373+ -- with 'concatMapWith' it will have O(n^2) performance.
378374 , zipWith
379375 , zipWithM
380376 -- , zipWith2
@@ -410,6 +406,7 @@ module Streamly.Data.Stream
410406 , concatMapWith
411407 , concatMap
412408 , concatMapM
409+ , mergeMapWith
413410
414411 -- * Repeated Fold
415412 , foldMany -- XXX Rename to foldRepeat
core/src/Streamly/Internal/Data/Stream/Expand.hs+13−13
@@ -22,7 +22,7 @@ module Streamly.Internal.Data.Stream.Expand
2222 -- complexity (instead O(n^2) for pair wise combinators described in the
2323 -- next section. These functions can be used efficiently with
2424 -- 'concatMapWith' et. al. combinators that combine streams in a linear
25− -- fashion (contrast with 'concatPairsWith' which combines streams as a
25+ -- fashion (contrast with 'mergeMapWith' which combines streams as a
2626 -- binary tree).
2727
2828 append
@@ -30,7 +30,7 @@ module Streamly.Internal.Data.Stream.Expand
3030 -- | Like the functions in the section above these functions also combine
3131 -- two streams into a single stream but when used @n@ times linearly they
3232 -- exhibit O(n^2) complexity. They are best combined in a binary tree
33− -- fashion using 'concatPairsWith' giving a @n * log n@ complexity. Avoid
33+ -- fashion using 'mergeMapWith' giving a @n * log n@ complexity. Avoid
3434 -- using these with 'concatMapWith' when combining a large or infinite
3535 -- number of streams.
3636
@@ -104,7 +104,7 @@ module Streamly.Internal.Data.Stream.Expand
104104 -- simultaneously anyway.
105105 --
106106 -- However, in cases where the merging consumes streams in a round robin
107− -- fashion, a pair wise merging using 'concatPairsWith' would be more
107+ -- fashion, a pair wise merging using 'mergeMapWith' would be more
108108 -- efficient. These cases include operations like 'mergeBy' or 'zipWith'.
109109
110110 , concatMapWith
@@ -114,7 +114,7 @@ module Streamly.Internal.Data.Stream.Expand
114114 -- * ConcatPairsWith
115115 -- | See the notes about suitable merge functions in the 'concatMapWith'
116116 -- section.
117− , concatPairsWith
117+ , mergeMapWith
118118
119119 -- * IterateMap
120120 -- | Map and flatten Trees of Streams
@@ -165,7 +165,7 @@ infixr 6 `append2`
165165-- 'append' when combining two fusible streams. However, it slows down
166166-- quadratically with the number of streams being appended. Therefore, it is
167167-- suitable for ad-hoc append of a few streams, and should not be used with
168−-- 'concatMapWith' or 'concatPairsWith'.
168+-- 'concatMapWith' or 'mergeMapWith'.
169169--
170170-- /Fused/
171171--
@@ -360,7 +360,7 @@ mergeByM f m1 m2 = fromStreamK $ K.mergeByM f (toStreamK m1) (toStreamK m2)
360360-- | Like 'mergeByM' but much faster, works best when merging statically known
361361-- number of streams. When merging more than two streams try to merge pairs and
362362-- pair of pairs in a tree like structure.'mergeByM' works better with variable
363−-- number of streams being merged using 'concatPairsWith'.
363+-- number of streams being merged using 'mergeMapWith'.
364364--
365365-- /Internal/
366366{-# INLINE mergeByM2 #-}
@@ -400,7 +400,7 @@ mergeFstBy _f _m1 _m2 = undefined
400400unfoldMany ::Monad m => Unfold m a b -> Stream m a -> Stream m b
401401unfoldMany u m = fromStreamD $ D.unfoldMany u (toStreamD m)
402402
403−-- | This does not pair streams like concatPairsWith, instead, it goes through
403+-- | This does not pair streams like mergeMapWith, instead, it goes through
404404-- each stream one by one and yields one element from each stream. After it
405405-- goes to the last stream it reverses the traversal to come back to the first
406406-- stream yielding elements from each stream on its way back to the first
@@ -411,7 +411,7 @@ unfoldMany u m = fromStreamD $ D.unfoldMany u (toStreamD m)
411411-- >>> Stream.fold Fold.toList interleaved
412412-- [1,2,3,4,5,5,4,3,2,1]
413413--
414−-- Note that this is order of magnitude more efficient than "concatPairsWith
414+-- Note that this is order of magnitude more efficient than "mergeMapWith
415415-- wSerial" because of fusion.
416416--
417417-- /Fused/
@@ -581,24 +581,24 @@ concatSmapMWith combine f initial =
581581-- >>> s = Stream.fromList [5,1,7,9,2]
582582-- >>> generate = Stream.fromPure
583583-- >>> combine = Stream.mergeBy compare
584−-- >>> Stream.fold Fold.toList $ Stream.concatPairsWith combine generate s
584+-- >>> Stream.fold Fold.toList $ Stream.mergeMapWith combine generate s
585585-- [1,2,5,7,9]
586586--
587587-- Note that if the stream length is not a power of 2, the binary tree composed
588−-- by concatPairsWith would not be balanced, which may or may not be important
588+-- by mergeMapWith would not be balanced, which may or may not be important
589589-- depending on what you are trying to achieve.
590590--
591591-- /Caution: the stream of streams must be finite/
592592--
593593-- /Pre-release/
594594--
595−{-# INLINE concatPairsWith #-}
596−concatPairsWith ::
595+{-# INLINE mergeMapWith #-}
596+mergeMapWith ::
597597 (Stream m b -> Stream m b -> Stream m b)
598598 -> (a -> Stream m b)
599599 -> Stream m a
600600 -> Stream m b
601−concatPairsWith par f m =
601+mergeMapWith par f m =
602602 fromStreamK
603603 $ K.concatPairsWith
604604 (\s1 s2 -> toStreamK $ fromStreamK s1 `par` fromStreamK s2)
core/src/Streamly/Internal/Data/Stream/Top.hs+1−1
@@ -114,7 +114,7 @@ sortBy cmp =
114114 (\x -> (< GT) . cmp x)
115115 Fold.toStreamRev
116116 Fold.toStream
117− in Stream.concatPairsWith (Stream.mergeBy cmp) id
117+ in Stream.mergeMapWith (Stream.mergeBy cmp) id
118118 . Stream.catRights . Stream.parseMany (fmap (either id id) p)
119119
120120------------------------------------------------------------------------------