@@ -145,6 +145,8 @@ import Streamly.Streams
145------------------------------------------------------------------------------
145------------------------------------------------------------------------------
147-- | Build a Stream by unfolding pure steps starting from a seed.
147-- | Build a Stream by unfolding pure steps starting from a seed.
148unfoldr :: IsStream t => (b -> Maybe (a, b)) -> b -> t m a
150unfoldr :: IsStream t => (b -> Maybe (a, b)) -> b -> t m a
149unfoldr step = fromStream . go
151unfoldr step = fromStream . go
@@ -154,6 +156,8 @@ unfoldr step = fromStream . go
154 Just (a, b) -> yld a (go b)
156 Just (a, b) -> yld a (go b)
156-- | Build a Stream by unfolding monadic steps starting from a seed.
158-- | Build a Stream by unfolding monadic steps starting from a seed.
157unfoldrM :: (IsStream t, Monad m) => (b -> m (Maybe (a, b))) -> b -> t m a
161unfoldrM :: (IsStream t, Monad m) => (b -> m (Maybe (a, b))) -> b -> t m a
158unfoldrM step = fromStream . go
162unfoldrM step = fromStream . go
@@ -164,11 +168,15 @@ unfoldrM step = fromStream . go
164 Just (a, b) -> yld a (go b)
168 Just (a, b) -> yld a (go b)
166-- | Construct a stream from a 'Foldable' container.
170-- | Construct a stream from a 'Foldable' container.
167{-# INLINE fromFoldable #-}
173{-# INLINE fromFoldable #-}
168fromFoldable :: (IsStream t, Foldable f) => f a -> t m a
174fromFoldable :: (IsStream t, Foldable f) => f a -> t m a
169fromFoldable = Prelude.foldr cons nil
175fromFoldable = Prelude.foldr cons nil
171-- | Same as 'fromFoldable'.
177-- | Same as 'fromFoldable'.
172{-# DEPRECATED each "Please use fromFoldable instead." #-}
180{-# DEPRECATED each "Please use fromFoldable instead." #-}
173{-# INLINE each #-}
181{-# INLINE each #-}
174each :: (IsStream t, Foldable f) => f a -> t m a
182each :: (IsStream t, Foldable f) => f a -> t m a
@@ -182,10 +190,14 @@ each = fromFoldable
183-- ["hello"]
191-- ["hello"]
185once :: (IsStream t, Monad m) => m a -> t m a
195once :: (IsStream t, Monad m) => m a -> t m a
186once = fromStream . S.once
196once = fromStream . S.once
188-- | Generate a stream by performing a monadic action @n@ times.
198-- | Generate a stream by performing a monadic action @n@ times.
189replicateM :: (IsStream t, Monad m) => Int -> m a -> t m a
201replicateM :: (IsStream t, Monad m) => Int -> m a -> t m a
190replicateM n m = fromStream $ go n
202replicateM n m = fromStream $ go n
@@ -195,6 +207,8 @@ replicateM n m = fromStream $ go n
195 else m >>= \a -> yld a (go (cnt - 1))
207 else m >>= \a -> yld a (go (cnt - 1))
197-- | Generate a stream by repeatedly executing a monadic action forever.
209-- | Generate a stream by repeatedly executing a monadic action forever.
198repeatM :: (IsStream t, Monad m) => m a -> t m a
212repeatM :: (IsStream t, Monad m) => m a -> t m a
199repeatM = fromStream . go
213repeatM = fromStream . go
@@ -202,6 +216,8 @@ repeatM = fromStream . go
202 m >>= \a -> yld a (go m)
216 m >>= \a -> yld a (go m)
204-- | Iterate a pure function from a seed value, streaming the results forever.
218-- | Iterate a pure function from a seed value, streaming the results forever.
205iterate :: IsStream t => (a -> a) -> a -> t m a
221iterate :: IsStream t => (a -> a) -> a -> t m a
206iterate step = fromStream . go
222iterate step = fromStream . go
@@ -209,6 +225,8 @@ iterate step = fromStream . go
210-- | Iterate a monadic function from a seed value, streaming the results
226-- | Iterate a monadic function from a seed value, streaming the results
211-- forever.
227-- forever.
212iterateM :: (IsStream t, Monad m) => (a -> m a) -> a -> t m a
230iterateM :: (IsStream t, Monad m) => (a -> m a) -> a -> t m a
213iterateM step = fromStream . go
231iterateM step = fromStream . go
@@ -217,6 +235,8 @@ iterateM step = fromStream . go
217 yld s (go a)
235 yld s (go a)
219-- | Read lines from an IO Handle into a stream of Strings.
237-- | Read lines from an IO Handle into a stream of Strings.
220fromHandle :: (IsStream t, MonadIO m) => IO.Handle -> t m String
240fromHandle :: (IsStream t, MonadIO m) => IO.Handle -> t m String
221fromHandle h = fromStream go
241fromHandle h = fromStream go
@@ -238,6 +258,8 @@ fromHandle h = fromStream go
238-- >> runIdentity $ foldr (:) [] (serially $ fromFoldable [1,2,3])
258-- >> runIdentity $ foldr (:) [] (serially $ fromFoldable [1,2,3])
239-- [1,2,3]
259-- [1,2,3]
241foldr :: Monad m => (a -> b -> b) -> b -> StreamT m a -> m b
263foldr :: Monad m => (a -> b -> b) -> b -> StreamT m a -> m b
242foldr step acc m = go (toStream m)
264foldr step acc m = go (toStream m)
@@ -254,6 +276,8 @@ foldr step acc m = go (toStream m)
254-- >> runIdentity $ foldrM (\\x xs -> return (x : xs)) [] (serially $ fromFoldable [1,2,3])
276-- >> runIdentity $ foldrM (\\x xs -> return (x : xs)) [] (serially $ fromFoldable [1,2,3])
255-- [1,2,3]
277-- [1,2,3]
257{-# INLINE foldrM #-}
281{-# INLINE foldrM #-}
258foldrM :: Monad m => (a -> b -> m b) -> b -> StreamT m a -> m b
282foldrM :: Monad m => (a -> b -> m b) -> b -> StreamT m a -> m b
259foldrM step acc m = go (toStream m)
283foldrM step acc m = go (toStream m)
@@ -268,6 +292,8 @@ foldrM step acc m = go (toStream m)
268-- user supplied extraction function (the third argument) at each step. This is
292-- user supplied extraction function (the third argument) at each step. This is
269-- designed to work with the @foldl@ library. The suffix @x@ is a mnemonic for
293-- designed to work with the @foldl@ library. The suffix @x@ is a mnemonic for
270-- extraction.
294-- extraction.
271{-# INLINE scanx #-}
297{-# INLINE scanx #-}
272scanx :: IsStream t => (x -> a -> x) -> x -> (x -> b) -> t m a -> t m b
298scanx :: IsStream t => (x -> a -> x) -> x -> (x -> b) -> t m a -> t m b
273scanx step begin done m = cons (done begin) $ fromStream $ go (toStream m) begin
299scanx step begin done m = cons (done begin) $ fromStream $ go (toStream m) begin
@@ -279,6 +305,8 @@ scanx step begin done m = cons (done begin) $ fromStream $ go (toStream m) begin
279 in yld (done s) (go r s)
305 in yld (done s) (go r s)
280 in S.runStream m1 Nothing stp single yield
306 in S.runStream m1 Nothing stp single yield
282{-# DEPRECATED scan "Please use scanx instead." #-}
310{-# DEPRECATED scan "Please use scanx instead." #-}
283scan :: IsStream t => (x -> a -> x) -> x -> (x -> b) -> t m a -> t m b
311scan :: IsStream t => (x -> a -> x) -> x -> (x -> b) -> t m a -> t m b
284scan = scanx
312scan = scanx
@@ -287,6 +315,8 @@ scan = scanx
287-- step, generating a stream of all intermediate fold results. The first
315-- step, generating a stream of all intermediate fold results. The first
288-- element of the stream is the user supplied initial value, and the last
316-- element of the stream is the user supplied initial value, and the last
289-- element of the stream is the same as the result of 'foldl''.
317-- element of the stream is the same as the result of 'foldl''.
290{-# INLINE scanl' #-}
320{-# INLINE scanl' #-}
291scanl' :: IsStream t => (b -> a -> b) -> b -> t m a -> t m b
321scanl' :: IsStream t => (b -> a -> b) -> b -> t m a -> t m b
292scanl' step begin m = scanx step begin id m
322scanl' step begin m = scanx step begin id m
@@ -295,6 +325,8 @@ scanl' step begin m = scanx step begin id m
295-- left fold, but applies a user supplied extraction function (the third
325-- left fold, but applies a user supplied extraction function (the third
296-- argument) to the folded value at the end. This is designed to work with the
326-- argument) to the folded value at the end. This is designed to work with the
297-- @foldl@ library. The suffix @x@ is a mnemonic for extraction.
327-- @foldl@ library. The suffix @x@ is a mnemonic for extraction.
298{-# INLINE foldx #-}
330{-# INLINE foldx #-}
299foldx :: Monad m => (x -> a -> x) -> x -> (x -> b) -> StreamT m a -> m b
331foldx :: Monad m => (x -> a -> x) -> x -> (x -> b) -> StreamT m a -> m b
300foldx step begin done m = get $ go (toStream m) begin
332foldx step begin done m = get $ go (toStream m) begin
@@ -316,17 +348,23 @@ foldx step begin done m = get $ go (toStream m) begin
316 in (S.runStream stream) Nothing undefined sng yld
348 in (S.runStream stream) Nothing undefined sng yld
317 in (S.runStream m1) Nothing stop single yield
349 in (S.runStream m1) Nothing stop single yield
319{-# DEPRECATED foldl "Please use foldx instead." #-}
353{-# DEPRECATED foldl "Please use foldx instead." #-}
320foldl :: Monad m => (x -> a -> x) -> x -> (x -> b) -> StreamT m a -> m b
354foldl :: Monad m => (x -> a -> x) -> x -> (x -> b) -> StreamT m a -> m b
321foldl = foldx
355foldl = foldx
323-- | Strict left associative fold.
357-- | Strict left associative fold.
324{-# INLINE foldl' #-}
360{-# INLINE foldl' #-}
325foldl' :: Monad m => (b -> a -> b) -> b -> StreamT m a -> m b
361foldl' :: Monad m => (b -> a -> b) -> b -> StreamT m a -> m b
326foldl' step begin m = foldx step begin id m
362foldl' step begin m = foldx step begin id m
328-- XXX replace the recursive "go" with explicit continuations.
364-- XXX replace the recursive "go" with explicit continuations.
329-- | Like 'foldx', but with a monadic step function.
365-- | Like 'foldx', but with a monadic step function.
330foldxM :: Monad m => (x -> a -> m x) -> m x -> (x -> m b) -> StreamT m a -> m b
368foldxM :: Monad m => (x -> a -> m x) -> m x -> (x -> m b) -> StreamT m a -> m b
331foldxM step begin done m = go begin (toStream m)
369foldxM step begin done m = go begin (toStream m)
@@ -336,17 +374,23 @@ foldxM step begin done m = go begin (toStream m)
336 yield a r = acc >>= \b -> go (step b a) r
374 yield a r = acc >>= \b -> go (step b a) r
337 in (S.runStream m1) Nothing stop single yield
375 in (S.runStream m1) Nothing stop single yield
339{-# DEPRECATED foldlM "Please use foldxM instead." #-}
379{-# DEPRECATED foldlM "Please use foldxM instead." #-}
340foldlM :: Monad m => (x -> a -> m x) -> m x -> (x -> m b) -> StreamT m a -> m b
380foldlM :: Monad m => (x -> a -> m x) -> m x -> (x -> m b) -> StreamT m a -> m b
341foldlM = foldxM
381foldlM = foldxM
343-- | Like 'foldl'' but with a monadic step function.
383-- | Like 'foldl'' but with a monadic step function.
344foldlM' :: Monad m => (b -> a -> m b) -> b -> StreamT m a -> m b
386foldlM' :: Monad m => (b -> a -> m b) -> b -> StreamT m a -> m b
345foldlM' step begin m = foldxM step (return begin) return m
387foldlM' step begin m = foldxM step (return begin) return m
347-- | Decompose a stream into its head and tail. If the stream is empty, returns
389-- | Decompose a stream into its head and tail. If the stream is empty, returns
348-- 'Nothing'. If the stream is non-empty, returns 'Just (a, ma)', where 'a' is
390-- 'Nothing'. If the stream is non-empty, returns 'Just (a, ma)', where 'a' is
349-- the head of the stream and 'ma' its tail.
391-- the head of the stream and 'ma' its tail.
350uncons :: (IsStream t, Monad m) => StreamT m a -> m (Maybe (a, t m a))
394uncons :: (IsStream t, Monad m) => StreamT m a -> m (Maybe (a, t m a))
351uncons m =
395uncons m =
352 let stop = return Nothing
396 let stop = return Nothing
@@ -355,6 +399,8 @@ uncons m =
355 in (S.runStream (toStream m)) Nothing stop single yield
399 in (S.runStream (toStream m)) Nothing stop single yield
357-- | Write a stream of Strings to an IO Handle.
401-- | Write a stream of Strings to an IO Handle.
358toHandle :: MonadIO m => IO.Handle -> StreamT m String -> m ()
404toHandle :: MonadIO m => IO.Handle -> StreamT m String -> m ()
359toHandle h m = go (toStream m)
405toHandle h m = go (toStream m)
@@ -369,11 +415,15 @@ toHandle h m = go (toStream m)
369------------------------------------------------------------------------------
415------------------------------------------------------------------------------
371-- | Convert a stream into a list in the underlying monad.
417-- | Convert a stream into a list in the underlying monad.
372{-# INLINABLE toList #-}
420{-# INLINABLE toList #-}
373toList :: Monad m => StreamT m a -> m [a]
421toList :: Monad m => StreamT m a -> m [a]
374toList = foldrM (\a xs -> return (a : xs)) []
422toList = foldrM (\a xs -> return (a : xs)) []
376-- | Take first 'n' elements from the stream and discard the rest.
424-- | Take first 'n' elements from the stream and discard the rest.
377{-# INLINE take #-}
427{-# INLINE take #-}
378take :: IsStream t => Int -> t m a -> t m a
428take :: IsStream t => Int -> t m a -> t m a
379take n m = fromStream $ go n (toStream m)
429take n m = fromStream $ go n (toStream m)
@@ -383,6 +433,8 @@ take n m = fromStream $ go n (toStream m)
383 in if n1 <= 0 then stp else (S.runStream m1) ctx stp sng yield
433 in if n1 <= 0 then stp else (S.runStream m1) ctx stp sng yield
385-- | Include only those elements that pass a predicate.
435-- | Include only those elements that pass a predicate.
386{-# INLINE filter #-}
438{-# INLINE filter #-}
387filter :: IsStream t => (a -> Bool) -> t m a -> t m a
439filter :: IsStream t => (a -> Bool) -> t m a -> t m a
388filter p m = fromStream $ go (toStream m)
440filter p m = fromStream $ go (toStream m)
@@ -395,6 +447,8 @@ filter p m = fromStream $ go (toStream m)
395 in (S.runStream m1) ctx stp single yield
447 in (S.runStream m1) ctx stp single yield
397-- | End the stream as soon as the predicate fails on an element.
449-- | End the stream as soon as the predicate fails on an element.
398{-# INLINE takeWhile #-}
452{-# INLINE takeWhile #-}
399takeWhile :: IsStream t => (a -> Bool) -> t m a -> t m a
453takeWhile :: IsStream t => (a -> Bool) -> t m a -> t m a
400takeWhile p m = fromStream $ go (toStream m)
454takeWhile p m = fromStream $ go (toStream m)
@@ -407,6 +461,8 @@ takeWhile p m = fromStream $ go (toStream m)
407 in (S.runStream m1) ctx stp single yield
461 in (S.runStream m1) ctx stp single yield
409-- | Discard first 'n' elements from the stream and take the rest.
463-- | Discard first 'n' elements from the stream and take the rest.
410drop :: IsStream t => Int -> t m a -> t m a
466drop :: IsStream t => Int -> t m a -> t m a
411drop n m = fromStream $ go n (toStream m)
467drop n m = fromStream $ go n (toStream m)
@@ -420,6 +476,8 @@ drop n m = fromStream $ go n (toStream m)
421-- | Drop elements in the stream as long as the predicate succeeds and then
477-- | Drop elements in the stream as long as the predicate succeeds and then
422-- take the rest of the stream.
478-- take the rest of the stream.
423{-# INLINE dropWhile #-}
481{-# INLINE dropWhile #-}
424dropWhile :: IsStream t => (a -> Bool) -> t m a -> t m a
482dropWhile :: IsStream t => (a -> Bool) -> t m a -> t m a
425dropWhile p m = fromStream $ go (toStream m)
483dropWhile p m = fromStream $ go (toStream m)
@@ -432,6 +490,8 @@ dropWhile p m = fromStream $ go (toStream m)
432 in (S.runStream m1) ctx stp single yield
490 in (S.runStream m1) ctx stp single yield
434-- | Determine whether all elements of a stream satisfy a predicate.
492-- | Determine whether all elements of a stream satisfy a predicate.
435all :: Monad m => (a -> Bool) -> StreamT m a -> m Bool
495all :: Monad m => (a -> Bool) -> StreamT m a -> m Bool
436all p m = go (toStream m)
496all p m = go (toStream m)
@@ -443,6 +503,8 @@ all p m = go (toStream m)
443 in (S.runStream m1) Nothing (return True) single yield
503 in (S.runStream m1) Nothing (return True) single yield
445-- | Determine whether any of the elements of a stream satisfy a predicate.
505-- | Determine whether any of the elements of a stream satisfy a predicate.
446any :: Monad m => (a -> Bool) -> StreamT m a -> m Bool
508any :: Monad m => (a -> Bool) -> StreamT m a -> m Bool
447any p m = go (toStream m)
509any p m = go (toStream m)
@@ -454,14 +516,20 @@ any p m = go (toStream m)
454 in (S.runStream m1) Nothing (return False) single yield
516 in (S.runStream m1) Nothing (return False) single yield
456-- | Determine the sum of all elements of a stream of numbers
518-- | Determine the sum of all elements of a stream of numbers
457sum :: (Monad m, Num a) => StreamT m a -> m a
521sum :: (Monad m, Num a) => StreamT m a -> m a
458sum = foldl (+) 0 id
522sum = foldl (+) 0 id
460-- | Determine the product of all elements of a stream of numbers
524-- | Determine the product of all elements of a stream of numbers
461product :: (Monad m, Num a) => StreamT m a -> m a
527product :: (Monad m, Num a) => StreamT m a -> m a
462product = foldl (*) 1 id
528product = foldl (*) 1 id
464-- | Extract the first element of the stream, if any.
530-- | Extract the first element of the stream, if any.
465head :: Monad m => StreamT m a -> m (Maybe a)
533head :: Monad m => StreamT m a -> m (Maybe a)
467 let stop = return Nothing
535 let stop = return Nothing
@@ -470,6 +538,8 @@ head m =
470 in (S.runStream (toStream m)) Nothing stop single yield
538 in (S.runStream (toStream m)) Nothing stop single yield
472-- | Extract all but the first element of the stream, if any.
540-- | Extract all but the first element of the stream, if any.
473tail :: (IsStream t, Monad m) => StreamT m a -> m (Maybe (t m a))
543tail :: (IsStream t, Monad m) => StreamT m a -> m (Maybe (t m a))
475 let stop = return Nothing
545 let stop = return Nothing
@@ -478,11 +548,15 @@ tail m =
478 in (S.runStream (toStream m)) Nothing stop single yield
548 in (S.runStream (toStream m)) Nothing stop single yield
480-- | Extract the last element of the stream, if any.
550-- | Extract the last element of the stream, if any.
481{-# INLINE last #-}
553{-# INLINE last #-}
482last :: Monad m => StreamT m a -> m (Maybe a)
554last :: Monad m => StreamT m a -> m (Maybe a)
483last = foldl (\_ y -> Just y) Nothing id
555last = foldl (\_ y -> Just y) Nothing id
485-- | Determine whether the stream is empty.
557-- | Determine whether the stream is empty.
486null :: Monad m => StreamT m a -> m Bool
560null :: Monad m => StreamT m a -> m Bool
488 let stop = return True
562 let stop = return True
@@ -491,6 +565,8 @@ null m =
491 in (S.runStream (toStream m)) Nothing stop single yield
565 in (S.runStream (toStream m)) Nothing stop single yield
493-- | Determine whether an element is present in the stream.
567-- | Determine whether an element is present in the stream.
494elem :: (Monad m, Eq a) => a -> StreamT m a -> m Bool
570elem :: (Monad m, Eq a) => a -> StreamT m a -> m Bool
495elem e m = go (toStream m)
571elem e m = go (toStream m)
@@ -501,6 +577,8 @@ elem e m = go (toStream m)
501 in (S.runStream m1) Nothing stop single yield
577 in (S.runStream m1) Nothing stop single yield
503-- | Determine whether an element is not present in the stream.
579-- | Determine whether an element is not present in the stream.
504notElem :: (Monad m, Eq a) => a -> StreamT m a -> m Bool
582notElem :: (Monad m, Eq a) => a -> StreamT m a -> m Bool
505notElem e m = go (toStream m)
583notElem e m = go (toStream m)
@@ -511,11 +589,15 @@ notElem e m = go (toStream m)
511 in (S.runStream m1) Nothing stop single yield
589 in (S.runStream m1) Nothing stop single yield
513-- | Determine the length of the stream.
591-- | Determine the length of the stream.
514length :: Monad m => StreamT m a -> m Int
594length :: Monad m => StreamT m a -> m Int
515length = foldl (\n _ -> n + 1) 0 id
595length = foldl (\n _ -> n + 1) 0 id
517-- | Returns the elements of the stream in reverse order.
597-- | Returns the elements of the stream in reverse order.
518-- The stream must be finite.
598-- The stream must be finite.
519reverse :: (IsStream t) => t m a -> t m a
601reverse :: (IsStream t) => t m a -> t m a
520reverse m = fromStream $ go S.nil (toStream m)
602reverse m = fromStream $ go S.nil (toStream m)
@@ -528,6 +610,8 @@ reverse m = fromStream $ go S.nil (toStream m)
529-- XXX replace the recursive "go" with continuation
611-- XXX replace the recursive "go" with continuation
530-- | Determine the minimum element in a stream.
612-- | Determine the minimum element in a stream.
531minimum :: (Monad m, Ord a) => StreamT m a -> m (Maybe a)
615minimum :: (Monad m, Ord a) => StreamT m a -> m (Maybe a)
532minimum m = go Nothing (toStream m)
616minimum m = go Nothing (toStream m)
@@ -543,6 +627,8 @@ minimum m = go Nothing (toStream m)
544-- XXX replace the recursive "go" with continuation
628-- XXX replace the recursive "go" with continuation
545-- | Determine the maximum element in a stream.
629-- | Determine the maximum element in a stream.
546maximum :: (Monad m, Ord a) => StreamT m a -> m (Maybe a)
632maximum :: (Monad m, Ord a) => StreamT m a -> m (Maybe a)
547maximum m = go Nothing (toStream m)
633maximum m = go Nothing (toStream m)
@@ -564,6 +650,8 @@ maximum m = go Nothing (toStream m)
565-- | Replace each element of the stream with the result of a monadic action
651-- | Replace each element of the stream with the result of a monadic action
566-- applied on the element.
652-- applied on the element.
567{-# INLINE mapM #-}
655{-# INLINE mapM #-}
568mapM :: (IsStream t, Monad m) => (a -> m b) -> t m a -> t m b
656mapM :: (IsStream t, Monad m) => (a -> m b) -> t m a -> t m b
569mapM f m = fromStream $ go (toStream m)
657mapM f m = fromStream $ go (toStream m)
@@ -575,6 +663,8 @@ mapM f m = fromStream $ go (toStream m)
576-- | Apply a monadic action to each element of the stream and discard the
664-- | Apply a monadic action to each element of the stream and discard the
577-- output of the action.
665-- output of the action.
578mapM_ :: Monad m => (a -> m b) -> StreamT m a -> m ()
668mapM_ :: Monad m => (a -> m b) -> StreamT m a -> m ()
579mapM_ f m = go (toStream m)
669mapM_ f m = go (toStream m)
@@ -586,6 +676,8 @@ mapM_ f m = go (toStream m)
587-- | Reduce a stream of monadic actions to a stream of the output of those
677-- | Reduce a stream of monadic actions to a stream of the output of those
588-- actions.
678-- actions.
589sequence :: (IsStream t, Monad m) => t m (m a) -> t m a
681sequence :: (IsStream t, Monad m) => t m (m a) -> t m a
590sequence m = fromStream $ go (toStream m)
682sequence m = fromStream $ go (toStream m)
@@ -599,10 +691,14 @@ sequence m = fromStream $ go (toStream m)
599------------------------------------------------------------------------------
691------------------------------------------------------------------------------
601-- | Zip two streams serially using a pure zipping function.
693-- | Zip two streams serially using a pure zipping function.
602zipWith :: IsStream t => (a -> b -> c) -> t m a -> t m b -> t m c
696zipWith :: IsStream t => (a -> b -> c) -> t m a -> t m b -> t m c
603zipWith f m1 m2 = fromStream $ S.zipWith f (toStream m1) (toStream m2)
697zipWith f m1 m2 = fromStream $ S.zipWith f (toStream m1) (toStream m2)
605-- | Zip two streams serially using a monadic zipping function.
699-- | Zip two streams serially using a monadic zipping function.
606zipWithM :: IsStream t => (a -> b -> t m c) -> t m a -> t m b -> t m c
702zipWithM :: IsStream t => (a -> b -> t m c) -> t m a -> t m b -> t m c
607zipWithM f m1 m2 = fromStream $ go (toStream m1) (toStream m2)
703zipWithM f m1 m2 = fromStream $ go (toStream m1) (toStream m2)
@@ -622,11 +718,15 @@ zipWithM f m1 m2 = fromStream $ go (toStream m1) (toStream m2)
623-- | Zip two streams concurrently (i.e. both the elements being zipped are
719-- | Zip two streams concurrently (i.e. both the elements being zipped are
624-- generated concurrently) using a pure zipping function.
720-- generated concurrently) using a pure zipping function.
625zipParallelWith :: (IsStream t, MonadParallel m)
723zipParallelWith :: (IsStream t, MonadParallel m)
626 => (a -> b -> c) -> t m a -> t m b -> t m c
724 => (a -> b -> c) -> t m a -> t m b -> t m c
627zipParallelWith f m1 m2 =
725zipParallelWith f m1 m2 =
628 fromStream $ S.zipParallelWith f (toStream m1) (toStream m2)
726 fromStream $ S.zipParallelWith f (toStream m1) (toStream m2)
630{-# DEPRECATED zipAsyncWith "Please use zipParallelWith instead." #-}
730{-# DEPRECATED zipAsyncWith "Please use zipParallelWith instead." #-}
631zipAsyncWith :: (IsStream t, MonadParallel m)
731zipAsyncWith :: (IsStream t, MonadParallel m)
632 => (a -> b -> c) -> t m a -> t m b -> t m c
732 => (a -> b -> c) -> t m a -> t m b -> t m c
@@ -634,6 +734,8 @@ zipAsyncWith = zipParallelWith
635-- | Zip two streams asyncly (i.e. both the elements being zipped are generated
735-- | Zip two streams asyncly (i.e. both the elements being zipped are generated
636-- concurrently) using a monadic zipping function.
736-- concurrently) using a monadic zipping function.
637zipParallelWithM :: (IsStream t, MonadParallel m)
739zipParallelWithM :: (IsStream t, MonadParallel m)
638 => (a -> b -> t m c) -> t m a -> t m b -> t m c
740 => (a -> b -> t m c) -> t m a -> t m b -> t m c
639zipParallelWithM f m1 m2 = fromStream $ Stream $ \_ stp sng yld -> do
741zipParallelWithM f m1 m2 = fromStream $ Stream $ \_ stp sng yld -> do
@@ -641,6 +743,8 @@ zipParallelWithM f m1 m2 = fromStream $ Stream $ \_ stp sng yld -> do
641 mb <- async m2
743 mb <- async m2
642 (S.runStream (toStream (zipWithM f ma mb))) Nothing stp sng yld
744 (S.runStream (toStream (zipWithM f ma mb))) Nothing stp sng yld
644{-# DEPRECATED zipAsyncWithM "Please use zipParallelWithM instead." #-}
748{-# DEPRECATED zipAsyncWithM "Please use zipParallelWithM instead." #-}
645zipAsyncWithM :: (IsStream t, MonadParallel m)
749zipAsyncWithM :: (IsStream t, MonadParallel m)
646 => (a -> b -> t m c) -> t m a -> t m b -> t m c
750 => (a -> b -> t m c) -> t m a -> t m b -> t m c