4-- Module : Streamly.Internal.Data.Stream.StreamDK
5-- Copyright : (c) 2019 Composewell Technologies
7-- Maintainer : streamly@composewell.com
8-- Stability : experimental
12module Streamly.Internal.Data.Stream.StreamDK
36import Streamly.Internal.Data.Stream.StreamDK.Type (Stream(..), Step(..))
38-------------------------------------------------------------------------------
40-------------------------------------------------------------------------------
42nil :: Monad m => Stream m a
43nil = Stream $ return Stop
45{-# INLINE_NORMAL cons #-}
46cons :: Monad m => a -> Stream m a -> Stream m a
47cons x xs = Stream $ return $ Yield x xs
49consM :: Monad m => m a -> Stream m a -> Stream m a
50consM eff xs = Stream $ eff >>= \x -> return $ Yield x xs
52unfoldrM :: Monad m => (s -> m (Maybe (a, s))) -> s -> Stream m a
53unfoldrM next state = Stream (step' state)
58 Just (x, s) -> Yield x (Stream (step' s))
61unfoldrM next s0 = buildM $ \yld stp ->
65 Just (a, b) -> yld a (go b)
71unfoldr :: Monad m => (b -> Maybe (a, b)) -> b -> Stream m a
72unfoldr next s0 = build $ \yld stp ->
75 Just (a, b) -> yld a (go b)
79replicateM :: Monad m => Int -> a -> Stream m a
80replicateM n x = Stream (step n)
85 else Yield x (Stream (step (i - 1)))
87-------------------------------------------------------------------------------
89-------------------------------------------------------------------------------
91uncons :: Monad m => Stream m a -> m (Maybe (a, Stream m a))
92uncons (Stream step) = do
95 Yield x xs -> Just (x, xs)
98-- | Lazy right associative fold to a stream.
99{-# INLINE_NORMAL foldrS #-}
101 => (a -> Stream m b -> Stream m b)
107 go (Stream stepa) = Stream $ do
110 Yield x xs -> let Stream step = f x (go xs) in step
111 Stop -> let Stream step = streamb in step
113{-# INLINE_LATE foldrM #-}
114foldrM :: Monad m => (a -> m b -> m b) -> m b -> Stream m a -> m b
115foldrM fstep acc ys = go ys
117 go (Stream step) = do
120 Yield x xs -> fstep x (go xs)
123{-# INLINE_NORMAL build #-}
125 => forall a. (forall b. (a -> b -> b) -> b -> b) -> Stream m a
129"foldrM/build" forall k z (g :: forall b. (a -> b -> b) -> b -> b).
130 foldrM k z (build g) = g k z #-}
133-- To fuse foldrM with unfoldrM we need the type m1 to be polymorphic such that
134-- it is either Monad m or Stream m. So that we can use cons/nil as well as
135-- monadic construction function as its arguments.
137{-# INLINE_NORMAL buildM #-}
139 => forall a. (forall b. (a -> m1 b -> m1 b) -> m1 b -> m1 b) -> Stream m a
143-------------------------------------------------------------------------------
145-------------------------------------------------------------------------------
148drain :: Monad m => Stream m a -> m ()
149drain = foldrM (\_ xs -> xs) (return ())
151drain (Stream step) = do
154 Yield _ next -> drain next
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