@@ -114,20 +114,20 @@ import Streamly.Streams
114unfoldr :: IsStream t => (b -> Maybe (a, b)) -> b -> t m a
114unfoldr :: IsStream t => (b -> Maybe (a, b)) -> b -> t m a
115unfoldr step = fromStream . go
115unfoldr step = fromStream . go
117− go s = Stream $ \_ stp yld ->
117+ go s = Stream $ \_ stp _ yld ->
118 case step s of
118 case step s of
119 Nothing -> stp
119 Nothing -> stp
120− Just (a, b) -> yld a (Just (go b))
120+ Just (a, b) -> yld a (go b)
122-- | Build a Stream by unfolding monadic steps starting from a seed.
122-- | Build a Stream by unfolding monadic steps starting from a seed.
123unfoldrM :: (IsStream t, Monad m) => (b -> m (Maybe (a, b))) -> b -> t m a
123unfoldrM :: (IsStream t, Monad m) => (b -> m (Maybe (a, b))) -> b -> t m a
124unfoldrM step = fromStream . go
124unfoldrM step = fromStream . go
126− go s = Stream $ \_ stp yld -> do
126+ go s = Stream $ \_ stp _ yld -> do
127 mayb <- step s
127 mayb <- step s
128 case mayb of
128 case mayb of
129 Nothing -> stp
129 Nothing -> stp
130− Just (a, b) -> yld a (Just (go b))
130+ Just (a, b) -> yld a (go b)
132-- | Construct a stream from a 'Foldable' container.
132-- | Construct a stream from a 'Foldable' container.
133{-# INLINE fromFoldable #-}
133{-# INLINE fromFoldable #-}
@@ -144,27 +144,27 @@ each = fromFoldable
144iterate :: IsStream t => (a -> a) -> a -> t m a
144iterate :: IsStream t => (a -> a) -> a -> t m a
145iterate step = fromStream . go
145iterate step = fromStream . go
147− go s = S.cons s (Just (go (step s)))
147+ go s = S.cons s (go (step s))
149-- | Iterate a monadic function from a seed value, streaming the results forever
149-- | Iterate a monadic function from a seed value, streaming the results forever
150iterateM :: (IsStream t, Monad m) => (a -> m a) -> a -> t m a
150iterateM :: (IsStream t, Monad m) => (a -> m a) -> a -> t m a
151iterateM step = fromStream . go
151iterateM step = fromStream . go
153− go s = Stream $ \_ _ yld -> do
153+ go s = Stream $ \_ _ _ yld -> do
154 a <- step s
154 a <- step s
155− yld s (Just (go a))
155+ yld s (go a)
157-- | Read lines from an IO Handle into a stream of Strings.
157-- | Read lines from an IO Handle into a stream of Strings.
158fromHandle :: (IsStream t, MonadIO m) => IO.Handle -> t m String
158fromHandle :: (IsStream t, MonadIO m) => IO.Handle -> t m String
159fromHandle h = fromStream go
159fromHandle h = fromStream go
161− go = Stream $ \_ stp yld -> do
161+ go = Stream $ \_ stp _ yld -> do
162 eof <- liftIO $ IO.hIsEOF h
162 eof <- liftIO $ IO.hIsEOF h
166 str <- liftIO $ IO.hGetLine h
166 str <- liftIO $ IO.hGetLine h
167− yld str (Just go)
167+ yld str go
169------------------------------------------------------------------------------
169------------------------------------------------------------------------------
170-- Elimination
170-- Elimination
@@ -181,9 +181,9 @@ foldr step acc m = go (toStream m)
183 let stop = return acc
183 let stop = return acc
184− yield a Nothing = return (step a acc)
184+ single a = return (step a acc)
185− yield a (Just x) = go x >>= \b -> return (step a b)
185+ yield a r = go r >>= \b -> return (step a b)
186− in (S.runStream m1) Nothing stop yield
186+ in (S.runStream m1) Nothing stop single yield
188-- | Lazy right fold with a monadic step function. For example, to fold a
188-- | Lazy right fold with a monadic step function. For example, to fold a
189-- stream into a list:
189-- stream into a list:
@@ -198,9 +198,9 @@ foldrM step acc m = go (toStream m)
200 let stop = return acc
200 let stop = return acc
201− yield a Nothing = step a acc
201+ single a = step a acc
202− yield a (Just x) = (go x) >>= (step a)
202+ yield a r = go r >>= step a
203− in (S.runStream m1) Nothing stop yield
203+ in (S.runStream m1) Nothing stop single yield
205-- | Strict left scan with an extraction function. Like 'scanl'', but applies a
205-- | Strict left scan with an extraction function. Like 'scanl'', but applies a
206-- user supplied extraction function (the third argument) at each step. This is
206-- user supplied extraction function (the third argument) at each step. This is
@@ -210,13 +210,12 @@ foldrM step acc m = go (toStream m)
210scanx :: IsStream t => (x -> a -> x) -> x -> (x -> b) -> t m a -> t m b
210scanx :: IsStream t => (x -> a -> x) -> x -> (x -> b) -> t m a -> t m b
211scanx step begin done m = cons (done begin) $ fromStream $ go (toStream m) begin
211scanx step begin done m = cons (done begin) $ fromStream $ go (toStream m) begin
213− go m1 !acc = Stream $ \_ stp yld ->
213+ go m1 !acc = Stream $ \_ stp sng yld ->
215− yield a Nothing = yld (done $ step acc a) Nothing
214+ let single a = sng (done $ step acc a)
216− yield a (Just x) =
215+ yield a r =
217 let s = step acc a
216 let s = step acc a
218− in yld (done s) (Just (go x s))
217+ in yld (done s) (go r s)
219− in S.runStream m1 Nothing stop yield
218+ in S.runStream m1 Nothing stp single yield
221{-# DEPRECATED scan "Please use scanx instead." #-}
220{-# DEPRECATED scan "Please use scanx instead." #-}
222scan :: IsStream t => (x -> a -> x) -> x -> (x -> b) -> t m a -> t m b
221scan :: IsStream t => (x -> a -> x) -> x -> (x -> b) -> t m a -> t m b
@@ -240,22 +239,20 @@ foldx step begin done m = get $ go (toStream m) begin
241 {-# NOINLINE get #-}
240 {-# NOINLINE get #-}
243− let yield a Nothing = return $ done a
242+ let single = return . done
244− yield _ _ = undefined
245− in (S.runStream m1) Nothing undefined yield
243+ in (S.runStream m1) Nothing undefined single undefined
247 -- Note, this can be implemented by making a recursive call to "go",
245 -- Note, this can be implemented by making a recursive call to "go",
248 -- however that is more expensive because of unnecessary recursion
246 -- however that is more expensive because of unnecessary recursion
249 -- that cannot be tail call optimized. Unfolding recursion explicitly via
247 -- that cannot be tail call optimized. Unfolding recursion explicitly via
250 -- continuations is much more efficient.
248 -- continuations is much more efficient.
251− go m1 !acc = Stream $ \_ _ yld ->
249+ go m1 !acc = Stream $ \_ _ sng yld ->
252− let stop = yld acc Nothing
250+ let stop = sng acc
251+ single a = sng $ step acc a
253 yield a r =
252 yield a r =
254− let s = step acc a
253+ let stream = go r (step acc a)
256− Nothing -> yld s Nothing
257− Just x -> (S.runStream (go x s)) Nothing undefined yld
254+ in (S.runStream stream) Nothing undefined sng yld
258− in (S.runStream m1) Nothing stop yield
255+ in (S.runStream m1) Nothing stop single yield
260{-# DEPRECATED foldl "Please use foldx instead." #-}
257{-# DEPRECATED foldl "Please use foldx instead." #-}
261foldl :: Monad m => (x -> a -> x) -> x -> (x -> b) -> SerialT m a -> m b
258foldl :: Monad m => (x -> a -> x) -> x -> (x -> b) -> SerialT m a -> m b
@@ -273,9 +270,9 @@ foldxM step begin done m = go begin (toStream m)
274 go !acc m1 =
271 go !acc m1 =
275 let stop = acc >>= done
272 let stop = acc >>= done
276− yield a Nothing = acc >>= \b -> step b a >>= done
273+ single a = acc >>= \b -> step b a >>= done
277− yield a (Just x) = acc >>= \b -> go (step b a) x
274+ yield a r = acc >>= \b -> go (step b a) r
278− in (S.runStream m1) Nothing stop yield
275+ in (S.runStream m1) Nothing stop single yield
280{-# DEPRECATED foldlM "Please use foldxM instead." #-}
277{-# DEPRECATED foldlM "Please use foldxM instead." #-}
281foldlM :: Monad m => (x -> a -> m x) -> m x -> (x -> m b) -> SerialT m a -> m b
278foldlM :: Monad m => (x -> a -> m x) -> m x -> (x -> m b) -> SerialT m a -> m b
@@ -291,9 +288,9 @@ foldlM' step begin m = foldxM step (return begin) return m
291uncons :: (IsStream t, Monad m) => SerialT m a -> m (Maybe (a, t m a))
288uncons :: (IsStream t, Monad m) => SerialT m a -> m (Maybe (a, t m a))
292uncons m =
289uncons m =
293 let stop = return Nothing
290 let stop = return Nothing
294− yield a Nothing = return (Just (a, nil))
291+ single a = return (Just (a, nil))
295− yield a (Just x) = return (Just (a, fromStream x))
292+ yield a r = return (Just (a, fromStream r))
296− in (S.runStream (toStream m)) Nothing stop yield
293+ in (S.runStream (toStream m)) Nothing stop single yield
298-- | Write a stream of Strings to an IO Handle.
295-- | Write a stream of Strings to an IO Handle.
299toHandle :: MonadIO m => IO.Handle -> SerialT m String -> m ()
296toHandle :: MonadIO m => IO.Handle -> SerialT m String -> m ()
@@ -301,9 +298,9 @@ toHandle h m = go (toStream m)
303 let stop = return ()
300 let stop = return ()
304− yield a Nothing = liftIO (IO.hPutStrLn h a)
301+ single a = liftIO (IO.hPutStrLn h a)
305− yield a (Just x) = liftIO (IO.hPutStrLn h a) >> go x
302+ yield a r = liftIO (IO.hPutStrLn h a) >> go r
306− in (S.runStream m1) Nothing stop yield
303+ in (S.runStream m1) Nothing stop single yield
308------------------------------------------------------------------------------
305------------------------------------------------------------------------------
309-- Special folds
306-- Special folds
@@ -319,46 +316,45 @@ toList = foldrM (\a xs -> return (a : xs)) []
319take :: IsStream t => Int -> t m a -> t m a
316take :: IsStream t => Int -> t m a -> t m a
320take n m = fromStream $ go n (toStream m)
317take n m = fromStream $ go n (toStream m)
322− go n1 m1 = Stream $ \ctx stp yld ->
319+ go n1 m1 = Stream $ \ctx stp sng yld ->
323− let yield a Nothing = yld a Nothing
324− yield a (Just x) = yld a (Just (go (n1 - 1) x))
320+ let yield a r = yld a (go (n1 - 1) r)
325− in if n1 <= 0 then stp else (S.runStream m1) ctx stp yield
321+ in if n1 <= 0 then stp else (S.runStream m1) ctx stp sng yield
327-- | Include only those elements that pass a predicate.
323-- | Include only those elements that pass a predicate.
328{-# INLINE filter #-}
324{-# INLINE filter #-}
329filter :: IsStream t => (a -> Bool) -> t m a -> t m a
325filter :: IsStream t => (a -> Bool) -> t m a -> t m a
330filter p m = fromStream $ go (toStream m)
326filter p m = fromStream $ go (toStream m)
332− go m1 = Stream $ \ctx stp yld ->
328+ go m1 = Stream $ \ctx stp sng yld ->
333− let yield a Nothing | p a = yld a Nothing
329+ let single a | p a = sng a
334− | otherwise = stp
330+ | otherwise = stp
335− yield a (Just x) | p a = yld a (Just (go x))
331+ yield a r | p a = yld a (go r)
336− | otherwise = (S.runStream x) ctx stp yield
332+ | otherwise = (S.runStream r) ctx stp single yield
337− in (S.runStream m1) ctx stp yield
333+ in (S.runStream m1) ctx stp single yield
339-- | End the stream as soon as the predicate fails on an element.
335-- | End the stream as soon as the predicate fails on an element.
340{-# INLINE takeWhile #-}
336{-# INLINE takeWhile #-}
341takeWhile :: IsStream t => (a -> Bool) -> t m a -> t m a
337takeWhile :: IsStream t => (a -> Bool) -> t m a -> t m a
342takeWhile p m = fromStream $ go (toStream m)
338takeWhile p m = fromStream $ go (toStream m)
344− go m1 = Stream $ \ctx stp yld ->
340+ go m1 = Stream $ \ctx stp sng yld ->
345− let yield a Nothing | p a = yld a Nothing
341+ let single a | p a = sng a
346− | otherwise = stp
342+ | otherwise = stp
347− yield a (Just x) | p a = yld a (Just (go x))
343+ yield a r | p a = yld a (go r)
348− | otherwise = stp
344+ | otherwise = stp
349− in (S.runStream m1) ctx stp yield
345+ in (S.runStream m1) ctx stp single yield
351-- | Discard first 'n' elements from the stream and take the rest.
347-- | Discard first 'n' elements from the stream and take the rest.
352drop :: IsStream t => Int -> t m a -> t m a
348drop :: IsStream t => Int -> t m a -> t m a
353drop n m = fromStream $ go n (toStream m)
349drop n m = fromStream $ go n (toStream m)
355− go n1 m1 = Stream $ \ctx stp yld ->
351+ go n1 m1 = Stream $ \ctx stp sng yld ->
356− let yield _ Nothing = stp
352+ let single _ = stp
357− yield _ (Just x) = (S.runStream $ go (n1 - 1) x) ctx stp yld
353+ yield _ r = (S.runStream $ go (n1 - 1) r) ctx stp sng yld
358 -- Somehow "<=" check performs better than a ">"
354 -- Somehow "<=" check performs better than a ">"
359 in if n1 <= 0
355 in if n1 <= 0
360− then (S.runStream m1) ctx stp yld
356+ then (S.runStream m1) ctx stp sng yld
361− else (S.runStream m1) ctx stp yield
357+ else (S.runStream m1) ctx stp single yield
363-- | Drop elements in the stream as long as the predicate succeeds and then
359-- | Drop elements in the stream as long as the predicate succeeds and then
364-- take the rest of the stream.
360-- take the rest of the stream.
@@ -366,34 +362,34 @@ drop n m = fromStream $ go n (toStream m)
366dropWhile :: IsStream t => (a -> Bool) -> t m a -> t m a
362dropWhile :: IsStream t => (a -> Bool) -> t m a -> t m a
367dropWhile p m = fromStream $ go (toStream m)
363dropWhile p m = fromStream $ go (toStream m)
369− go m1 = Stream $ \ctx stp yld ->
365+ go m1 = Stream $ \ctx stp sng yld ->
370− let yield a Nothing | p a = stp
366+ let single a | p a = stp
371− | otherwise = yld a Nothing
367+ | otherwise = sng a
372− yield a (Just x) | p a = (S.runStream x) ctx stp yield
368+ yield a r | p a = (S.runStream r) ctx stp single yield
373− | otherwise = yld a (Just x)
369+ | otherwise = yld a r
374− in (S.runStream m1) ctx stp yield
370+ in (S.runStream m1) ctx stp single yield
376-- | Determine whether all elements of a stream satisfy a predicate.
372-- | Determine whether all elements of a stream satisfy a predicate.
377all :: Monad m => (a -> Bool) -> SerialT m a -> m Bool
373all :: Monad m => (a -> Bool) -> SerialT m a -> m Bool
378all p m = go (toStream m)
374all p m = go (toStream m)
381− let yield a Nothing | p a = return True
377+ let single a | p a = return True
382− | otherwise = return False
378+ | otherwise = return False
383− yield a (Just x) | p a = go x
379+ yield a r | p a = go r
384− | otherwise = return False
380+ | otherwise = return False
385− in (S.runStream m1) Nothing (return True) yield
381+ in (S.runStream m1) Nothing (return True) single yield
387-- | Determine whether any of the elements of a stream satisfy a predicate.
383-- | Determine whether any of the elements of a stream satisfy a predicate.
388any :: Monad m => (a -> Bool) -> SerialT m a -> m Bool
384any :: Monad m => (a -> Bool) -> SerialT m a -> m Bool
389any p m = go (toStream m)
385any p m = go (toStream m)
392− let yield a Nothing | p a = return True
388+ let single a | p a = return True
393− | otherwise = return False
389+ | otherwise = return False
394− yield a (Just x) | p a = return True
390+ yield a r | p a = return True
395− | otherwise = go x
391+ | otherwise = go r
396− in (S.runStream m1) Nothing (return False) yield
392+ in (S.runStream m1) Nothing (return False) single yield
398-- | Determine the sum of all elements of a stream of numbers
394-- | Determine the sum of all elements of a stream of numbers
399sum :: (Monad m, Num a) => SerialT m a -> m a
395sum :: (Monad m, Num a) => SerialT m a -> m a
@@ -407,16 +403,17 @@ product = foldl (*) 1 id
407head :: Monad m => SerialT m a -> m (Maybe a)
403head :: Monad m => SerialT m a -> m (Maybe a)
409 let stop = return Nothing
405 let stop = return Nothing
406+ single a = return (Just a)
410 yield a _ = return (Just a)
407 yield a _ = return (Just a)
411− in (S.runStream (toStream m)) Nothing stop yield
408+ in (S.runStream (toStream m)) Nothing stop single yield
413-- | Extract all but the first element of the stream, if any.
410-- | Extract all but the first element of the stream, if any.
414tail :: (IsStream t, Monad m) => SerialT m a -> m (Maybe (t m a))
411tail :: (IsStream t, Monad m) => SerialT m a -> m (Maybe (t m a))
416− let stop = return Nothing
413+ let stop = return Nothing
417− yield _ Nothing = return $ Just nil
414+ single _ = return $ Just nil
418− yield _ (Just t) = return $ Just $ fromStream t
415+ yield _ r = return $ Just $ fromStream r
419− in (S.runStream (toStream m)) Nothing stop yield
416+ in (S.runStream (toStream m)) Nothing stop single yield
421-- | Extract the last element of the stream, if any.
418-- | Extract the last element of the stream, if any.
422{-# INLINE last #-}
419{-# INLINE last #-}
@@ -427,28 +424,29 @@ last = foldl (\_ y -> Just y) Nothing id
427null :: Monad m => SerialT m a -> m Bool
424null :: Monad m => SerialT m a -> m Bool
429 let stop = return True
426 let stop = return True
427+ single _ = return False
430 yield _ _ = return False
428 yield _ _ = return False
431− in (S.runStream (toStream m)) Nothing stop yield
429+ in (S.runStream (toStream m)) Nothing stop single yield
433-- | Determine whether an element is present in the stream.
431-- | Determine whether an element is present in the stream.
434elem :: (Monad m, Eq a) => a -> SerialT m a -> m Bool
432elem :: (Monad m, Eq a) => a -> SerialT m a -> m Bool
435elem e m = go (toStream m)
433elem e m = go (toStream m)
438− let stop = return False
436+ let stop = return False
439− yield a Nothing = return (a == e)
437+ single a = return (a == e)
440− yield a (Just x) = if a == e then return True else go x
438+ yield a r = if a == e then return True else go r
441− in (S.runStream m1) Nothing stop yield
439+ in (S.runStream m1) Nothing stop single yield
443-- | Determine whether an element is not present in the stream.
441-- | Determine whether an element is not present in the stream.
444notElem :: (Monad m, Eq a) => a -> SerialT m a -> m Bool
442notElem :: (Monad m, Eq a) => a -> SerialT m a -> m Bool
445notElem e m = go (toStream m)
443notElem e m = go (toStream m)
448− let stop = return True
446+ let stop = return True
449− yield a Nothing = return (a /= e)
447+ single a = return (a /= e)
450− yield a (Just x) = if a == e then return False else go x
448+ yield a r = if a == e then return False else go r
451− in (S.runStream m1) Nothing stop yield
449+ in (S.runStream m1) Nothing stop single yield
453-- | Determine the length of the stream.
451-- | Determine the length of the stream.
454length :: Monad m => SerialT m a -> m Int
452length :: Monad m => SerialT m a -> m Int
@@ -457,28 +455,27 @@ length = foldl (\n _ -> n + 1) 0 id
457-- | Returns the elements of the stream in reverse order.
455-- | Returns the elements of the stream in reverse order.
458-- The stream must be finite.
456-- The stream must be finite.
459reverse :: (IsStream t) => t m a -> t m a
457reverse :: (IsStream t) => t m a -> t m a
460−reverse m = fromStream $ go Nothing (toStream m)
458+reverse m = fromStream $ go S.nil (toStream m)
462− go rev rest = Stream $ \svr stp yld ->
460+ go rev rest = Stream $ \svr stp sng yld ->
463− let stop = case rev of
465− Just str -> S.runStream str svr stp yld
461+ let run x = S.runStream x svr stp sng yld
466− yield a Nothing = S.runStream (a `S.cons` rev) svr stp yld
462+ stop = run rev
463+ single a = run $ a `S.cons` rev
467− yield a (Just x) = S.runStream (go (Just $ a `S.cons` rev) x) svr stp yld
464+ yield a r = run $ go (a `S.cons` rev) r
468− in S.runStream rest svr stop yield
465+ in S.runStream rest svr stop single yield
470-- XXX replace the recursive "go" with continuation
467-- XXX replace the recursive "go" with continuation
471-- | Determine the minimum element in a stream.
468-- | Determine the minimum element in a stream.
472minimum :: (Monad m, Ord a) => SerialT m a -> m (Maybe a)
469minimum :: (Monad m, Ord a) => SerialT m a -> m (Maybe a)
473minimum m = go Nothing (toStream m)
470minimum m = go Nothing (toStream m)
475− go r m1 =
472+ go res m1 =
476− let stop = return r
473+ let stop = return res
477− yield a Nothing = return $ min_ a r
474+ single a = return $ min_ a res
478− yield a (Just x) = go (min_ a r) x
475+ yield a r = go (min_ a res) r
479− in (S.runStream m1) Nothing stop yield
476+ in (S.runStream m1) Nothing stop single yield
481− min_ a r = case r of
478+ min_ a res = case res of
482 Nothing -> Just a
479 Nothing -> Just a
483 Just e -> Just $ min a e
480 Just e -> Just $ min a e
@@ -487,13 +484,13 @@ minimum m = go Nothing (toStream m)
487maximum :: (Monad m, Ord a) => SerialT m a -> m (Maybe a)
484maximum :: (Monad m, Ord a) => SerialT m a -> m (Maybe a)
488maximum m = go Nothing (toStream m)
485maximum m = go Nothing (toStream m)
490− go r m1 =
487+ go res m1 =
491− let stop = return r
488+ let stop = return res
492− yield a Nothing = return $ max_ a r
489+ single a = return $ max_ a res
493− yield a (Just x) = go (max_ a r) x
490+ yield a r = go (max_ a res) r
494− in (S.runStream m1) Nothing stop yield
491+ in (S.runStream m1) Nothing stop single yield
496− max_ a r = case r of
493+ max_ a res = case res of
497 Nothing -> Just a
494 Nothing -> Just a
498 Just e -> Just $ max a e
495 Just e -> Just $ max a e
@@ -509,11 +506,10 @@ maximum m = go Nothing (toStream m)
509mapM :: (IsStream t, Monad m) => (a -> m b) -> t m a -> t m b
506mapM :: (IsStream t, Monad m) => (a -> m b) -> t m a -> t m b
510mapM f m = fromStream $ go (toStream m)
507mapM f m = fromStream $ go (toStream m)
512− go m1 = Stream $ \_ stp yld ->
509+ go m1 = Stream $ \_ stp sng yld ->
513− let stop = stp
510+ let single a = f a >>= sng
514− yield a Nothing = f a >>= \b -> yld b Nothing
515− yield a (Just x) = f a >>= \b -> yld b (Just (go x))
511+ yield a r = f a >>= \b -> yld b (go r)
516− in (S.runStream m1) Nothing stop yield
512+ in (S.runStream m1) Nothing stp single yield
518-- | Apply a monadic action to each element of the stream and discard the
514-- | Apply a monadic action to each element of the stream and discard the
519-- output of the action.
515-- output of the action.
@@ -522,29 +518,28 @@ mapM_ f m = go (toStream m)
524 let stop = return ()
520 let stop = return ()
525− yield a Nothing = void (f a)
521+ single a = void (f a)
526− yield a (Just x) = f a >> go x
522+ yield a r = f a >> go r
527− in (S.runStream m1) Nothing stop yield
523+ in (S.runStream m1) Nothing stop single yield
529-- | Reduce a stream of monadic actions to a stream of the output of those
525-- | Reduce a stream of monadic actions to a stream of the output of those
530-- actions.
526-- actions.
531sequence :: (IsStream t, Monad m) => t m (m a) -> t m a
527sequence :: (IsStream t, Monad m) => t m (m a) -> t m a
532sequence m = fromStream $ go (toStream m)
528sequence m = fromStream $ go (toStream m)
534− go m1 = Stream $ \_ stp yld ->
530+ go m1 = Stream $ \_ stp sng yld ->
535− let stop = stp
531+ let single ma = ma >>= sng
536− yield a Nothing = a >>= \b -> yld b Nothing
537− yield a (Just x) = a >>= \b -> yld b (Just (go x))
532+ yield ma r = ma >>= \b -> yld b (go r)
538− in (S.runStream m1) Nothing stop yield
533+ in (S.runStream m1) Nothing stp single yield
540-- | Generate a stream by performing an action @n@ times.
535-- | Generate a stream by performing an action @n@ times.
541replicateM :: (IsStream t, Monad m) => Int -> m a -> t m a
536replicateM :: (IsStream t, Monad m) => Int -> m a -> t m a
542replicateM n m = fromStream $ go n
537replicateM n m = fromStream $ go n
544− go cnt = Stream $ \_ stp yld ->
539+ go cnt = Stream $ \_ stp _ yld ->
545 if cnt <= 0
540 if cnt <= 0
547− else m >>= \a -> yld a (Just $ go (cnt - 1))
542+ else m >>= \a -> yld a (go (cnt - 1))
549------------------------------------------------------------------------------
544------------------------------------------------------------------------------
550-- Serially Zipping Streams
545-- Serially Zipping Streams
@@ -554,16 +549,15 @@ replicateM n m = fromStream $ go n
554zipWithM :: IsStream t => (a -> b -> t m c) -> t m a -> t m b -> t m c
549zipWithM :: IsStream t => (a -> b -> t m c) -> t m a -> t m b -> t m c
555zipWithM f m1 m2 = fromStream $ go (toStream m1) (toStream m2)
550zipWithM f m1 m2 = fromStream $ go (toStream m1) (toStream m2)
557− go mx my = Stream $ \_ stp yld -> do
552+ go mx my = Stream $ \_ stp sng yld -> do
558 let merge a ra =
553 let merge a ra =
559− let yield2 b Nothing = (S.runStream (g a b)) Nothing stp yld
554+ let run x = S.runStream x Nothing stp sng yld
560− yield2 b (Just rb) =
555+ single2 b = run $ toStream (f a b)
561− (S.runStream (g a b <> go ra rb)) Nothing stp yld
556+ yield2 b rb = run $ toStream (f a b) <> go ra rb
562− in (S.runStream my) Nothing stp yield2
557+ in (S.runStream my) Nothing stp single2 yield2
563− let yield1 a Nothing = merge a S.nil
558+ let single1 a = merge a S.nil
564− yield1 a (Just ra) = merge a ra
559+ yield1 a ra = merge a ra
565− (S.runStream mx) Nothing stp yield1
560+ (S.runStream mx) Nothing stp single1 yield1
566− g a b = toStream $ f a b
568------------------------------------------------------------------------------
562------------------------------------------------------------------------------
569-- Parallely Zipping Streams
563-- Parallely Zipping Streams
@@ -573,10 +567,10 @@ zipWithM f m1 m2 = fromStream $ go (toStream m1) (toStream m2)
573-- concurrently) using a monadic zipping function.
567-- concurrently) using a monadic zipping function.
574zipParallelWithM :: (IsStream t, MonadParallel m)
568zipParallelWithM :: (IsStream t, MonadParallel m)
575 => (a -> b -> t m c) -> t m a -> t m b -> t m c
569 => (a -> b -> t m c) -> t m a -> t m b -> t m c
576−zipParallelWithM f m1 m2 = fromStream $ Stream $ \_ stp yld -> do
570+zipParallelWithM f m1 m2 = fromStream $ Stream $ \_ stp sng yld -> do
577 ma <- async m1
571 ma <- async m1
578 mb <- async m2
572 mb <- async m2
579− (S.runStream (toStream (zipWithM f ma mb))) Nothing stp yld
573+ (S.runStream (toStream (zipWithM f ma mb))) Nothing stp sng yld
581{-# DEPRECATED zipAsyncWithM "Please use zipParallelWithM instead." #-}
575{-# DEPRECATED zipAsyncWithM "Please use zipParallelWithM instead." #-}
582zipAsyncWithM :: (IsStream t, MonadParallel m)
576zipAsyncWithM :: (IsStream t, MonadParallel m)