on master help sign in
Rename concatUnfold to unfoldMany #986
commitb73b5ffa3a82b70ed8c6c7f6f362fc612198d489 authorRanjeet Kumar Ranjan <ranjeetr@composewell.com> dateSat 20 Mar 2021, 10:53 (6 years ago) parent7759cc0
24 files changed+125−118
benchmark/Streamly/Benchmark/Data/Stream/StreamD.hs+11−11 benchmark/Streamly/Benchmark/FileSystem/Handle/Read.hs+1−1 benchmark/Streamly/Benchmark/Prelude/Serial/Nested.hs+12−12 benchmark/Streamly/Benchmark/Prelude/WSerial.hs+18−18 design/optimization-guidelines.md+1−1 docs/Build.md+1−1 src/Streamly/Data/Unfold.hs+1−1 src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+3−3 src/Streamly/Internal/Data/Array/Foreign/Type.hs+2−2 src/Streamly/Internal/Data/Array/PrimInclude.hs+1−1 src/Streamly/Internal/Data/Array/Stream/Foreign.hs+3−3 src/Streamly/Internal/Data/Stream/IsStream/Common.hs+1−1 src/Streamly/Internal/Data/Stream/IsStream/Expand.hs+33−27 src/Streamly/Internal/Data/Stream/StreamD/Generate.hs+1−1 src/Streamly/Internal/Data/Stream/StreamD/Nesting.hs+18−18 src/Streamly/Internal/Data/Stream/StreamD/Transform.hs+1−1 src/Streamly/Internal/Data/Stream/StreamD/Type.hs+7−7 src/Streamly/Internal/Data/Stream/StreamK/Type.hs+1−1 src/Streamly/Internal/Data/Unfold.hs+3−3 src/Streamly/Internal/Data/Unfold/Type.hs+1−1 src/Streamly/Prelude.hs+1 test/Streamly/Test/Common/Array.hs+1−1 test/Streamly/Test/Data/Parser/ParserD.hs+1−1 test/lib/Streamly/Test/Prelude/Common.hs+2−2
benchmark/Streamly/Benchmark/Data/Stream/StreamD.hs+11−11
@@ -402,20 +402,20 @@ inspect $ hasNoTypeClasses 'concatMapRepl
402402inspect $ 'concatMapRepl `hasNoType` ''SPEC
403403#endif
404404
405−-- concatUnfold replicate/unfoldrM
405+-- unfoldMany replicate/unfoldrM
406406
407−{-# INLINE concatUnfoldRepl #-}
408−concatUnfoldRepl :: Int -> Int -> Int -> IO ()
409−concatUnfoldRepl outer inner n =
407+{-# INLINE unfoldManyRepl #-}
408+unfoldManyRepl :: Int -> Int -> Int -> IO ()
409+unfoldManyRepl outer inner n =
410410 S.drain
411− $ S.concatUnfold
411+ $ S.unfoldMany
412412 (UF.lmap return (UF.replicateM inner))
413413 (sourceUnfoldrMN outer n)
414414
415415#ifdef INSPECTION
416−inspect $ hasNoTypeClasses 'concatUnfoldRepl
417−inspect $ 'concatUnfoldRepl `hasNoType` ''S.ConcatMapUState
418−inspect $ 'concatUnfoldRepl `hasNoType` ''SPEC
416+inspect $ hasNoTypeClasses 'unfoldManyRepl
417+inspect $ 'unfoldManyRepl `hasNoType` ''S.ConcatMapUState
418+inspect $ 'unfoldManyRepl `hasNoType` ''SPEC
419419#endif
420420
421421-------------------------------------------------------------------------------
@@ -488,11 +488,11 @@ o_1_space =
488488 , benchIOSrc1 "concatMap (1 of n)"
489489 (concatMap 1 value)
490490
491− -- concatMap vs concatUnfold
491+ -- concatMap vs unfoldMany
492492 , benchIOSrc1 "concatMapRepl (sqrt n of sqrt n)"
493493 (concatMapRepl value2 value2)
494− , benchIOSrc1 "concatUnfoldRepl (sqrt n of sqrt n)"
495− (concatUnfoldRepl value2 value2)
494+ , benchIOSrc1 "unfoldManyRepl (sqrt n of sqrt n)"
495+ (unfoldManyRepl value2 value2)
496496 ]
497497 , bgroup "filtering"
498498 [ benchFold "filter-even" (filterEven 1) sourceUnfoldrM
benchmark/Streamly/Benchmark/FileSystem/Handle/Read.hs+1−1
@@ -286,7 +286,7 @@ toChunksConcatUnfoldCountLines inh =
286286 $ IUS.lines FL.drain
287287 $ SS.decodeLatin1
288288 -- XXX replace with toBytes
289− $ S.concatUnfold A.read (IFH.toChunks inh)
289+ $ S.unfoldMany A.read (IFH.toChunks inh)
290290
291291#ifdef INSPECTION
292292inspect $ hasNoTypeClasses 'toChunksConcatUnfoldCountLines
benchmark/Streamly/Benchmark/Prelude/Serial/Nested.hs+12−12
@@ -261,22 +261,22 @@ inspect $ hasNoTypeClasses 'concatMapWithAppend
261261inspect $ 'concatMapWithAppend `hasNoType` ''SPEC
262262#endif
263263
264−-- concatUnfold
264+-- unfoldMany
265265
266−-- concatUnfold replicate/unfoldrM
266+-- unfoldMany replicate/unfoldrM
267267
268−{-# INLINE concatUnfoldRepl #-}
269−concatUnfoldRepl :: Int -> Int -> Int -> IO ()
270−concatUnfoldRepl outer inner n =
268+{-# INLINE unfoldManyRepl #-}
269+unfoldManyRepl :: Int -> Int -> Int -> IO ()
270+unfoldManyRepl outer inner n =
271271 S.drain
272− $ S.concatUnfold
272+ $ S.unfoldMany
273273 (UF.lmap return (UF.replicateM inner))
274274 (sourceUnfoldrM outer n)
275275
276276#ifdef INSPECTION
277−inspect $ hasNoTypeClasses 'concatUnfoldRepl
278−inspect $ 'concatUnfoldRepl `hasNoType` ''D.ConcatMapUState
279−inspect $ 'concatUnfoldRepl `hasNoType` ''SPEC
277+inspect $ hasNoTypeClasses 'unfoldManyRepl
278+inspect $ 'unfoldManyRepl `hasNoType` ''D.ConcatMapUState
279+inspect $ 'unfoldManyRepl `hasNoType` ''SPEC
280280#endif
281281
282282o_1_space_concat :: Int -> [Benchmark]
@@ -322,11 +322,11 @@ o_1_space_concat value = sqrtVal `seq`
322322 , benchIOSrc1 "concatMapWithAppend (2 of n/2)"
323323 (concatMapWithAppend 2 (value `div` 2))
324324
325− -- concatMap vs concatUnfold
325+ -- concatMap vs unfoldMany
326326 , benchIOSrc1 "concatMapRepl (sqrt n of sqrt n)"
327327 (concatMapRepl sqrtVal sqrtVal)
328− , benchIOSrc1 "concatUnfoldRepl (sqrt n of sqrt n)"
329− (concatUnfoldRepl sqrtVal sqrtVal)
328+ , benchIOSrc1 "unfoldManyRepl (sqrt n of sqrt n)"
329+ (unfoldManyRepl sqrtVal sqrtVal)
330330 ]
331331 ]
332332
benchmark/Streamly/Benchmark/Prelude/WSerial.hs+18−18
@@ -122,31 +122,31 @@ o_1_space_concat value =
122122 ]
123123 ]
124124
125−{-# INLINE concatUnfoldInterleaveRepl4xN #-}
126−concatUnfoldInterleaveRepl4xN :: Int -> Int -> IO ()
127−concatUnfoldInterleaveRepl4xN value n =
128− S.drain $ Internal.concatUnfoldInterleave
125+{-# INLINE unfoldManyInterleaveRepl4xN #-}
126+unfoldManyInterleaveRepl4xN :: Int -> Int -> IO ()
127+unfoldManyInterleaveRepl4xN value n =
128+ S.drain $ Internal.unfoldManyInterleave
129129 (UF.lmap return (UF.replicateM 4))
130130 (sourceUnfoldrM (value `div` 4) n)
131131
132132#ifdef INSPECTION
133−inspect $ hasNoTypeClasses 'concatUnfoldInterleaveRepl4xN
134−-- inspect $ 'concatUnfoldInterleaveRepl4xN `hasNoType` ''SPEC
135−-- inspect $ 'concatUnfoldInterleaveRepl4xN `hasNoType`
133+inspect $ hasNoTypeClasses 'unfoldManyInterleaveRepl4xN
134+-- inspect $ 'unfoldManyInterleaveRepl4xN `hasNoType` ''SPEC
135+-- inspect $ 'unfoldManyInterleaveRepl4xN `hasNoType`
136136-- ''D.ConcatUnfoldInterleaveState
137137#endif
138138
139−{-# INLINE concatUnfoldRoundrobinRepl4xN #-}
140−concatUnfoldRoundrobinRepl4xN :: Int -> Int -> IO ()
141−concatUnfoldRoundrobinRepl4xN value n =
142− S.drain $ Internal.concatUnfoldRoundrobin
139+{-# INLINE unfoldManyRoundRobinRepl4xN #-}
140+unfoldManyRoundRobinRepl4xN :: Int -> Int -> IO ()
141+unfoldManyRoundRobinRepl4xN value n =
142+ S.drain $ Internal.unfoldManyRoundRobin
143143 (UF.lmap return (UF.replicateM 4))
144144 (sourceUnfoldrM (value `div` 4) n)
145145
146146#ifdef INSPECTION
147−inspect $ hasNoTypeClasses 'concatUnfoldRoundrobinRepl4xN
148−-- inspect $ 'concatUnfoldRoundrobinRepl4xN `hasNoType` ''SPEC
149−-- inspect $ 'concatUnfoldRoundrobinRepl4xN `hasNoType`
147+inspect $ hasNoTypeClasses 'unfoldManyRoundRobinRepl4xN
148+-- inspect $ 'unfoldManyRoundRobinRepl4xN `hasNoType` ''SPEC
149+-- inspect $ 'unfoldManyRoundRobinRepl4xN `hasNoType`
150150-- ''D.ConcatUnfoldInterleaveState
151151#endif
152152
@@ -159,11 +159,11 @@ o_n_heap_concat value =
159159 -- WSerial expands slowly because of binary interleave behavior and
160160 -- this expands immediately because of Nary interleave behavior.
161161 benchIOSrc1
162− "concatUnfoldInterleaveRepl (x/4,4)"
163− (concatUnfoldInterleaveRepl4xN value)
162+ "unfoldManyInterleaveRepl (x/4,4)"
163+ (unfoldManyInterleaveRepl4xN value)
164164 , benchIOSrc1
165− "concatUnfoldRoundrobinRepl (x/4,4)"
166− (concatUnfoldRoundrobinRepl4xN value)
165+ "unfoldManyRoundRobinRepl (x/4,4)"
166+ (unfoldManyRoundRobinRepl4xN value)
167167 ]
168168 ]
169169
design/optimization-guidelines.md+1−1
@@ -52,7 +52,7 @@ The haddock documentation includes a note when an operation cannot fuse.
5252### Unfolds
5353
5454* Use unfolds especially when higher order operations are involved. For
55− example, `concatUnfold` can fuse completely whereas `concatMap` would
55+ example, `unfoldMany` can fuse completely whereas `concatMap` would
5656 not fuse.
5757* Use `outerProduct` in `Unfold` module instead of using the monad instance of
5858 streams to fuse nested loops where performance matters. See the unfold
docs/Build.md+1−1
@@ -167,7 +167,7 @@ we can resolve this using fusion-plugin in future.
167167### Delayed INLINE
168168
169169When a function is passed to a higher order function e.g. a function
170−passed to `concatMap` or `concatUnfold` then we want the function to be
170+passed to `concatMap` or `unfoldMany` then we want the function to be
171171inlined after the higher order is inlined so that proper fusion of the
172172higher order function can occur. For such cases we usually add INLINE[1]
173173on the function being passed to instruct GHC not to inline it too early.
src/Streamly/Data/Unfold.hs+1−1
@@ -30,7 +30,7 @@
3030-- much less efficient when compared to combinators using 'Unfold'. For
3131-- example, the 'Streamly.Prelude.concatMap' combinator which uses @a -> t m b@
3232-- (where @t@ is a stream type) to generate streams is much less efficient
33−-- compared to 'Streamly.Prelude.concatUnfold'.
33+-- compared to 'Streamly.Prelude.unfoldMany'.
3434--
3535-- On the other hand, transformation operations on stream types are as
3636-- efficient as transformations on 'Unfold'.
src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+3−3
@@ -569,7 +569,7 @@ data FlattenState s a =
569569
570570-- | Use the "read" unfold instead.
571571--
572−-- @flattenArrays = concatUnfold read@
572+-- @flattenArrays = unfoldMany read@
573573--
574574-- We can try this if there are any fusion issues in the unfold.
575575--
@@ -603,7 +603,7 @@ flattenArrays (D.Stream step state) = D.Stream step' (OuterLoop state)
603603
604604-- | Use the "readRev" unfold instead.
605605--
606−-- @flattenArrays = concatUnfold readRev@
606+-- @flattenArrays = unfoldMany readRev@
607607--
608608-- We can try this if there are any fusion issues in the unfold.
609609--
@@ -987,7 +987,7 @@ fromStreamD :: (MonadIO m, Storable a) => D.Stream m a -> m (Array a)
987987fromStreamD m = do
988988 buffered <- bufferChunks m
989989 len <- K.foldl' (+) 0 (K.map length buffered)
990− fromStreamDN len $ D.concatUnfold read $ D.fromStreamK buffered
990+ fromStreamDN len $ D.unfoldMany read $ D.fromStreamK buffered
991991
992992-- | Create an 'Array' from a list. The list must be of finite size.
993993--
src/Streamly/Internal/Data/Array/Foreign/Type.hs+2−2
@@ -334,7 +334,7 @@ arraysOf n str = D.map unsafeFreeze $ MA.arraysOf n str
334334
335335-- | Use the "read" unfold instead.
336336--
337−-- @flattenArrays = concatUnfold read@
337+-- @flattenArrays = unfoldMany read@
338338--
339339-- We can try this if there are any fusion issues in the unfold.
340340--
@@ -345,7 +345,7 @@ flattenArrays = MA.flattenArrays . D.map unsafeThaw
345345
346346-- | Use the "readRev" unfold instead.
347347--
348−-- @flattenArrays = concatUnfold readRev@
348+-- @flattenArrays = unfoldMany readRev@
349349--
350350-- We can try this if there are any fusion issues in the unfold.
351351--
src/Streamly/Internal/Data/Array/PrimInclude.hs+1−1
@@ -173,7 +173,7 @@ last arr = readIndex arr (length arr - 1)
173173concat :: (IsStream t, MonadIO m, Prim a) => t m (Array a) -> t m a
174174-- concat m = D.fromStreamD $ A.flattenArrays (D.toStreamD m)
175175-- concat m = D.fromStreamD $ D.concatMap A.toStreamD (D.toStreamD m)
176−concat m = D.fromStreamD $ D.concatUnfold read (D.toStreamD m)
176+concat m = D.fromStreamD $ D.unfoldMany read (D.toStreamD m)
177177
178178-- | Coalesce adjacent arrays in incoming stream to form bigger arrays of a
179179-- maximum specified size in bytes.
src/Streamly/Internal/Data/Array/Stream/Foreign.hs+3−3
@@ -93,19 +93,19 @@ arraysOf n str = D.fromStreamD $ A.arraysOf n (D.toStreamD str)
9393--
9494-- Same as the following but more efficient:
9595--
96−-- > concat = Stream.concatUnfold Array.read
96+-- > concat = Stream.unfoldMany Array.read
9797--
9898-- @since 0.7.0
9999{-# INLINE concat #-}
100100concat :: (IsStream t, MonadIO m, Storable a) => t m (Array a) -> t m a
101101-- concat m = D.fromStreamD $ A.flattenArrays (D.toStreamD m)
102102-- concat m = D.fromStreamD $ D.concatMap A.toStreamD (D.toStreamD m)
103−concat m = D.fromStreamD $ D.concatUnfold A.read (D.toStreamD m)
103+concat m = D.fromStreamD $ D.unfoldMany A.read (D.toStreamD m)
104104
105105-- | Convert a stream of arrays into a stream of their elements reversing the
106106-- contents of each array before flattening.
107107--
108−-- > concatRev = Stream.concatUnfold Array.readRev
108+-- > concatRev = Stream.unfoldMany Array.readRev
109109--
110110-- @since 0.7.0
111111{-# INLINE concatRev #-}
src/Streamly/Internal/Data/Stream/IsStream/Common.hs+1−1
@@ -460,7 +460,7 @@ concatMapM f m = fromStreamD $ D.concatMapM (fmap toStreamD . f) (toStreamD m)
460460-- concatMap f = 'concatMapM' (return . f)
461461-- concatMap = 'concatMapWith' 'Serial.serial'
462462-- concatMap f = 'concat . map f'
463−-- concatMap f = 'concatUnfold' (UF.lmap f UF.fromStream)
463+-- concatMap f = 'unfoldMany' (UF.lmap f UF.fromStream)
464464-- @
465465--
466466-- @since 0.6.0
src/Streamly/Internal/Data/Stream/IsStream/Expand.hs+33−27
@@ -89,12 +89,13 @@ module Streamly.Internal.Data.Stream.IsStream.Expand
8989
9090 -- ** Append Many (Unfold)
9191 -- | Unfold and flatten streams.
92+ , unfoldMany
93+ , unfoldManyInterleave
94+ , unfoldManyRoundRobin
9295 , concatUnfold
93− , concatUnfoldInterleave
94− , concatUnfoldRoundrobin
9596
9697 -- ** Interpose
97− -- | Insert effects between streams. Like concatUnfold but intersperses an
98+ -- | Insert effects between streams. Like unfoldMany but intersperses an
9899 -- effect between the streams. A special case of gintercalate.
99100 , interpose
100101 , interposeSuffix
@@ -102,7 +103,7 @@ module Streamly.Internal.Data.Stream.IsStream.Expand
102103
103104 -- ** Intercalate
104105 -- | Insert Streams between Streams.
105− -- Like concatUnfold but intersperses streams from another source between
106+ -- Like unfoldMany but intersperses streams from another source between
106107 -- the streams from the first source.
107108 , intercalate
108109 , intercalateSuffix
@@ -449,40 +450,45 @@ mergeAsyncByM f m1 m2 =
449450 let par = Par.mkParallelD . toStreamD
450451 in D.mergeByM f (par m1) (par m2)
451452
453+
454+-- @since 0.7.0
455+{-# DEPRECATED concatUnfold "Please use unfoldMany instead." #-}
456+{-# INLINE concatUnfold #-}
457+concatUnfold ::(IsStream t, Monad m) => Unfold m a b -> t m a -> t m b
458+concatUnfold u m = fromStreamD $ D.unfoldMany u (toStreamD m)
459+
452460------------------------------------------------------------------------------
453−-- Combine N Streams - concatUnfold
461+-- Combine N Streams - unfoldMany
454462------------------------------------------------------------------------------
455463
456−-- XXX Rename to unfoldMany
457−--
458464-- | Like 'concatMap' but uses an 'Unfold' for stream generation. Unlike
459465-- 'concatMap' this can fuse the 'Unfold' code with the inner loop and
460466-- therefore provide many times better performance.
461467--
462−-- @since 0.7.0
463−{-# INLINE concatUnfold #-}
464−concatUnfold ::(IsStream t, Monad m) => Unfold m a b -> t m a -> t m b
465−concatUnfold u m = fromStreamD $ D.concatUnfold u (toStreamD m)
468+-- @since 0.8.0
469+{-# INLINE unfoldMany #-}
470+unfoldMany ::(IsStream t, Monad m) => Unfold m a b -> t m a -> t m b
471+unfoldMany u m = fromStreamD $ D.unfoldMany u (toStreamD m)
466472
467−-- | Like 'concatUnfold' but interleaves the streams in the same way as
473+-- | Like 'unfoldMany' but interleaves the streams in the same way as
468474-- 'interleave' behaves instead of appending them.
469475--
470476-- /Pre-release/
471−{-# INLINE concatUnfoldInterleave #-}
472−concatUnfoldInterleave ::(IsStream t, Monad m)
477+{-# INLINE unfoldManyInterleave #-}
478+unfoldManyInterleave ::(IsStream t, Monad m)
473479 => Unfold m a b -> t m a -> t m b
474−concatUnfoldInterleave u m =
475− fromStreamD $ D.concatUnfoldInterleave u (toStreamD m)
480+unfoldManyInterleave u m =
481+ fromStreamD $ D.unfoldManyInterleave u (toStreamD m)
476482
477−-- | Like 'concatUnfold' but executes the streams in the same way as
483+-- | Like 'unfoldMany' but executes the streams in the same way as
478484-- 'roundrobin'.
479485--
480486-- /Pre-release/
481−{-# INLINE concatUnfoldRoundrobin #-}
482−concatUnfoldRoundrobin ::(IsStream t, Monad m)
487+{-# INLINE unfoldManyRoundRobin #-}
488+unfoldManyRoundRobin ::(IsStream t, Monad m)
483489 => Unfold m a b -> t m a -> t m b
484−concatUnfoldRoundrobin u m =
485− fromStreamD $ D.concatUnfoldRoundrobin u (toStreamD m)
490+unfoldManyRoundRobin u m =
491+ fromStreamD $ D.unfoldManyRoundRobin u (toStreamD m)
486492
487493------------------------------------------------------------------------------
488494-- Combine N Streams - interpose
@@ -521,11 +527,11 @@ interposeSuffix x unf str =
521527------------------------------------------------------------------------------
522528
523529-- XXX we can swap the order of arguments to gintercalate so that the
524−-- definition of concatUnfold becomes simpler? The first stream should be
530+-- definition of unfoldMany becomes simpler? The first stream should be
525531-- infixed inside the second one. However, if we change the order in
526532-- "interleave" as well similarly, then that will make it a bit unintuitive.
527533--
528−-- > concatUnfold unf str =
534+-- > unfoldMany unf str =
529535-- > gintercalate unf str (UF.nilM (\_ -> return ())) (repeat ())
530536--
531537-- | 'interleaveInfix' followed by unfold and concat.
@@ -547,7 +553,7 @@ gintercalate unf1 str1 unf2 str2 =
547553-- will be starting with the second stream.
548554
549555-- > intercalate seed unf str = gintercalate unf str unf (repeatM seed)
550−-- > intercalate a unf str = concatUnfold unf $ intersperse a str
556+-- > intercalate a unf str = unfoldMany unf $ intersperse a str
551557--
552558-- | 'intersperse' followed by unfold and concat.
553559--
@@ -561,7 +567,7 @@ gintercalate unf1 str1 unf2 str2 =
561567intercalate :: (IsStream t, Monad m)
562568 => b -> Unfold m b c -> t m b -> t m c
563569intercalate seed unf str = D.fromStreamD $
564− D.concatUnfold unf $ D.intersperse seed (toStreamD str)
570+ D.unfoldMany unf $ D.intersperse seed (toStreamD str)
565571
566572-- | 'interleaveSuffix' followed by unfold and concat.
567573--
@@ -576,7 +582,7 @@ gintercalateSuffix unf1 str1 unf2 str2 =
576582 unf2 (D.toStreamD str2)
577583
578584-- > intercalateSuffix seed unf str = gintercalateSuffix unf str unf (repeatM seed)
579−-- > intercalateSuffix a unf str = concatUnfold unf $ intersperseSuffix a str
585+-- > intercalateSuffix a unf str = unfoldMany unf $ intersperseSuffix a str
580586--
581587-- | 'intersperseSuffix' followed by unfold and concat.
582588--
@@ -589,7 +595,7 @@ gintercalateSuffix unf1 str1 unf2 str2 =
589595{-# INLINE intercalateSuffix #-}
590596intercalateSuffix :: (IsStream t, Monad m)
591597 => b -> Unfold m b c -> t m b -> t m c
592−intercalateSuffix seed unf str = fromStreamD $ D.concatUnfold unf
598+intercalateSuffix seed unf str = fromStreamD $ D.unfoldMany unf
593599 $ D.intersperseSuffix (return seed) (D.toStreamD str)
594600
595601{-
src/Streamly/Internal/Data/Stream/StreamD/Generate.hs+1−1
@@ -10,7 +10,7 @@
1010-- Prefer unfolds ("Streamly.Internal.Data.Unfold") over the combinators in
1111-- this module. They are more powerful and efficient as they can be transformed
1212-- and composed on the input side efficiently and they can fuse in nested
13−-- operations (e.g. concatUnfold). All the combinators in this module can be
13+-- operations (e.g. unfoldMany). All the combinators in this module can be
1414-- expressed using unfolds with the same efficiency.
1515--
1616-- Operations in this module that are not in "Streamly.Internal.Data.Unfold":
src/Streamly/Internal/Data/Stream/StreamD/Nesting.hs+18−18
@@ -35,13 +35,13 @@ module Streamly.Internal.Data.Stream.StreamD.Nesting
3535
3636 -- *** Appending
3737 -- | Append a stream after another. A special case of concatMap or
38− -- concatUnfold.
38+ -- unfoldMany.
3939 AppendState(..)
4040 , append
4141
4242 -- *** Interleaving
4343 -- | Interleave elements from two streams alternately. A special case of
44− -- concatUnfoldInterleave.
44+ -- unfoldManyInterleave.
4545 , InterleaveState(..)
4646 , interleave
4747 , interleaveMin
@@ -51,7 +51,7 @@ module Streamly.Internal.Data.Stream.StreamD.Nesting
5151 -- *** Scheduling
5252 -- | Execute streams alternately irrespective of whether they generate
5353 -- elements or not. Note 'interleave' would execute a stream until it
54− -- yields an element. A special case of concatUnfoldRoundrobin.
54+ -- yields an element. A special case of unfoldManyRoundRobin.
5555 , roundRobin -- interleaveFair?/ParallelFair
5656
5757 -- *** Zipping
@@ -70,7 +70,7 @@ module Streamly.Internal.Data.Stream.StreamD.Nesting
7070 -- @
7171 -- concat: f (t m a) -> t m a
7272 -- concatMap: (a -> t m b) -> t m a -> t m b
73− -- concatUnfold: Unfold m a b -> t m a -> t m b
73+ -- unfoldMany: Unfold m a b -> t m a -> t m b
7474 -- @
7575
7676 -- *** ConcatMap
@@ -83,19 +83,19 @@ module Streamly.Internal.Data.Stream.StreamD.Nesting
8383 -- | Generate streams by using an unfold on each element of an input
8484 -- stream, append the resulting streams and flatten. A special case of
8585 -- gintercalate.
86− , concatUnfold
86+ , unfoldMany
8787 , ConcatUnfoldInterleaveState (..)
88− , concatUnfoldInterleave
89− , concatUnfoldRoundrobin
88+ , unfoldManyInterleave
89+ , unfoldManyRoundRobin
9090
9191 -- *** Interpose
92− -- | Like concatUnfold but intersperses an effect between the streams. A
92+ -- | Like unfoldMany but intersperses an effect between the streams. A
9393 -- special case of gintercalate.
9494 , interpose
9595 , interposeSuffix
9696
9797 -- *** Intercalate
98− -- | Like concatUnfold but intersperses streams from another source between
98+ -- | Like unfoldMany but intersperses streams from another source between
9999 -- the streams from the first source.
100100 , gintercalate
101101 , gintercalateSuffix
@@ -116,7 +116,7 @@ module Streamly.Internal.Data.Stream.StreamD.Nesting
116116
117117 -- ** Parsing
118118 -- | Parsing is opposite to flattening. 'parseMany' is dual to concatMap or
119− -- concatUnfold. concatMap generates a stream from single values in a
119+ -- unfoldMany. concatMap generates a stream from single values in a
120120 -- stream and flattens, parseMany does the opposite of flattening by
121121 -- splitting the stream and then folds each such split to single value in
122122 -- the output stream.
@@ -477,7 +477,7 @@ mergeBy
477477mergeBy cmp = mergeByM (\a b -> return $ cmp a b)
478478
479479------------------------------------------------------------------------------
480−-- Combine N Streams - concatUnfold
480+-- Combine N Streams - unfoldMany
481481------------------------------------------------------------------------------
482482
483483data ConcatUnfoldInterleaveState o i =
@@ -502,9 +502,9 @@ data ConcatUnfoldInterleaveState o i =
502502-- Ideally, we need some scheduling bias to inner streams vs outer stream.
503503-- Maybe we can configure the behavior.
504504--
505−{-# INLINE_NORMAL concatUnfoldInterleave #-}
506−concatUnfoldInterleave :: Monad m => Unfold m a b -> Stream m a -> Stream m b
507−concatUnfoldInterleave (Unfold istep inject) (Stream ostep ost) =
505+{-# INLINE_NORMAL unfoldManyInterleave #-}
506+unfoldManyInterleave :: Monad m => Unfold m a b -> Stream m a -> Stream m b
507+unfoldManyInterleave (Unfold istep inject) (Stream ostep ost) =
508508 Stream step (ConcatUnfoldInterleaveOuter ost [])
509509 where
510510 {-# INLINE_LATE step #-}
@@ -553,11 +553,11 @@ concatUnfoldInterleave (Unfold istep inject) (Stream ostep ost) =
553553--
554554-- This could be inefficient if the tasks are too small.
555555--
556−-- Compared to concatUnfoldInterleave this one switches streams on Skips.
556+-- Compared to unfoldManyInterleave this one switches streams on Skips.
557557--
558−{-# INLINE_NORMAL concatUnfoldRoundrobin #-}
559−concatUnfoldRoundrobin :: Monad m => Unfold m a b -> Stream m a -> Stream m b
560−concatUnfoldRoundrobin (Unfold istep inject) (Stream ostep ost) =
558+{-# INLINE_NORMAL unfoldManyRoundRobin #-}
559+unfoldManyRoundRobin :: Monad m => Unfold m a b -> Stream m a -> Stream m b
560+unfoldManyRoundRobin (Unfold istep inject) (Stream ostep ost) =
561561 Stream step (ConcatUnfoldInterleaveOuter ost [])
562562 where
563563 {-# INLINE_LATE step #-}
src/Streamly/Internal/Data/Stream/StreamD/Transform.hs+1−1
@@ -1129,7 +1129,7 @@ reverse' m = Stream step Nothing
11291129 return $ Yield x (Just (start, next))
11301130-}
11311131reverse' =
1132− A.flattenArraysRev -- concatUnfold A.readRev
1132+ A.flattenArraysRev -- unfoldMany A.readRev
11331133 . fromStreamK
11341134 . K.reverse
11351135 . toStreamK
src/Streamly/Internal/Data/Stream/StreamD/Type.hs+7−7
@@ -75,7 +75,7 @@ module Streamly.Internal.Data.Stream.StreamD.Type
7575
7676 -- * Nesting
7777 , ConcatMapUState (..)
78− , concatUnfold
78+ , unfoldMany
7979 , concatMap
8080 , concatMapM
8181 , FoldMany (..) -- for inspection testing
@@ -690,7 +690,7 @@ instance Applicative f => Applicative (Stream f) where
690690 (<*) = apDiscardSnd
691691
692692------------------------------------------------------------------------------
693−-- Combine N Streams - concatUnfold
693+-- Combine N Streams - unfoldMany
694694------------------------------------------------------------------------------
695695
696696-- Define a unique structure to use in inspection testing
@@ -698,7 +698,7 @@ data ConcatMapUState o i =
698698 ConcatMapUOuter o
699699 | ConcatMapUInner o i
700700
701−-- | @concatUnfold unfold stream@ uses @unfold@ to map the input stream elements
701+-- | @unfoldMany unfold stream@ uses @unfold@ to map the input stream elements
702702-- to streams and then flattens the generated streams into a single output
703703-- stream.
704704
@@ -707,9 +707,9 @@ data ConcatMapUState o i =
707707-- optimization via fusion. This can be many times more efficient than
708708-- 'concatMap'.
709709
710−{-# INLINE_NORMAL concatUnfold #-}
711−concatUnfold :: Monad m => Unfold m a b -> Stream m a -> Stream m b
712−concatUnfold (Unfold istep inject) (Stream ostep ost) =
710+{-# INLINE_NORMAL unfoldMany #-}
711+unfoldMany :: Monad m => Unfold m a b -> Stream m a -> Stream m b
712+unfoldMany (Unfold istep inject) (Stream ostep ost) =
713713 Stream step (ConcatMapUOuter ost)
714714 where
715715 {-# INLINE_LATE step #-}
@@ -728,7 +728,7 @@ concatUnfold (Unfold istep inject) (Stream ostep ost) =
728728 Yield x i' -> Yield x (ConcatMapUInner o i')
729729 Skip i' -> Skip (ConcatMapUInner o i')
730730 Stop -> Skip (ConcatMapUOuter o)
731−
731+
732732------------------------------------------------------------------------------
733733-- Combine N Streams - concatMap
734734------------------------------------------------------------------------------
src/Streamly/Internal/Data/Stream/StreamK/Type.hs+1−1
@@ -1181,7 +1181,7 @@ instance Monad m => Monad (Stream m) where
11811181
11821182{-
11831183-- Like concatMap but generates stream using an unfold function. Similar to
1184−-- concatUnfold but for StreamK.
1184+-- unfoldMany but for StreamK.
11851185concatUnfoldr :: IsStream t
11861186 => (b -> t m (Maybe (a, b))) -> t m b -> t m a
11871187concatUnfoldr = undefined
src/Streamly/Internal/Data/Unfold.hs+3−3
@@ -17,13 +17,13 @@
1717--
1818-- This allows an important optimization to occur in several cases, making the
1919-- 'Unfold' a more efficient abstraction. Consider the 'concatMap' and
20−-- 'concatUnfold' operations, the latter is more efficient. 'concatMap'
20+-- 'unfoldMany' operations, the latter is more efficient. 'concatMap'
2121-- generates a new stream object from each element in the stream by applying
2222-- the supplied function to the element, the stream object includes the "step"
2323-- function as well as the initial "state" of the stream. Since the stream is
2424-- generated dynamically the compiler does not know the step function or the
2525-- state type statically at compile time, therefore, it cannot inline it. On
26−-- the other hand in case of 'concatUnfold' the compiler has visibility into
26+-- the other hand in case of 'unfoldMany' the compiler has visibility into
2727-- the unfold's state generation function, therefore, the compiler knows all
2828-- the types statically and it can inline the inject as well as the step
2929-- functions, generating efficient code. Essentially, the stream is not opaque
@@ -844,7 +844,7 @@ fromProducer = Unfold step (return . FromSVarRead)
844844-- stream.
845845--
846846-- @
847−-- S.mapM_ print $ S.concatUnfold (UF.teeZipWith (,) UF.identity (UF.singleton sqrt)) $ S.fromList [1..10]
847+-- S.mapM_ print $ S.unfoldMany (UF.teeZipWith (,) UF.identity (UF.singleton sqrt)) $ S.fromList [1..10]
848848-- @
849849--
850850-- /Pre-release/
src/Streamly/Internal/Data/Unfold/Type.hs+1−1
@@ -352,7 +352,7 @@ zipWithM f (Unfold step1 inject1) (Unfold step2 inject2) = Unfold step inject
352352--
353353-- @
354354-- S.mapM_ print
355−-- $ S.concatUnfold (UF.zipWith (,) UF.identity (UF.singleton sqrt))
355+-- $ S.unfoldMany (UF.zipWith (,) UF.identity (UF.singleton sqrt))
356356-- $ S.map (\x -> (x,x))
357357-- $ S.fromList [1..10]
358358-- @
src/Streamly/Prelude.hs+1
@@ -596,6 +596,7 @@ module Streamly.Prelude
596596 , concatMap
597597 , concatMapM
598598 , concatUnfold
599+ , unfoldMany
599600
600601 -- ** Folding Containers of Streams
601602 -- | These are variants of standard 'Foldable' fold functions that use a
test/Streamly/Test/Common/Array.hs+1−1
@@ -163,7 +163,7 @@ testArraysOf =
163163 monadicIO $ do
164164 xs <- run
165165 $ S.toList
166− $ S.concatUnfold A.read
166+ $ S.unfoldMany A.read
167167 $ arraysOf 240
168168 $ S.fromList list
169169 assert (xs == list)
test/Streamly/Test/Data/Parser/ParserD.hs+1−1
@@ -630,7 +630,7 @@ parseUnfold = do
630630 Producer.simplify (Source.parseManyD parser readSrc)
631631 xs <- run
632632 $ S.toList
633− $ S.concatUnfold Unfold.fromList
633+ $ S.unfoldMany Unfold.fromList
634634 $ S.unfold streamParser src
635635
636636 listEquals (==) xs ls
test/lib/Streamly/Test/Prelude/Common.hs+2−2
@@ -1198,10 +1198,10 @@ transformCombineOpsCommon constr desc eq t = do
11981198 forAll (choose (0, 100)) $ \n ->
11991199 transform (concatMap (const [1..n]))
12001200 t (S.concatMapM (const (return $ S.fromList [1..n])))
1201− prop (desc <> " concatUnfold") $
1201+ prop (desc <> " unfoldMany") $
12021202 forAll (choose (0, 100)) $ \n ->
12031203 transform (concatMap (const [1..n]))
1204− t (S.concatUnfold (UF.lmap (const undefined)
1204+ t (S.unfoldMany (UF.lmap (const undefined)
12051205 $ UF.supply UF.fromList [1..n]))
12061206
12071207toListFL :: Monad m => FL.Fold m a [a]