@@ -142,8 +142,8 @@ import Prelude hiding (filter, drop, dropWhile, take,
142142import qualified Prelude
143143import qualified System.IO as IO
144144
145−import qualified Streamly.Core as S
146−import Streamly.Core (Stream(Stream))
145+import qualified Streamly.CPS.Stream as C
146+import qualified Streamly.CPS.Concurrent as CC
147147import Streamly.Streams
148148
149149------------------------------------------------------------------------------
@@ -171,7 +171,7 @@ import Streamly.Streams
171171unfoldr :: IsStream t => (b -> Maybe (a, b)) -> b -> t m a
172172unfoldr step = fromStream . go
173173 where
174− go s = Stream $ \_ stp _ yld ->
174+ go s = C.Stream $ \_ stp _ yld ->
175175 case step s of
176176 Nothing -> stp
177177 Just (a, b) -> yld a (go b)
@@ -211,12 +211,12 @@ unfoldr step = fromStream . go
211211unfoldrM :: (IsStream t, MonadAsync m) => (b -> m (Maybe (a, b))) -> b -> t m a
212212unfoldrM step = go
213213 where
214− go s = fromStream $ Stream $ \svr stp sng yld -> do
214+ go s = fromStream $ C.Stream $ \svr stp sng yld -> do
215215 mayb <- step s
216216 case mayb of
217217 Nothing -> stp
218218 Just (a, b) ->
219− S.runStream (toStream (return a |: go b)) svr stp sng yld
219+ C.runStream (toStream (return a |: go b)) svr stp sng yld
220220
221221-- | Construct a stream from a 'Foldable' containing pure values.
222222--
@@ -258,7 +258,7 @@ each = fromFoldable
258258--
259259-- @since 0.2.0
260260once :: (IsStream t, Monad m) => m a -> t m a
261−once = fromStream . S.once
261+once = fromStream . C.once
262262
263263-- | Generate a stream by performing a monadic action @n@ times.
264264--
@@ -296,7 +296,7 @@ repeatM = go
296296iterate :: IsStream t => (a -> a) -> a -> t m a
297297iterate step = fromStream . go
298298 where
299− go s = S.cons s (go (step s))
299+ go s = C.cons s (go (step s))
300300
301301-- | Iterate a monadic function from a seed value, streaming the results
302302-- forever.
@@ -320,9 +320,9 @@ iterate step = fromStream . go
320320iterateM :: (IsStream t, MonadAsync m) => (a -> m a) -> a -> t m a
321321iterateM step = go
322322 where
323− go s = fromStream $ Stream $ \svr stp sng yld -> do
323+ go s = fromStream $ C.Stream $ \svr stp sng yld -> do
324324 next <- step s
325− S.runStream (toStream (return s |: go next)) svr stp sng yld
325+ C.runStream (toStream (return s |: go next)) svr stp sng yld
326326
327327-- | Read lines from an IO Handle into a stream of Strings.
328328--
@@ -330,7 +330,7 @@ iterateM step = go
330330fromHandle :: (IsStream t, MonadIO m) => IO.Handle -> t m String
331331fromHandle h = fromStream go
332332 where
333− go = Stream $ \_ stp _ yld -> do
333+ go = C.Stream $ \_ stp _ yld -> do
334334 eof <- liftIO $ IO.hIsEOF h
335335 if eof
336336 then stp
@@ -357,7 +357,7 @@ foldr step acc m = go (toStream m)
357357 let stop = return acc
358358 single a = return (step a acc)
359359 yield a r = go r >>= \b -> return (step a b)
360− in (S.runStream m1) Nothing stop single yield
360+ in (C.runStream m1) Nothing stop single yield
361361
362362-- | Lazy right fold with a monadic step function. For example, to fold a
363363-- stream into a list:
@@ -376,7 +376,7 @@ foldrM step acc m = go (toStream m)
376376 let stop = return acc
377377 single a = step a acc
378378 yield a r = go r >>= step a
379− in (S.runStream m1) Nothing stop single yield
379+ in (C.runStream m1) Nothing stop single yield
380380
381381-- | Strict left scan with an extraction function. Like 'scanl'', but applies a
382382-- user supplied extraction function (the third argument) at each step. This is
@@ -389,12 +389,12 @@ scanx :: IsStream t => (x -> a -> x) -> x -> (x -> b) -> t m a -> t m b
389389scanx step begin done m =
390390 cons (done begin) $ fromStream $ go (toStream m) begin
391391 where
392− go m1 !acc = Stream $ \_ stp sng yld ->
392+ go m1 !acc = C.Stream $ \_ stp sng yld ->
393393 let single a = sng (done $ step acc a)
394394 yield a r =
395395 let s = step acc a
396396 in yld (done s) (go r s)
397− in S.runStream m1 Nothing stp single yield
397+ in C.runStream m1 Nothing stp single yield
398398
399399-- |
400400-- @since 0.1.1
@@ -425,19 +425,19 @@ foldx step begin done m = get $ go (toStream m) begin
425425 {-# NOINLINE get #-}
426426 get m1 =
427427 let single = return . done
428− in (S.runStream m1) Nothing undefined single undefined
428+ in (C.runStream m1) Nothing undefined single undefined
429429
430430 -- Note, this can be implemented by making a recursive call to "go",
431431 -- however that is more expensive because of unnecessary recursion
432432 -- that cannot be tail call optimized. Unfolding recursion explicitly via
433433 -- continuations is much more efficient.
434− go m1 !acc = Stream $ \_ _ sng yld ->
434+ go m1 !acc = C.Stream $ \_ _ sng yld ->
435435 let stop = sng acc
436436 single a = sng $ step acc a
437437 yield a r =
438438 let stream = go r (step acc a)
439− in (S.runStream stream) Nothing undefined sng yld
440− in (S.runStream m1) Nothing stop single yield
439+ in (C.runStream stream) Nothing undefined sng yld
440+ in (C.runStream m1) Nothing stop single yield
441441
442442-- |
443443-- @since 0.1.0
@@ -463,7 +463,7 @@ foldxM step begin done m = go begin (toStream m)
463463 let stop = acc >>= done
464464 single a = acc >>= \b -> step b a >>= done
465465 yield a r = acc >>= \b -> go (step b a) r
466− in (S.runStream m1) Nothing stop single yield
466+ in (C.runStream m1) Nothing stop single yield
467467
468468-- |
469469-- @since 0.1.0
@@ -487,7 +487,7 @@ uncons m =
487487 let stop = return Nothing
488488 single a = return (Just (a, nil))
489489 yield a r = return (Just (a, fromStream r))
490− in (S.runStream (toStream m)) Nothing stop single yield
490+ in (C.runStream (toStream m)) Nothing stop single yield
491491
492492-- | Write a stream of Strings to an IO Handle.
493493--
@@ -499,7 +499,7 @@ toHandle h m = go (toStream m)
499499 let stop = return ()
500500 single a = liftIO (IO.hPutStrLn h a)
501501 yield a r = liftIO (IO.hPutStrLn h a) >> go r
502− in (S.runStream m1) Nothing stop single yield
502+ in (C.runStream m1) Nothing stop single yield
503503
504504------------------------------------------------------------------------------
505505-- Special folds
@@ -519,9 +519,9 @@ toList = foldrM (\a xs -> return (a : xs)) []
519519take :: IsStream t => Int -> t m a -> t m a
520520take n m = fromStream $ go n (toStream m)
521521 where
522− go n1 m1 = Stream $ \_ stp sng yld ->
522+ go n1 m1 = C.Stream $ \_ stp sng yld ->
523523 let yield a r = yld a (go (n1 - 1) r)
524− in if n1 <= 0 then stp else (S.runStream m1) Nothing stp sng yield
524+ in if n1 <= 0 then stp else (C.runStream m1) Nothing stp sng yield
525525
526526-- | Include only those elements that pass a predicate.
527527--
@@ -530,12 +530,12 @@ take n m = fromStream $ go n (toStream m)
530530filter :: IsStream t => (a -> Bool) -> t m a -> t m a
531531filter p m = fromStream $ go (toStream m)
532532 where
533− go m1 = Stream $ \_ stp sng yld ->
533+ go m1 = C.Stream $ \_ stp sng yld ->
534534 let single a | p a = sng a
535535 | otherwise = stp
536536 yield a r | p a = yld a (go r)
537− | otherwise = (S.runStream r) Nothing stp single yield
538− in (S.runStream m1) Nothing stp single yield
537+ | otherwise = (C.runStream r) Nothing stp single yield
538+ in (C.runStream m1) Nothing stp single yield
539539
540540-- | End the stream as soon as the predicate fails on an element.
541541--
@@ -544,12 +544,12 @@ filter p m = fromStream $ go (toStream m)
544544takeWhile :: IsStream t => (a -> Bool) -> t m a -> t m a
545545takeWhile p m = fromStream $ go (toStream m)
546546 where
547− go m1 = Stream $ \_ stp sng yld ->
547+ go m1 = C.Stream $ \_ stp sng yld ->
548548 let single a | p a = sng a
549549 | otherwise = stp
550550 yield a r | p a = yld a (go r)
551551 | otherwise = stp
552− in (S.runStream m1) Nothing stp single yield
552+ in (C.runStream m1) Nothing stp single yield
553553
554554-- | Discard first 'n' elements from the stream and take the rest.
555555--
@@ -557,13 +557,13 @@ takeWhile p m = fromStream $ go (toStream m)
557557drop :: IsStream t => Int -> t m a -> t m a
558558drop n m = fromStream $ go n (toStream m)
559559 where
560− go n1 m1 = Stream $ \_ stp sng yld ->
560+ go n1 m1 = C.Stream $ \_ stp sng yld ->
561561 let single _ = stp
562− yield _ r = (S.runStream $ go (n1 - 1) r) Nothing stp sng yld
562+ yield _ r = (C.runStream $ go (n1 - 1) r) Nothing stp sng yld
563563 -- Somehow "<=" check performs better than a ">"
564564 in if n1 <= 0
565− then (S.runStream m1) Nothing stp sng yld
566− else (S.runStream m1) Nothing stp single yield
565+ then (C.runStream m1) Nothing stp sng yld
566+ else (C.runStream m1) Nothing stp single yield
567567
568568-- | Drop elements in the stream as long as the predicate succeeds and then
569569-- take the rest of the stream.
@@ -573,12 +573,12 @@ drop n m = fromStream $ go n (toStream m)
573573dropWhile :: IsStream t => (a -> Bool) -> t m a -> t m a
574574dropWhile p m = fromStream $ go (toStream m)
575575 where
576− go m1 = Stream $ \_ stp sng yld ->
576+ go m1 = C.Stream $ \_ stp sng yld ->
577577 let single a | p a = stp
578578 | otherwise = sng a
579− yield a r | p a = (S.runStream r) Nothing stp single yield
579+ yield a r | p a = (C.runStream r) Nothing stp single yield
580580 | otherwise = yld a r
581− in (S.runStream m1) Nothing stp single yield
581+ in (C.runStream m1) Nothing stp single yield
582582
583583-- | Determine whether all elements of a stream satisfy a predicate.
584584--
@@ -591,7 +591,7 @@ all p m = go (toStream m)
591591 | otherwise = return False
592592 yield a r | p a = go r
593593 | otherwise = return False
594− in (S.runStream m1) Nothing (return True) single yield
594+ in (C.runStream m1) Nothing (return True) single yield
595595
596596-- | Determine whether any of the elements of a stream satisfy a predicate.
597597--
@@ -604,7 +604,7 @@ any p m = go (toStream m)
604604 | otherwise = return False
605605 yield a r | p a = return True
606606 | otherwise = go r
607− in (S.runStream m1) Nothing (return False) single yield
607+ in (C.runStream m1) Nothing (return False) single yield
608608
609609-- | Determine the sum of all elements of a stream of numbers
610610--
@@ -626,7 +626,7 @@ head m =
626626 let stop = return Nothing
627627 single a = return (Just a)
628628 yield a _ = return (Just a)
629− in (S.runStream (toStream m)) Nothing stop single yield
629+ in (C.runStream (toStream m)) Nothing stop single yield
630630
631631-- | Extract all but the first element of the stream, if any.
632632--
@@ -636,7 +636,7 @@ tail m =
636636 let stop = return Nothing
637637 single _ = return $ Just nil
638638 yield _ r = return $ Just $ fromStream r
639− in (S.runStream (toStream m)) Nothing stop single yield
639+ in (C.runStream (toStream m)) Nothing stop single yield
640640
641641-- | Extract the last element of the stream, if any.
642642--
@@ -653,7 +653,7 @@ null m =
653653 let stop = return True
654654 single _ = return False
655655 yield _ _ = return False
656− in (S.runStream (toStream m)) Nothing stop single yield
656+ in (C.runStream (toStream m)) Nothing stop single yield
657657
658658-- | Determine whether an element is present in the stream.
659659--
@@ -665,7 +665,7 @@ elem e m = go (toStream m)
665665 let stop = return False
666666 single a = return (a == e)
667667 yield a r = if a == e then return True else go r
668− in (S.runStream m1) Nothing stop single yield
668+ in (C.runStream m1) Nothing stop single yield
669669
670670-- | Determine whether an element is not present in the stream.
671671--
@@ -677,7 +677,7 @@ notElem e m = go (toStream m)
677677 let stop = return True
678678 single a = return (a /= e)
679679 yield a r = if a == e then return False else go r
680− in (S.runStream m1) Nothing stop single yield
680+ in (C.runStream m1) Nothing stop single yield
681681
682682-- | Determine the length of the stream.
683683--
@@ -690,14 +690,14 @@ length = foldl (\n _ -> n + 1) 0 id
690690--
691691-- @since 0.1.1
692692reverse :: (IsStream t) => t m a -> t m a
693−reverse m = fromStream $ go S.nil (toStream m)
693+reverse m = fromStream $ go C.nil (toStream m)
694694 where
695− go rev rest = Stream $ \_ stp sng yld ->
696− let run x = S.runStream x Nothing stp sng yld
695+ go rev rest = C.Stream $ \_ stp sng yld ->
696+ let run x = C.runStream x Nothing stp sng yld
697697 stop = run rev
698− single a = run $ a `S.cons` rev
699− yield a r = run $ go (a `S.cons` rev) r
700− in S.runStream rest Nothing stop single yield
698+ single a = run $ a `C.cons` rev
699+ yield a r = run $ go (a `C.cons` rev) r
700+ in C.runStream rest Nothing stop single yield
701701
702702-- XXX replace the recursive "go" with continuation
703703-- | Determine the minimum element in a stream.
@@ -710,7 +710,7 @@ minimum m = go Nothing (toStream m)
710710 let stop = return res
711711 single a = return $ min_ a res
712712 yield a r = go (min_ a res) r
713− in (S.runStream m1) Nothing stop single yield
713+ in (C.runStream m1) Nothing stop single yield
714714
715715 min_ a res = case res of
716716 Nothing -> Just a
@@ -727,7 +727,7 @@ maximum m = go Nothing (toStream m)
727727 let stop = return res
728728 single a = return $ max_ a res
729729 yield a r = go (max_ a res) r
730− in (S.runStream m1) Nothing stop single yield
730+ in (C.runStream m1) Nothing stop single yield
731731
732732 max_ a res = case res of
733733 Nothing -> Just a
@@ -755,10 +755,10 @@ maximum m = go Nothing (toStream m)
755755mapM :: (IsStream t, MonadAsync m) => (a -> m b) -> t m a -> t m b
756756mapM f m = go (toStream m)
757757 where
758− go m1 = fromStream $ Stream $ \svr stp sng yld ->
758+ go m1 = fromStream $ C.Stream $ \svr stp sng yld ->
759759 let single a = f a >>= sng
760− yield a r = S.runStream (toStream (f a |: (go r))) svr stp sng yld
761− in (S.runStream m1) Nothing stp single yield
760+ yield a r = C.runStream (toStream (f a |: (go r))) svr stp sng yld
761+ in (C.runStream m1) Nothing stp single yield
762762
763763-- | Map a 'Maybe' returning function to a stream, filter out the 'Nothing'
764764-- elements, and return a stream of values extracted from 'Just'.
@@ -768,14 +768,14 @@ mapM f m = go (toStream m)
768768mapMaybe :: (IsStream t) => (a -> Maybe b) -> t m a -> t m b
769769mapMaybe f m = go (toStream m)
770770 where
771− go m1 = fromStream $ Stream $ \_ stp sng yld ->
771+ go m1 = fromStream $ C.Stream $ \_ stp sng yld ->
772772 let single a = case f a of
773773 Just b -> sng b
774774 Nothing -> stp
775775 yield a r = case f a of
776776 Just b -> yld b (toStream $ go r)
777− Nothing -> (S.runStream r) Nothing stp single yield
778− in (S.runStream m1) Nothing stp single yield
777+ Nothing -> (C.runStream r) Nothing stp single yield
778+ in (C.runStream m1) Nothing stp single yield
779779
780780-- | Like 'mapMaybe' but maps a monadic function.
781781--
@@ -799,7 +799,7 @@ mapM_ f m = go (toStream m)
799799 let stop = return ()
800800 single a = void (f a)
801801 yield a r = f a >> go r
802− in (S.runStream m1) Nothing stop single yield
802+ in (C.runStream m1) Nothing stop single yield
803803
804804-- | Reduce a stream of monadic actions to a stream of the output of those
805805-- actions.
@@ -818,10 +818,10 @@ mapM_ f m = go (toStream m)
818818sequence :: (IsStream t, MonadAsync m) => t m (m a) -> t m a
819819sequence m = go (toStream m)
820820 where
821− go m1 = fromStream $ Stream $ \svr stp sng yld ->
821+ go m1 = fromStream $ C.Stream $ \svr stp sng yld ->
822822 let single ma = ma >>= sng
823− yield ma r = S.runStream (toStream $ ma |: go r) svr stp sng yld
824− in (S.runStream m1) Nothing stp single yield
823+ yield ma r = C.runStream (toStream $ ma |: go r) svr stp sng yld
824+ in (C.runStream m1) Nothing stp single yield
825825
826826------------------------------------------------------------------------------
827827-- Serially Zipping Streams
@@ -832,7 +832,7 @@ sequence m = go (toStream m)
832832-- @since 0.1.0
833833{-# INLINABLE zipWith #-}
834834zipWith :: IsStream t => (a -> b -> c) -> t m a -> t m b -> t m c
835−zipWith f m1 m2 = fromStream $ S.zipWith f (toStream m1) (toStream m2)
835+zipWith f m1 m2 = fromStream $ C.zipWith f (toStream m1) (toStream m2)
836836
837837-- | Zip two streams serially using a monadic zipping function.
838838--
@@ -840,15 +840,15 @@ zipWith f m1 m2 = fromStream $ S.zipWith f (toStream m1) (toStream m2)
840840zipWithM :: IsStream t => (a -> b -> t m c) -> t m a -> t m b -> t m c
841841zipWithM f m1 m2 = fromStream $ go (toStream m1) (toStream m2)
842842 where
843− go mx my = Stream $ \_ stp sng yld -> do
843+ go mx my = C.Stream $ \_ stp sng yld -> do
844844 let merge a ra =
845− let run x = S.runStream x Nothing stp sng yld
845+ let run x = C.runStream x Nothing stp sng yld
846846 single2 b = run $ toStream (f a b)
847847 yield2 b rb = run $ toStream (f a b) <> go ra rb
848− in (S.runStream my) Nothing stp single2 yield2
849− let single1 a = merge a S.nil
848+ in (C.runStream my) Nothing stp single2 yield2
849+ let single1 a = merge a C.nil
850850 yield1 a ra = merge a ra
851− (S.runStream mx) Nothing stp single1 yield1
851+ (C.runStream mx) Nothing stp single1 yield1
852852
853853------------------------------------------------------------------------------
854854-- Parallely Zipping Streams
@@ -861,7 +861,7 @@ zipWithM f m1 m2 = fromStream $ go (toStream m1) (toStream m2)
861861zipAsyncWith :: (IsStream t, MonadAsync m)
862862 => (a -> b -> c) -> t m a -> t m b -> t m c
863863zipAsyncWith f m1 m2 =
864− fromStream $ S.zipAsyncWith f (toStream m1) (toStream m2)
864+ fromStream $ CC.zipAsyncWith f (toStream m1) (toStream m2)
865865
866866-- | Zip two streams asyncly (i.e. both the elements being zipped are generated
867867-- concurrently) using a monadic zipping function.
@@ -869,7 +869,7 @@ zipAsyncWith f m1 m2 =
869869-- @since 0.1.0
870870zipAsyncWithM :: (IsStream t, MonadAsync m)
871871 => (a -> b -> t m c) -> t m a -> t m b -> t m c
872−zipAsyncWithM f m1 m2 = fromStream $ Stream $ \_ stp sng yld -> do
872+zipAsyncWithM f m1 m2 = fromStream $ C.Stream $ \_ stp sng yld -> do
873873 ma <- mkAsync m1
874874 mb <- mkAsync m2
875− (S.runStream (toStream (zipWithM f ma mb))) Nothing stp sng yld
875+ (C.runStream (toStream (zipWithM f ma mb))) Nothing stp sng yld