1{-# LANGUAGE BangPatterns #-}
3{-# LANGUAGE ExistentialQuantification #-}
4{-# LANGUAGE FlexibleContexts #-}
5{-# LANGUAGE PatternSynonyms #-}
6{-# LANGUAGE RankNTypes #-}
7{-# LANGUAGE RecordWildCards #-}
8-- {-# LANGUAGE ScopedTypeVariables #-}
13-- Module : Streamly.Streams.StreamDK
14-- Copyright : (c) 2019 Composewell Technologies
16-- Maintainer : harendra.kumar@gmail.com
17-- Stability : experimental
21module Streamly.Streams.StreamDK
45import Streamly.Streams.StreamDK.Type (Stream(..), Step(..))
47-------------------------------------------------------------------------------
49-------------------------------------------------------------------------------
51nil :: Monad m => Stream m a
52nil = Stream $ return Stop
54{-# INLINE_NORMAL cons #-}
55cons :: Monad m => a -> Stream m a -> Stream m a
56cons x xs = Stream $ return $ Yield x xs
58consM :: Monad m => m a -> Stream m a -> Stream m a
59consM eff xs = Stream $ eff >>= \x -> return $ Yield x xs
61unfoldrM :: Monad m => (s -> m (Maybe (a, s))) -> s -> Stream m a
62unfoldrM next state = Stream (step' state)
67 Just (x, s) -> Yield x (Stream (step' s))
70unfoldrM next s0 = buildM $ \yld stp ->
74 Just (a, b) -> yld a (go b)
80unfoldr :: Monad m => (b -> Maybe (a, b)) -> b -> Stream m a
81unfoldr next s0 = build $ \yld stp ->
84 Just (a, b) -> yld a (go b)
88replicateM :: Monad m => Int -> a -> Stream m a
89replicateM n x = Stream (step n)
94 else Yield x (Stream (step (i - 1)))
96-------------------------------------------------------------------------------
98-------------------------------------------------------------------------------
100uncons :: Monad m => Stream m a -> m (Maybe (a, Stream m a))
101uncons (Stream step) = do
104 Yield x xs -> Just (x, xs)
107-- | Lazy right associative fold to a stream.
108{-# INLINE_NORMAL foldrS #-}
110 => (a -> Stream m b -> Stream m b)
116 go (Stream stepa) = Stream $ do
119 Yield x xs -> let Stream step = f x (go xs) in step
120 Stop -> let Stream step = streamb in step
122{-# INLINE_LATE foldrM #-}
123foldrM :: Monad m => (a -> m b -> m b) -> m b -> Stream m a -> m b
124foldrM fstep acc ys = go ys
126 go (Stream step) = do
129 Yield x xs -> fstep x (go xs)
132{-# INLINE_NORMAL build #-}
134 => forall a. (forall b. (a -> b -> b) -> b -> b) -> Stream m a
138"foldrM/build" forall k z (g :: forall b. (a -> b -> b) -> b -> b).
139 foldrM k z (build g) = g k z #-}
142-- To fuse foldrM with unfoldrM we need the type m1 to be polymorphic such that
143-- it is either Monad m or Stream m. So that we can use cons/nil as well as
144-- monadic construction function as its arguments.
146{-# INLINE_NORMAL buildM #-}
148 => forall a. (forall b. (a -> m1 b -> m1 b) -> m1 b -> m1 b) -> Stream m a
152-------------------------------------------------------------------------------
154-------------------------------------------------------------------------------
157drain :: Monad m => Stream m a -> m ()
158drain = foldrM (\_ xs -> xs) (return ())
160drain (Stream step) = do
163 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