@@ -32,6 +32,7 @@ module Streamly.Internal.Data.Stream.Concurrent
32 , StopWhen (..)
32 , StopWhen (..)
33 , stopWhen
33 , stopWhen
35 -- maxYields
36 -- maxYields
36 , Rate(..)
37 , Rate(..)
@@ -62,13 +63,12 @@ module Streamly.Internal.Data.Stream.Concurrent
62 -- | Use a channel for each pair.
63 -- | Use a channel for each pair.
63 -- combine/concur/conjoin
64 -- combine/concur/conjoin
66 , interleave
66 , interleave
69 , parallel
68 , parallel
70 , parallelFst
69 , parallelFst
71 , parallelMin
70 , parallelMin
73 -- ** Apply
73 -- ** Apply
74 -- | Use a separate channel for each application.
74 -- | Use a separate channel for each application.
@@ -83,7 +83,6 @@ module Streamly.Internal.Data.Stream.Concurrent
83 , concatMap
83 , concatMap
84 , concatMapInterleave
84 , concatMapInterleave
85 , concatMapWith
85 , concatMapWith
86− , concatMapInterleaveWith
@@ -140,23 +139,14 @@ eval = evalWith id
140-------------------------------------------------------------------------------
139-------------------------------------------------------------------------------
142-- | Like 'append' but with a Config modifier.
141-- | Like 'append' but with a Config modifier.
143−{-# INLINE appendWith #-}
142+{-# INLINE combineWith #-}
144−appendWith :: MonadAsync m =>
143+combineWith :: MonadAsync m =>
145 (Config -> Config) -> Stream m a -> Stream m a -> Stream m a
144 (Config -> Config) -> Stream m a -> Stream m a -> Stream m a
146−appendWith modifier stream1 stream2 =
145+combineWith modifier stream1 stream2 =
147 Stream.fromStreamK
146 Stream.fromStreamK
148 $ appendWithK
147 $ appendWithK
149 modifier (Stream.toStreamK stream1) (Stream.toStreamK stream2)
148 modifier (Stream.toStreamK stream1) (Stream.toStreamK stream2)
151−-- | Like 'interleave' but with a Config modifier.
152−{-# INLINE interleaveWith #-}
153−interleaveWith :: MonadAsync m =>
154− (Config -> Config) -> Stream m a -> Stream m a -> Stream m a
155−interleaveWith modifier stream1 stream2 =
158− modifier (Stream.toStreamK stream1) (Stream.toStreamK stream2)
160-- | Binary operation to evaluate two streams concurrently prioritizing the
150-- | Binary operation to evaluate two streams concurrently prioritizing the
161-- left stream.
151-- left stream.
@@ -167,7 +157,7 @@ interleaveWith modifier stream1 stream2 =
167-- all streams. However, with this operation you can precisely control the
157-- all streams. However, with this operation you can precisely control the
168-- scheduling by creating arbitrary shape expression trees.
158-- scheduling by creating arbitrary shape expression trees.
170−-- >>> append = Async.appendWith id
160+-- >>> append = Async.combineWith id
172-- The following code finishes in 4 seconds:
162-- The following code finishes in 4 seconds:
@@ -180,52 +170,52 @@ interleaveWith modifier stream1 stream2 =
181{-# INLINE append #-}
171{-# INLINE append #-}
182append :: MonadAsync m => Stream m a -> Stream m a -> Stream m a
172append :: MonadAsync m => Stream m a -> Stream m a -> Stream m a
183−append = appendWith id
173+append = combineWith id
185-- | Like 'append' but interleaves the streams fairly instead of prioritizing
175-- | Like 'append' but interleaves the streams fairly instead of prioritizing
186-- the left stream. This schedules all streams in a round robin fashion over
176-- the left stream. This schedules all streams in a round robin fashion over
187-- limited number of threads.
177-- limited number of threads.
189−-- >>> interleave = Async.interleaveWith id
179+-- >>> interleave = Async.combineWith Async.interleaved
191{-# INLINE interleave #-}
181{-# INLINE interleave #-}
192interleave :: MonadAsync m => Stream m a -> Stream m a -> Stream m a
182interleave :: MonadAsync m => Stream m a -> Stream m a -> Stream m a
193−interleave = interleaveWith id
183+interleave = combineWith interleaved
195-- | Like 'append' but with 'ordered' on.
185-- | Like 'append' but with 'ordered' on.
197−-- >>> ahead = Async.appendWith Async.ordered
187+-- >>> ahead = Async.combineWith Async.ordered
199{-# INLINE ahead #-}
189{-# INLINE ahead #-}
200ahead :: MonadAsync m => Stream m a -> Stream m a -> Stream m a
190ahead :: MonadAsync m => Stream m a -> Stream m a -> Stream m a
201−ahead = appendWith ordered
191+ahead = combineWith ordered
203-- | Like 'append' but with 'eagerEval' on.
193-- | Like 'append' but with 'eagerEval' on.
205−-- >>> parallel = Async.appendWith Async.eagerEval
195+-- >>> parallel = Async.combineWith Async.eagerEval
207{-# INLINE parallel #-}
197{-# INLINE parallel #-}
208parallel :: MonadAsync m => Stream m a -> Stream m a -> Stream m a
198parallel :: MonadAsync m => Stream m a -> Stream m a -> Stream m a
209−parallel = appendWith eagerEval
199+parallel = combineWith eagerEval
211-- | Like 'parallel' but stops the output as soon as the first stream stops.
201-- | Like 'parallel' but stops the output as soon as the first stream stops.
213−-- >>> parallelFst = Async.appendWith (Async.eagerEval . Async.stopWhen Async.FirstStops)
203+-- >>> parallelFst = Async.combineWith (Async.eagerEval . Async.stopWhen Async.FirstStops)
215-- /Pre-release/
205-- /Pre-release/
216{-# INLINE parallelFst #-}
206{-# INLINE parallelFst #-}
217parallelFst :: MonadAsync m => Stream m a -> Stream m a -> Stream m a
207parallelFst :: MonadAsync m => Stream m a -> Stream m a -> Stream m a
218−parallelFst = appendWith (eagerEval . stopWhen FirstStops)
208+parallelFst = combineWith (eagerEval . stopWhen FirstStops)
220-- | Like 'parallel' but stops the output as soon as any of the two streams
210-- | Like 'parallel' but stops the output as soon as any of the two streams
223−-- >>> parallelMin = Async.appendWith (Async.eagerEval . Async.stopWhen Async.AnyStops)
213+-- >>> parallelMin = Async.combineWith (Async.eagerEval . Async.stopWhen Async.AnyStops)
225-- /Pre-release/
215-- /Pre-release/
226{-# INLINE parallelMin #-}
216{-# INLINE parallelMin #-}
227parallelMin :: MonadAsync m => Stream m a -> Stream m a -> Stream m a
217parallelMin :: MonadAsync m => Stream m a -> Stream m a -> Stream m a
228−parallelMin = appendWith (eagerEval . stopWhen AnyStops)
218+parallelMin = combineWith (eagerEval . stopWhen AnyStops)
230-- concatMapWith modifier f stream = concatWith modifier $ fmap f stream
220-- concatMapWith modifier f stream = concatWith modifier $ fmap f stream
@@ -240,17 +230,6 @@ concatMapWith modifier f stream =
240 $ concatMapWithK
230 $ concatMapWithK
241 modifier (Stream.toStreamK . f) (Stream.toStreamK stream)
231 modifier (Stream.toStreamK . f) (Stream.toStreamK stream)
243−-- | Like 'concatMapInterleave' but we can also specify the concurrent
244−-- channel's configuration parameters using Config modifiers.
246−{-# INLINE concatMapInterleaveWith #-}
247−concatMapInterleaveWith :: MonadAsync m =>
248− (Config -> Config) -> (a -> Stream m b) -> Stream m a -> Stream m b
249−concatMapInterleaveWith modifier f stream =
251− $ concatMapInterleaveWithK
252− modifier (Stream.toStreamK . f) (Stream.toStreamK stream)
254-- | Map each element of the input to a stream and then concurrently evaluate
233-- | Map each element of the input to a stream and then concurrently evaluate
255-- and concatenate the resulting streams. Multiple streams may be evaluated
234-- and concatenate the resulting streams. Multiple streams may be evaluated
256-- concurrently but earlier streams are perferred. Output from the streams are
235-- concurrently but earlier streams are perferred. Output from the streams are
@@ -298,11 +277,11 @@ concatMap = concatMapWith id
298-- evaluate the earlier stream first, this schedules all streams in a round
277-- evaluate the earlier stream first, this schedules all streams in a round
299-- robin fashion over the available threads.
278-- robin fashion over the available threads.
301−-- >>> concatMapInterleave = Async.concatMapInterleaveWith id
280+-- >>> concatMapInterleave = Async.concatMapWith Async.interleaved
303-- When used with a single thread it behaves like serial interleaving:
282-- When used with a single thread it behaves like serial interleaving:
305−-- >>> f cfg xs = Stream.fold Fold.toList $ Async.concatMapInterleaveWith cfg id $ Stream.fromList xs
284+-- >>> f cfg xs = Stream.fold Fold.toList $ Async.concatMapWith (Async.interleaved . cfg) id $ Stream.fromList xs
307-- >>> stream1 = Stream.fromList [1,2,3]
286-- >>> stream1 = Stream.fromList [1,2,3]
308-- >>> stream2 = Stream.fromList [4,5,6]
287-- >>> stream2 = Stream.fromList [4,5,6]
@@ -313,7 +292,7 @@ concatMap = concatMapWith id
314{-# INLINE concatMapInterleave #-}
293{-# INLINE concatMapInterleave #-}
315concatMapInterleave :: MonadAsync m => (a -> Stream m b) -> Stream m a -> Stream m b
294concatMapInterleave :: MonadAsync m => (a -> Stream m b) -> Stream m a -> Stream m b
316−concatMapInterleave = concatMapInterleaveWith id
295+concatMapInterleave = concatMapWith interleaved
318-- | Like 'concat' but we can also specify the concurrent channel's
297-- | Like 'concat' but we can also specify the concurrent channel's
319-- configuration parameters using Config modifiers.
298-- configuration parameters using Config modifiers.