1{-# LANGUAGE UndecidableInstances #-}
6-- Module : Streamly.Internal.Data.Stream.StreamK
7-- Copyright : (c) 2017 Composewell Technologies
10-- Maintainer : streamly@composewell.com
11-- Stability : experimental
15-- Continuation passing style (CPS) stream implementation. The symbol 'K' below
16-- denotes a function as well as a Kontinuation.
19-- import qualified Streamly.Internal.Data.Stream.StreamK as K
22module Streamly.Internal.Data.Stream.StreamK
27 -- * Construction Primitives
34 -- * Elimination Primitives
38 -- * Transformation Primitives
49 -- ** Specialized Generation
86 -- ** Specialized Folds
115 -- ** By folding (scans)
163 -- ** Transformation comprehensions
166 -- * Semigroup Style Composition
176import Control.Monad.Trans.Class (MonadTrans(lift))
177import Control.Monad (void, join)
178import Streamly.Internal.Data.SVar.Type (adaptState, defState)
180import qualified Streamly.Internal.Data.Fold.Type as FL
183 hiding (foldl, foldr, last, map, mapM, mapM_, repeat, sequence,
184 take, filter, all, any, takeWhile, drop, dropWhile, minimum,
185 maximum, elem, notElem, null, head, tail, init, zipWith, lookup,
186 foldr1, (!!), replicate, reverse, concatMap, iterate)
188import Streamly.Internal.Data.Stream.StreamK.Type
193-------------------------------------------------------------------------------
195-------------------------------------------------------------------------------
197{-# INLINE unfoldrM #-}
198unfoldrM :: Monad m => (b -> m (Maybe (a, b))) -> b -> Stream m a
199unfoldrM = unfoldrMWith consM
201-- Generalization of concurrent streams/SVar via unfoldr.
203-- Unfold a value into monadic actions and then run the resulting monadic
204-- actions to generate a stream. Since the step of generating the monadic
205-- action and running them are decoupled we can run the monadic actions
206-- cooncurrently. For example, the seed could be a list of monadic actions or a
207-- pure stream of monadic actions.
209-- We can have different flavors of this depending on the stream type t. The
210-- concurrent version could be async or ahead etc. Depending on how we queue
211-- back the feedback portion b, it could be DFS or BFS style.
213unfoldrA :: (IsStream t, MonadAsync m) =>
214 (b -> Maybe (m a, b)) -> b -> Stream m a
218-------------------------------------------------------------------------------
220-------------------------------------------------------------------------------
222repeatM :: Monad m => m a -> Stream m a
223repeatM = repeatMWith consM
225{-# INLINE replicateM #-}
226replicateM :: Monad m => Int -> m a -> Stream m a
227replicateM = replicateMWith consM
228{-# INLINE replicate #-}
229replicate :: Int -> a -> Stream m a
232 go cnt = if cnt <= 0 then nil else a `cons` go (cnt - 1)
234{-# INLINE fromIndicesM #-}
235fromIndicesM :: Monad m => (Int -> m a) -> Stream m a
236fromIndicesM = fromIndicesMWith consM
237{-# INLINE fromIndices #-}
238fromIndices :: (Int -> a) -> Stream m a
239fromIndices gen = go 0
241 go n = gen n `cons` go (n + 1)
243{-# INLINE iterate #-}
244iterate :: (a -> a) -> a -> Stream m a
247 go !s = cons s (go (step s))
249{-# INLINE iterateM #-}
250iterateM :: Monad m => (a -> m a) -> m a -> Stream m a
251iterateM = iterateMWith consM
253-------------------------------------------------------------------------------
255-------------------------------------------------------------------------------
257{-# INLINE fromList #-}
258fromList :: [a] -> Stream m a
259fromList = fromFoldable
261-------------------------------------------------------------------------------
262-- Elimination by Folding
263-------------------------------------------------------------------------------
265-- | Lazy right associative fold.
267foldr :: Monad m => (a -> b -> b) -> b -> Stream m a -> m b
268foldr step acc = foldrM (\x xs -> xs >>= \b -> return (step x b)) (return acc)
270-- | Right associative fold to an arbitrary transformer monad.
272foldrT :: (Monad m, Monad (s m), MonadTrans s)
273 => (a -> s m b -> s m b) -> s m b -> Stream m a -> s m b
274foldrT step final = go
277 res <- lift $ uncons m1
279 Just (h, t) -> step h (go t)
283foldr1 :: Monad m => (a -> a -> a) -> Stream m a -> m (Maybe a)
287 Nothing -> return Nothing
288 Just (h, t) -> fmap Just (go h t)
292 single a = return $ step a p
293 yieldk a r = fmap (step p) (go a r)
294 in foldStream defState yieldk single stp m1
296-- XXX replace the recursive "go" with explicit continuations.
297-- | Like 'foldx', but with a monadic step function.
298{-# INLINABLE foldlMx' #-}
300 => (x -> a -> m x) -> m x -> (x -> m b) -> Stream m a -> m b
301foldlMx' step begin done = go begin
304 let stop = acc >>= done
305 single a = acc >>= \b -> step b a >>= done
306 yieldk a r = acc >>= \b -> step b a >>= \x -> go (return x) r
307 in foldStream defState yieldk single stop m1
309{-# INLINABLE fold #-}
310fold :: Monad m => FL.Fold m a b -> Stream m a -> m b
311fold (FL.Fold step begin done) m = do
314 FL.Partial fs -> go fs m
315 FL.Done fb -> return fb
320 single a = step acc a
322 FL.Partial s -> done s
323 FL.Done b1 -> return b1
324 yieldk a r = step acc a
326 FL.Partial s -> go s r
327 FL.Done b1 -> return b1
328 in foldStream defState yieldk single stop m1
330-- | Like 'foldl'' but with a monadic step function.
331{-# INLINE foldlM' #-}
332foldlM' :: Monad m => (b -> a -> m b) -> m b -> Stream m a -> m b
333foldlM' step begin = foldlMx' step begin return
335-- | Lazy left fold to an arbitrary transformer monad.
337foldlT :: (Monad m, Monad (s m), MonadTrans s)
338 => (s m b -> a -> s m b) -> s m b -> Stream m a -> s m b
342 res <- lift $ uncons m1
344 Just (h, t) -> go (step acc h) t
347------------------------------------------------------------------------------
349------------------------------------------------------------------------------
352head :: Monad m => Stream m a -> m (Maybe a)
353-- head = foldrM (\x _ -> return $ Just x) (return Nothing)
355 let stop = return Nothing
356 single a = return (Just a)
357 yieldk a _ = return (Just a)
358 in foldStream defState yieldk single stop m
361elem :: (Monad m, Eq a) => a -> Stream m a -> m Bool
365 let stop = return False
366 single a = return (a == e)
367 yieldk a r = if a == e then return True else go r
368 in foldStream defState yieldk single stop m1
370{-# INLINE notElem #-}
371notElem :: (Monad m, Eq a) => a -> Stream m a -> m Bool
375 let stop = return True
376 single a = return (a /= e)
377 yieldk a r = if a == e then return False else go r
378 in foldStream defState yieldk single stop m1
381all :: Monad m => (a -> Bool) -> Stream m a -> m Bool
385 let single a | p a = return True
386 | otherwise = return False
387 yieldk a r | p a = go r
388 | otherwise = return False
389 in foldStream defState yieldk single (return True) m1
392any :: Monad m => (a -> Bool) -> Stream m a -> m Bool
396 let single a | p a = return True
397 | otherwise = return False
398 yieldk a r | p a = return True
400 in foldStream defState yieldk single (return False) m1
402-- | Extract the last element of the stream, if any.
404last :: Monad m => Stream m a -> m (Maybe a)
405last = foldlx' (\_ y -> Just y) Nothing id
407{-# INLINE minimum #-}
408minimum :: (Monad m, Ord a) => Stream m a -> m (Maybe a)
412 let stop = return Nothing
413 single a = return (Just a)
414 yieldk a r = go (Just a) r
415 in foldStream defState yieldk single stop m1
418 let stop = return (Just res)
421 then return (Just res)
427 in foldStream defState yieldk single stop m1
429{-# INLINE minimumBy #-}
432 => (a -> a -> Ordering) -> Stream m a -> m (Maybe a)
433minimumBy cmp = go Nothing
436 let stop = return Nothing
437 single a = return (Just a)
438 yieldk a r = go (Just a) r
439 in foldStream defState yieldk single stop m1
442 let stop = return (Just res)
443 single a = case cmp res a of
444 GT -> return (Just a)
445 _ -> return (Just res)
446 yieldk a r = case cmp res a of
449 in foldStream defState yieldk single stop m1
451{-# INLINE maximum #-}
452maximum :: (Monad m, Ord a) => Stream m a -> m (Maybe a)
456 let stop = return Nothing
457 single a = return (Just a)
458 yieldk a r = go (Just a) r
459 in foldStream defState yieldk single stop m1
462 let stop = return (Just res)
466 else return (Just res)
471 in foldStream defState yieldk single stop m1
473{-# INLINE maximumBy #-}
474maximumBy :: Monad m => (a -> a -> Ordering) -> Stream m a -> m (Maybe a)
475maximumBy cmp = go Nothing
478 let stop = return Nothing
479 single a = return (Just a)
480 yieldk a r = go (Just a) r
481 in foldStream defState yieldk single stop m1
484 let stop = return (Just res)
485 single a = case cmp res a of
486 GT -> return (Just res)
488 yieldk a r = case cmp res a of
489 GT -> go (Just res) r
491 in foldStream defState yieldk single stop m1
494(!!) :: Monad m => Stream m a -> Int -> m (Maybe a)
498 let single a | n == 0 = return $ Just a
499 | otherwise = return Nothing
500 yieldk a x | n < 0 = return Nothing
501 | n == 0 = return $ Just a
502 | otherwise = go (n - 1) x
503 in foldStream defState yieldk single (return Nothing) m1
506lookup :: (Monad m, Eq a) => a -> Stream m (a, b) -> m (Maybe b)
510 let single (a, b) | a == e = return $ Just b
511 | otherwise = return Nothing
512 yieldk (a, b) x | a == e = return $ Just b
514 in foldStream defState yieldk single (return Nothing) m1
517findM :: Monad m => (a -> m Bool) -> Stream m a -> m (Maybe a)
523 if b then return $ Just a else return Nothing
526 if b then return $ Just a else go x
527 in foldStream defState yieldk single (return Nothing) m1
530find :: Monad m => (a -> Bool) -> Stream m a -> m (Maybe a)
531find p = findM (return . p)
533{-# INLINE findIndices #-}
534findIndices :: (a -> Bool) -> Stream m a -> Stream m Int
537 go offset m1 = mkStream $ \st yld sng stp ->
538 let single a | p a = sng offset
540 yieldk a x | p a = yld offset $ go (offset + 1) x
541 | otherwise = foldStream (adaptState st) yld sng stp $
543 in foldStream (adaptState st) yieldk single stp m1
545------------------------------------------------------------------------------
547------------------------------------------------------------------------------
549-- | Apply a monadic action to each element of the stream and discard the
550-- output of the action.
552mapM_ :: Monad m => (a -> m b) -> Stream m a -> m ()
557 single a = void (f a)
558 yieldk a r = f a >> go r
559 in foldStream defState yieldk single stop m1
562mapM :: Monad m => (a -> m b) -> Stream m a -> Stream m b
565------------------------------------------------------------------------------
567------------------------------------------------------------------------------
569{-# INLINABLE toList #-}
570toList :: Monad m => Stream m a -> m [a]
573-- Based on suggestions by David Feuer and Pranay Sashank
575hoist :: (Monad m, Monad n)
576 => (forall x. m x -> n x) -> Stream m a -> Stream n a
578 mkStream $ \st yld sng stp ->
579 let single = return . sng
580 yieldk a s = return $ yld a (hoist f s)
582 state = adaptState st
583 in join . f $ foldStreamShared state yieldk single stop str
585-------------------------------------------------------------------------------
586-- Transformation by folding (Scans)
587-------------------------------------------------------------------------------
589{-# INLINE scanlx' #-}
590scanlx' :: (x -> a -> x) -> x -> (x -> b) -> Stream m a -> Stream m b
591scanlx' step begin done m =
592 cons (done begin) $ go m begin
594 go m1 !acc = mkStream $ \st yld sng stp ->
595 let single a = sng (done $ step acc a)
598 in yld (done s) (go r s)
599 in foldStream (adaptState st) yieldk single stp m1
602scanl' :: (b -> a -> b) -> b -> Stream m a -> Stream m b
603scanl' step begin = scanlx' step begin id
605-------------------------------------------------------------------------------
607-------------------------------------------------------------------------------
610filter :: (a -> Bool) -> Stream m a -> Stream m a
613 go m1 = mkStream $ \st yld sng stp ->
614 let single a | p a = sng a
616 yieldk a r | p a = yld a (go r)
617 | otherwise = foldStream st yieldk single stp r
618 in foldStream st yieldk single stp m1
621take :: Int -> Stream m a -> Stream m a
624 go n1 m1 = mkStream $ \st yld sng stp ->
625 let yieldk a r = yld a (go (n1 - 1) r)
628 else foldStream st yieldk sng stp m1
630{-# INLINE takeWhile #-}
631takeWhile :: (a -> Bool) -> Stream m a -> Stream m a
634 go m1 = mkStream $ \st yld sng stp ->
635 let single a | p a = sng a
637 yieldk a r | p a = yld a (go r)
639 in foldStream st yieldk single stp m1
642drop :: Int -> Stream m a -> Stream m a
643drop n m = unShare (go n m)
645 go n1 m1 = mkStream $ \st yld sng stp ->
647 yieldk _ r = foldStreamShared st yld sng stp $ go (n1 - 1) r
648 -- Somehow "<=" check performs better than a ">"
650 then foldStreamShared st yld sng stp m1
651 else foldStreamShared st yieldk single stp m1
653{-# INLINE dropWhile #-}
654dropWhile :: (a -> Bool) -> Stream m a -> Stream m a
657 go m1 = mkStream $ \st yld sng stp ->
658 let single a | p a = stp
660 yieldk a r | p a = foldStream st yieldk single stp r
661 | otherwise = yld a r
662 in foldStream st yieldk single stp m1
664-------------------------------------------------------------------------------
666-------------------------------------------------------------------------------
668-- Be careful when modifying this, this uses a consM (|:) deliberately to allow
669-- other stream types to overload it.
670{-# INLINE sequence #-}
671sequence :: Monad m => Stream m (m a) -> Stream m a
674 go m1 = mkStream $ \st yld sng stp ->
675 let single ma = ma >>= sng
676 yieldk ma r = foldStreamShared st yld sng stp $ ma `consM` go r
677 in foldStream (adaptState st) yieldk single stp m1
679-------------------------------------------------------------------------------
681-------------------------------------------------------------------------------
683{-# INLINE intersperseM #-}
684intersperseM :: Monad m => m a -> Stream m a -> Stream m a
685intersperseM a = prependingStart
687 prependingStart m1 = mkStream $ \st yld sng stp ->
689 foldStreamShared st yld sng stp $ return i `consM` go x
690 in foldStream st yieldk sng stp m1
691 go m2 = mkStream $ \st yld sng stp ->
692 let single i = foldStreamShared st yld sng stp $ a `consM` fromPure i
695 st yld sng stp $ a `consM` return i `consM` go x
696 in foldStream st yieldk single stp m2
698{-# INLINE intersperse #-}
699intersperse :: Monad m => a -> Stream m a -> Stream m a
700intersperse a = intersperseM (return a)
702{-# INLINE insertBy #-}
703insertBy :: (a -> a -> Ordering) -> a -> Stream m a -> Stream m a
706 go m1 = mkStream $ \st yld _ _ ->
707 let single a = case cmp x a of
708 GT -> yld a (fromPure x)
709 _ -> yld x (fromPure a)
711 yieldk a r = case cmp x a of
713 _ -> yld x (a `cons` r)
714 in foldStream st yieldk single stop m1
716------------------------------------------------------------------------------
718------------------------------------------------------------------------------
720{-# INLINE deleteBy #-}
721deleteBy :: (a -> a -> Bool) -> a -> Stream m a -> Stream m a
724 go m1 = mkStream $ \st yld sng stp ->
725 let single a = if eq x a then stp else sng a
726 yieldk a r = if eq x a
727 then foldStream st yld sng stp r
729 in foldStream st yieldk single stp m1
731-------------------------------------------------------------------------------
733-------------------------------------------------------------------------------
735{-# INLINE mapMaybe #-}
736mapMaybe :: (a -> Maybe b) -> Stream m a -> Stream m b
739 go m1 = mkStream $ \st yld sng stp ->
740 let single a = maybe stp sng (f a)
741 yieldk a r = case f a of
742 Just b -> yld b $ go r
743 Nothing -> foldStream (adaptState st) yieldk single stp r
744 in foldStream (adaptState st) yieldk single stp m1
746------------------------------------------------------------------------------
748------------------------------------------------------------------------------
750-- | Zip two streams serially using a pure zipping function.
753{-# INLINE zipWith #-}
754zipWith :: Monad m => (a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
755zipWith f = zipWithM (\a b -> return (f a b))
757-- | Zip two streams serially using a monadic zipping function.
760{-# INLINE zipWithM #-}
761zipWithM :: Monad m =>
762 (a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
767 go mx my = mkStream $ \st yld sng stp -> do
769 let single2 b = f a b >>= sng
770 yield2 b rb = f a b >>= \x -> yld x (go ra rb)
771 in foldStream (adaptState st) yield2 single2 stp my
772 let single1 a = merge a nil
774 foldStream (adaptState st) yield1 single1 stp mx
776------------------------------------------------------------------------------
778------------------------------------------------------------------------------
780{-# INLINE mergeByM #-}
781mergeByM :: Monad m =>
782 (a -> a -> m Ordering) -> Stream m a -> Stream m a -> Stream m a
787 go mx my = mkStream $ \st yld sng stp -> do
788 let stop = foldStream st yld sng stp my
789 single x = foldStream st yld sng stp (goX0 x my)
790 yield x rx = foldStream st yld sng stp (goX x rx my)
791 foldStream st yield single stop mx
793 goX0 x my = mkStream $ \st yld sng _ -> do
798 GT -> yld y (fromPure x)
799 _ -> yld x (fromPure y)
803 GT -> yld y (goX0 x ry)
804 _ -> yld x (y `cons` ry)
805 in foldStream st yield single stop my
807 goX x mx my = mkStream $ \st yld _ _ -> do
812 GT -> yld y (x `cons` mx)
813 _ -> yld x (goY0 mx y)
817 GT -> yld y (goX x mx ry)
818 _ -> yld x (goY mx y ry)
819 in foldStream st yield single stop my
821 goY0 mx y = mkStream $ \st yld sng _ -> do
826 GT -> yld y (fromPure x)
827 _ -> yld x (fromPure y)
831 GT -> yld y (x `cons` rx)
832 _ -> yld x (goY0 rx y)
833 in foldStream st yield single stop mx
835 goY mx y my = mkStream $ \st yld _ _ -> do
840 GT -> yld y (goX0 x my)
841 _ -> yld x (y `cons` my)
845 GT -> yld y (goX x rx my)
846 _ -> yld x (goY rx y my)
847 in foldStream st yield single stop mx
849{-# INLINE mergeBy #-}
850mergeBy :: (a -> a -> Ordering) -> Stream m a -> Stream m a -> Stream m a
851-- XXX GHC: This has slightly worse performance than replacing "r <- cmp x y"
852-- with "let r = cmp x y" in the monadic version. The definition below is
853-- exactly the same as mergeByM except this change.
854-- mergeBy cmp = mergeByM (\a b -> return $ cmp a b)
859 go mx my = mkStream $ \st yld sng stp -> do
860 let stop = foldStream st yld sng stp my
861 single x = foldStream st yld sng stp (goX0 x my)
862 yield x rx = foldStream st yld sng stp (goX x rx my)
863 foldStream st yield single stop mx
865 goX0 x my = mkStream $ \st yld sng _ -> do
869 GT -> yld y (fromPure x)
870 _ -> yld x (fromPure y)
873 GT -> yld y (goX0 x ry)
874 _ -> yld x (y `cons` ry)
875 in foldStream st yield single stop my
877 goX x mx my = mkStream $ \st yld _ _ -> do
881 GT -> yld y (x `cons` mx)
882 _ -> yld x (goY0 mx y)
885 GT -> yld y (goX x mx ry)
886 _ -> yld x (goY mx y ry)
887 in foldStream st yield single stop my
889 goY0 mx y = mkStream $ \st yld sng _ -> do
893 GT -> yld y (fromPure x)
894 _ -> yld x (fromPure y)
897 GT -> yld y (x `cons` rx)
898 _ -> yld x (goY0 rx y)
899 in foldStream st yield single stop mx
901 goY mx y my = mkStream $ \st yld _ _ -> do
905 GT -> yld y (goX0 x my)
906 _ -> yld x (y `cons` my)
909 GT -> yld y (goX x rx my)
910 _ -> yld x (goY rx y my)
911 in foldStream st yield single stop mx
913------------------------------------------------------------------------------
914-- Transformation comprehensions
915------------------------------------------------------------------------------
918the :: (Eq a, Monad m) => Stream m a -> m (Maybe a)
922 Nothing -> return Nothing
923 Just (h, t) -> go h t
926 let single a | h == a = return $ Just h
927 | otherwise = return Nothing
928 yieldk a r | h == a = go h r
929 | otherwise = return Nothing
930 in foldStream defState yieldk single (return $ Just h) m1
932------------------------------------------------------------------------------
933-- Alternative & MonadPlus
934------------------------------------------------------------------------------
936_alt :: Stream m a -> Stream m a -> Stream m a
937_alt m1 m2 = mkStream $ \st yld sng stp ->
938 let stop = foldStream st yld sng stp m2
939 in foldStream st yld sng stop m1
941------------------------------------------------------------------------------
943------------------------------------------------------------------------------
946-- XXX handle and test cross thread state transfer
949 => Stream m a -> (e -> Stream m a) -> Stream m a
951 mkStream $ \_ stp sng yld ->
952 let run x = unStream x Nothing stp sng yieldk
953 handle r = r `catchError` \e -> run $ h e
954 yieldk a r = yld a (withCatchError r h)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list