@@ -53,7 +53,7 @@ module Streamly.Prelude
53 -- * Special Generation
53 -- * Special Generation
54 -- | Generate a monadic stream from an input structure, a seed or a
54 -- | Generate a monadic stream from an input structure, a seed or a
55 -- generation function.
55 -- generation function.
57 , replicateM
57 , replicateM
@@ -121,6 +121,7 @@ module Streamly.Prelude
121 , toHandle
121 , toHandle
123 -- * Deprecated
123 -- * Deprecated
@@ -217,7 +218,7 @@ unfoldrM step = go
217 case mayb of
218 case mayb of
218 Nothing -> stp
219 Nothing -> stp
219 Just (a, b) ->
220 Just (a, b) ->
220− K.runStream (toStream (return a |: go b)) svr stp sng yld
221+ K.unStream (toStream (return a |: go b)) svr stp sng yld
222-- | Construct a stream from a 'Foldable' containing pure values.
223-- | Construct a stream from a 'Foldable' containing pure values.
@@ -310,7 +311,7 @@ iterateM step = go
311 go s = fromStream $ K.Stream $ \svr stp sng yld -> do
312 go s = fromStream $ K.Stream $ \svr stp sng yld -> do
312 next <- step s
313 next <- step s
313− K.runStream (toStream (return s |: go next)) svr stp sng yld
314+ K.unStream (toStream (return s |: go next)) svr stp sng yld
315-- | Read lines from an IO Handle into a stream of Strings.
316-- | Read lines from an IO Handle into a stream of Strings.
@@ -344,8 +345,8 @@ foldr step acc m = go (toStream m)
345 let stop = return acc
346 let stop = return acc
346 single a = return (step a acc)
347 single a = return (step a acc)
347− yield a r = go r >>= \b -> return (step a b)
348+ yieldk a r = go r >>= \b -> return (step a b)
348− in (K.runStream m1) Nothing stop single yield
349+ in (K.unStream m1) Nothing stop single yieldk
350-- | Lazy right fold with a monadic step function. For example, to fold a
351-- | Lazy right fold with a monadic step function. For example, to fold a
351-- stream into a list:
352-- stream into a list:
@@ -363,8 +364,8 @@ foldrM step acc m = go (toStream m)
364 let stop = return acc
365 let stop = return acc
365 single a = step a acc
366 single a = step a acc
366− yield a r = go r >>= step a
367+ yieldk a r = go r >>= step a
367− in (K.runStream m1) Nothing stop single yield
368+ in (K.unStream m1) Nothing stop single yieldk
369-- | Strict left scan with an extraction function. Like 'scanl'', but applies a
370-- | Strict left scan with an extraction function. Like 'scanl'', but applies a
370-- user supplied extraction function (the third argument) at each step. This is
371-- user supplied extraction function (the third argument) at each step. This is
@@ -379,10 +380,10 @@ scanx step begin done m =
380 go m1 !acc = K.Stream $ \_ stp sng yld ->
381 go m1 !acc = K.Stream $ \_ stp sng yld ->
381 let single a = sng (done $ step acc a)
382 let single a = sng (done $ step acc a)
382− yield a r =
383+ yieldk a r =
383 let s = step acc a
384 let s = step acc a
384 in yld (done s) (go r s)
385 in yld (done s) (go r s)
385− in K.runStream m1 Nothing stp single yield
386+ in K.unStream m1 Nothing stp single yieldk
388-- @since 0.1.1
389-- @since 0.1.1
@@ -413,7 +414,7 @@ foldx step begin done m = get $ go (toStream m) begin
413 {-# NOINLINE get #-}
414 {-# NOINLINE get #-}
415 let single = return . done
416 let single = return . done
416− in (K.runStream m1) Nothing undefined single undefined
417+ in (K.unStream m1) Nothing undefined single undefined
418 -- Note, this can be implemented by making a recursive call to "go",
419 -- Note, this can be implemented by making a recursive call to "go",
419 -- however that is more expensive because of unnecessary recursion
420 -- however that is more expensive because of unnecessary recursion
@@ -422,10 +423,10 @@ foldx step begin done m = get $ go (toStream m) begin
422 go m1 !acc = K.Stream $ \_ _ sng yld ->
423 go m1 !acc = K.Stream $ \_ _ sng yld ->
423 let stop = sng acc
424 let stop = sng acc
424 single a = sng $ step acc a
425 single a = sng $ step acc a
425− yield a r =
426+ yieldk a r =
426 let stream = go r (step acc a)
427 let stream = go r (step acc a)
427− in (K.runStream stream) Nothing undefined sng yld
428+ in (K.unStream stream) Nothing undefined sng yld
428− in (K.runStream m1) Nothing stop single yield
429+ in (K.unStream m1) Nothing stop single yieldk
431-- @since 0.1.0
432-- @since 0.1.0
@@ -450,8 +451,8 @@ foldxM step begin done m = go begin (toStream m)
450 go !acc m1 =
451 go !acc m1 =
451 let stop = acc >>= done
452 let stop = acc >>= done
452 single a = acc >>= \b -> step b a >>= done
453 single a = acc >>= \b -> step b a >>= done
453− yield a r = acc >>= \b -> go (step b a) r
454+ yieldk a r = acc >>= \b -> go (step b a) r
454− in (K.runStream m1) Nothing stop single yield
455+ in (K.unStream m1) Nothing stop single yieldk
457-- @since 0.1.0
458-- @since 0.1.0
@@ -474,8 +475,8 @@ uncons :: (IsStream t, Monad m) => SerialT m a -> m (Maybe (a, t m a))
474uncons m =
475uncons m =
475 let stop = return Nothing
476 let stop = return Nothing
476 single a = return (Just (a, nil))
477 single a = return (Just (a, nil))
477− yield a r = return (Just (a, fromStream r))
478+ yieldk a r = return (Just (a, fromStream r))
478− in (K.runStream (toStream m)) Nothing stop single yield
479+ in (K.unStream (toStream m)) Nothing stop single yieldk
480-- | Write a stream of Strings to an IO Handle.
481-- | Write a stream of Strings to an IO Handle.
@@ -486,8 +487,8 @@ toHandle h m = go (toStream m)
487 let stop = return ()
488 let stop = return ()
488 single a = liftIO (IO.hPutStrLn h a)
489 single a = liftIO (IO.hPutStrLn h a)
489− yield a r = liftIO (IO.hPutStrLn h a) >> go r
490+ yieldk a r = liftIO (IO.hPutStrLn h a) >> go r
490− in (K.runStream m1) Nothing stop single yield
491+ in (K.unStream m1) Nothing stop single yieldk
492------------------------------------------------------------------------------
493------------------------------------------------------------------------------
493-- Special folds
494-- Special folds
@@ -508,8 +509,8 @@ take :: IsStream t => Int -> t m a -> t m a
508take n m = fromStream $ go n (toStream m)
509take n m = fromStream $ go n (toStream m)
510 go n1 m1 = K.Stream $ \_ stp sng yld ->
511 go n1 m1 = K.Stream $ \_ stp sng yld ->
511− let yield a r = yld a (go (n1 - 1) r)
512+ let yieldk a r = yld a (go (n1 - 1) r)
512− in if n1 <= 0 then stp else (K.runStream m1) Nothing stp sng yield
513+ in if n1 <= 0 then stp else (K.unStream m1) Nothing stp sng yieldk
514-- | Include only those elements that pass a predicate.
515-- | Include only those elements that pass a predicate.
@@ -521,9 +522,9 @@ filter p m = fromStream $ go (toStream m)
521 go m1 = K.Stream $ \_ stp sng yld ->
522 go m1 = K.Stream $ \_ stp sng yld ->
522 let single a | p a = sng a
523 let single a | p a = sng a
523 | otherwise = stp
524 | otherwise = stp
524− yield a r | p a = yld a (go r)
525+ yieldk a r | p a = yld a (go r)
525− | otherwise = (K.runStream r) Nothing stp single yield
526+ | otherwise = (K.unStream r) Nothing stp single yieldk
526− in (K.runStream m1) Nothing stp single yield
527+ in (K.unStream m1) Nothing stp single yieldk
528-- | End the stream as soon as the predicate fails on an element.
529-- | End the stream as soon as the predicate fails on an element.
@@ -535,9 +536,9 @@ takeWhile p m = fromStream $ go (toStream m)
535 go m1 = K.Stream $ \_ stp sng yld ->
536 go m1 = K.Stream $ \_ stp sng yld ->
536 let single a | p a = sng a
537 let single a | p a = sng a
537 | otherwise = stp
538 | otherwise = stp
538− yield a r | p a = yld a (go r)
539+ yieldk a r | p a = yld a (go r)
539 | otherwise = stp
540 | otherwise = stp
540− in (K.runStream m1) Nothing stp single yield
541+ in (K.unStream m1) Nothing stp single yieldk
542-- | Discard first 'n' elements from the stream and take the rest.
543-- | Discard first 'n' elements from the stream and take the rest.
@@ -547,11 +548,11 @@ drop n m = fromStream $ go n (toStream m)
548 go n1 m1 = K.Stream $ \_ stp sng yld ->
549 go n1 m1 = K.Stream $ \_ stp sng yld ->
549 let single _ = stp
550 let single _ = stp
550− yield _ r = (K.runStream $ go (n1 - 1) r) Nothing stp sng yld
551+ yieldk _ r = (K.unStream $ go (n1 - 1) r) Nothing stp sng yld
551 -- Somehow "<=" check performs better than a ">"
552 -- Somehow "<=" check performs better than a ">"
552 in if n1 <= 0
553 in if n1 <= 0
553− then (K.runStream m1) Nothing stp sng yld
554+ then (K.unStream m1) Nothing stp sng yld
554− else (K.runStream m1) Nothing stp single yield
555+ else (K.unStream m1) Nothing stp single yieldk
556-- | Drop elements in the stream as long as the predicate succeeds and then
557-- | Drop elements in the stream as long as the predicate succeeds and then
557-- take the rest of the stream.
558-- take the rest of the stream.
@@ -564,9 +565,9 @@ dropWhile p m = fromStream $ go (toStream m)
564 go m1 = K.Stream $ \_ stp sng yld ->
565 go m1 = K.Stream $ \_ stp sng yld ->
565 let single a | p a = stp
566 let single a | p a = stp
566 | otherwise = sng a
567 | otherwise = sng a
567− yield a r | p a = (K.runStream r) Nothing stp single yield
568+ yieldk a r | p a = (K.unStream r) Nothing stp single yieldk
568 | otherwise = yld a r
569 | otherwise = yld a r
569− in (K.runStream m1) Nothing stp single yield
570+ in (K.unStream m1) Nothing stp single yieldk
571-- | Determine whether all elements of a stream satisfy a predicate.
572-- | Determine whether all elements of a stream satisfy a predicate.
@@ -577,9 +578,9 @@ all p m = go (toStream m)
578 let single a | p a = return True
579 let single a | p a = return True
579 | otherwise = return False
580 | otherwise = return False
580− yield a r | p a = go r
581+ yieldk a r | p a = go r
581 | otherwise = return False
582 | otherwise = return False
582− in (K.runStream m1) Nothing (return True) single yield
583+ in (K.unStream m1) Nothing (return True) single yieldk
584-- | Determine whether any of the elements of a stream satisfy a predicate.
585-- | Determine whether any of the elements of a stream satisfy a predicate.
@@ -590,9 +591,9 @@ any p m = go (toStream m)
591 let single a | p a = return True
592 let single a | p a = return True
592 | otherwise = return False
593 | otherwise = return False
593− yield a r | p a = return True
594+ yieldk a r | p a = return True
594 | otherwise = go r
595 | otherwise = go r
595− in (K.runStream m1) Nothing (return False) single yield
596+ in (K.unStream m1) Nothing (return False) single yieldk
597-- | Determine the sum of all elements of a stream of numbers
598-- | Determine the sum of all elements of a stream of numbers
@@ -613,8 +614,8 @@ head :: Monad m => SerialT m a -> m (Maybe a)
614 let stop = return Nothing
615 let stop = return Nothing
615 single a = return (Just a)
616 single a = return (Just a)
616− yield a _ = return (Just a)
617+ yieldk a _ = return (Just a)
617− in (K.runStream (toStream m)) Nothing stop single yield
618+ in (K.unStream (toStream m)) Nothing stop single yieldk
619-- | Extract all but the first element of the stream, if any.
620-- | Extract all but the first element of the stream, if any.
@@ -623,8 +624,8 @@ tail :: (IsStream t, Monad m) => SerialT m a -> m (Maybe (t m a))
624 let stop = return Nothing
625 let stop = return Nothing
625 single _ = return $ Just nil
626 single _ = return $ Just nil
626− yield _ r = return $ Just $ fromStream r
627+ yieldk _ r = return $ Just $ fromStream r
627− in (K.runStream (toStream m)) Nothing stop single yield
628+ in (K.unStream (toStream m)) Nothing stop single yieldk
629-- | Extract the last element of the stream, if any.
630-- | Extract the last element of the stream, if any.
@@ -640,8 +641,8 @@ null :: Monad m => SerialT m a -> m Bool
641 let stop = return True
642 let stop = return True
642 single _ = return False
643 single _ = return False
643− yield _ _ = return False
644+ yieldk _ _ = return False
644− in (K.runStream (toStream m)) Nothing stop single yield
645+ in (K.unStream (toStream m)) Nothing stop single yieldk
646-- | Determine whether an element is present in the stream.
647-- | Determine whether an element is present in the stream.
@@ -652,8 +653,8 @@ elem e m = go (toStream m)
653 let stop = return False
654 let stop = return False
654 single a = return (a == e)
655 single a = return (a == e)
655− yield a r = if a == e then return True else go r
656+ yieldk a r = if a == e then return True else go r
656− in (K.runStream m1) Nothing stop single yield
657+ in (K.unStream m1) Nothing stop single yieldk
658-- | Determine whether an element is not present in the stream.
659-- | Determine whether an element is not present in the stream.
@@ -664,8 +665,8 @@ notElem e m = go (toStream m)
665 let stop = return True
666 let stop = return True
666 single a = return (a /= e)
667 single a = return (a /= e)
667− yield a r = if a == e then return False else go r
668+ yieldk a r = if a == e then return False else go r
668− in (K.runStream m1) Nothing stop single yield
669+ in (K.unStream m1) Nothing stop single yieldk
670-- | Determine the length of the stream.
671-- | Determine the length of the stream.
@@ -681,11 +682,11 @@ reverse :: (IsStream t) => t m a -> t m a
681reverse m = fromStream $ go K.nil (toStream m)
682reverse m = fromStream $ go K.nil (toStream m)
683 go rev rest = K.Stream $ \_ stp sng yld ->
684 go rev rest = K.Stream $ \_ stp sng yld ->
684− let runIt x = K.runStream x Nothing stp sng yld
685+ let runIt x = K.unStream x Nothing stp sng yld
685 stop = runIt rev
686 stop = runIt rev
686 single a = runIt $ a `K.cons` rev
687 single a = runIt $ a `K.cons` rev
687− yield a r = runIt $ go (a `K.cons` rev) r
688+ yieldk a r = runIt $ go (a `K.cons` rev) r
688− in K.runStream rest Nothing stop single yield
689+ in K.unStream rest Nothing stop single yieldk
690-- XXX replace the recursive "go" with continuation
691-- XXX replace the recursive "go" with continuation
691-- | Determine the minimum element in a stream.
692-- | Determine the minimum element in a stream.
@@ -697,8 +698,8 @@ minimum m = go Nothing (toStream m)
697 go res m1 =
698 go res m1 =
698 let stop = return res
699 let stop = return res
699 single a = return $ min_ a res
700 single a = return $ min_ a res
700− yield a r = go (min_ a res) r
701+ yieldk a r = go (min_ a res) r
701− in (K.runStream m1) Nothing stop single yield
702+ in (K.unStream m1) Nothing stop single yieldk
703 min_ a res = case res of
704 min_ a res = case res of
704 Nothing -> Just a
705 Nothing -> Just a
@@ -714,8 +715,8 @@ maximum m = go Nothing (toStream m)
714 go res m1 =
715 go res m1 =
715 let stop = return res
716 let stop = return res
716 single a = return $ max_ a res
717 single a = return $ max_ a res
717− yield a r = go (max_ a res) r
718+ yieldk a r = go (max_ a res) r
718− in (K.runStream m1) Nothing stop single yield
719+ in (K.unStream m1) Nothing stop single yieldk
720 max_ a res = case res of
721 max_ a res = case res of
721 Nothing -> Just a
722 Nothing -> Just a
@@ -745,8 +746,8 @@ mapM f m = go (toStream m)
746 go m1 = fromStream $ K.Stream $ \svr stp sng yld ->
747 go m1 = fromStream $ K.Stream $ \svr stp sng yld ->
747 let single a = f a >>= sng
748 let single a = f a >>= sng
748− yield a r = K.runStream (toStream (f a |: (go r))) svr stp sng yld
749+ yieldk a r = K.unStream (toStream (f a |: (go r))) svr stp sng yld
749− in (K.runStream m1) Nothing stp single yield
750+ in (K.unStream m1) Nothing stp single yieldk
751-- | Map a 'Maybe' returning function to a stream, filter out the 'Nothing'
752-- | Map a 'Maybe' returning function to a stream, filter out the 'Nothing'
752-- elements, and return a stream of values extracted from 'Just'.
753-- elements, and return a stream of values extracted from 'Just'.
@@ -760,10 +761,10 @@ mapMaybe f m = go (toStream m)
760 let single a = case f a of
761 let single a = case f a of
761 Just b -> sng b
762 Just b -> sng b
762 Nothing -> stp
763 Nothing -> stp
763− yield a r = case f a of
764+ yieldk a r = case f a of
764 Just b -> yld b (toStream $ go r)
765 Just b -> yld b (toStream $ go r)
765− Nothing -> (K.runStream r) Nothing stp single yield
766+ Nothing -> (K.unStream r) Nothing stp single yieldk
766− in (K.runStream m1) Nothing stp single yield
767+ in (K.unStream m1) Nothing stp single yieldk
768-- | Like 'mapMaybe' but maps a monadic function.
769-- | Like 'mapMaybe' but maps a monadic function.
@@ -786,8 +787,8 @@ mapM_ f m = go (toStream m)
787 let stop = return ()
788 let stop = return ()
788 single a = void (f a)
789 single a = void (f a)
789− yield a r = f a >> go r
790+ yieldk a r = f a >> go r
790− in (K.runStream m1) Nothing stop single yield
791+ in (K.unStream m1) Nothing stop single yieldk
792-- | Reduce a stream of monadic actions to a stream of the output of those
793-- | Reduce a stream of monadic actions to a stream of the output of those
793-- actions.
794-- actions.
@@ -808,5 +809,5 @@ sequence m = go (toStream m)
809 go m1 = fromStream $ K.Stream $ \svr stp sng yld ->
810 go m1 = fromStream $ K.Stream $ \svr stp sng yld ->
810 let single ma = ma >>= sng
811 let single ma = ma >>= sng
811− yield ma r = K.runStream (toStream $ ma |: go r) svr stp sng yld
812+ yieldk ma r = K.unStream (toStream $ ma |: go r) svr stp sng yld
812− in (K.runStream m1) Nothing stp single yield
813+ in (K.unStream m1) Nothing stp single yieldk