3-- Copyright : (c) 2018 Harendra Kumar
6-- Maintainer : harendra.kumar@gmail.com
8{-# LANGUAGE FlexibleContexts #-}
10module StreamKOps where
12import Control.Monad (when)
14 (Monad, Int, (+), ($), (.), return, fmap, even, (>), (<=),
15 subtract, undefined, Maybe(..), not, mapM_, (>>=))
17import qualified Streamly.Streams.StreamK as S
18import qualified Streamly.Streams.Prelude as S
19import qualified Streamly.SVar as S
23maxValue = value + 1000
25-------------------------------------------------------------------------------
27-------------------------------------------------------------------------------
31{-# INLINE nullHeadTail #-}
34{-# INLINE filterEven #-}
35{-# INLINE filterAllOut #-}
36{-# INLINE filterAllIn #-}
39{-# INLINE takeWhileTrue #-}
41{-# INLINE dropWhileTrue #-}
44{-# INLINE composeAllInFilters #-}
45{-# INLINE composeAllOutFilters #-}
46{-# INLINE composeMapAllInFilter #-}
47toNull, uncons, nullHeadTail, scan, map, filterEven, filterAllOut,
48 filterAllIn, takeOne, takeAll, takeWhileTrue, dropAll, dropWhileTrue, zip,
49 concat, composeAllInFilters, composeAllOutFilters,
52 => Stream m Int -> m ()
54{-# INLINE composeMapM #-}
55composeMapM :: S.MonadAsync m => Stream m Int -> m ()
58toList :: Monad m => Stream m Int -> m [Int]
60foldl :: Monad m => Stream m Int -> m Int
62last :: Monad m => Stream m Int -> m (Maybe Int)
65mapM :: S.MonadAsync m => Stream m Int -> m ()
67-------------------------------------------------------------------------------
68-- Stream generation and elimination
69-------------------------------------------------------------------------------
71type Stream m a = S.Stream m a
73{-# INLINE sourceUnfoldr #-}
74sourceUnfoldr :: Int -> Stream m Int
75sourceUnfoldr n = S.unfoldr step n
80 else Just (cnt, cnt + 1)
82{-# INLINE sourceUnfoldrM #-}
83sourceUnfoldrM :: S.MonadAsync m => Int -> Stream m Int
84sourceUnfoldrM n = S.unfoldrM step n
89 else return (Just (cnt, cnt + 1))
92{-# INLINE sourceFromEnum #-}
93sourceFromEnum :: Monad m => Int -> Stream m Int
94sourceFromEnum n = S.enumFromStepN n 1 value
97{-# INLINE sourceFromFoldable #-}
98sourceFromFoldable :: Int -> Stream m Int
99sourceFromFoldable n = S.fromFoldable [n..n+value]
102{-# INLINE sourceFromFoldableM #-}
103sourceFromFoldableM :: S.MonadAsync m => Int -> Stream m Int
104sourceFromFoldableM n = S.fromFoldableM (Prelude.fmap return [n..n+value])
107{-# INLINE sourceFoldMapWith #-}
108sourceFoldMapWith :: Int -> Stream m Int
109sourceFoldMapWith n = S.foldMapWith S.serial S.yield [n..n+value]
111{-# INLINE sourceFoldMapWithM #-}
112sourceFoldMapWithM :: Monad m => Int -> Stream m Int
113sourceFoldMapWithM n = S.foldMapWith S.serial (S.yieldM . return) [n..n+value]
116source :: S.MonadAsync m => Int -> Stream m Int
117source = sourceUnfoldrM
119-------------------------------------------------------------------------------
121-------------------------------------------------------------------------------
123{-# INLINE runStream #-}
124runStream :: Monad m => Stream m a -> m ()
125runStream = S.runStream
132 Just (_, t) -> uncons t
135init :: (Monad m, S.IsStream t) => t m a -> m ()
141tail :: (Monad m, S.IsStream t) => t m a -> m ()
142tail s = S.tail s >>= mapM_ tail
144-- | If the stream is not null get its head and tail and then do the same to
150 S.tail s >>= mapM_ nullHeadTail
153foldl = S.foldl' (+) 0
156-------------------------------------------------------------------------------
158-------------------------------------------------------------------------------
160{-# INLINE transform #-}
161transform :: Monad m => Stream m a -> m ()
164scan = transform . S.scanl' (+) 0
165map = transform . fmap (+1)
166mapM = transform . S.mapM return
167filterEven = transform . S.filter even
168filterAllOut = transform . S.filter (> maxValue)
169filterAllIn = transform . S.filter (<= maxValue)
170takeOne = transform . S.take 1
171takeAll = transform . S.take maxValue
172takeWhileTrue = transform . S.takeWhile (<= maxValue)
173dropAll = transform . S.drop maxValue
174dropWhileTrue = transform . S.dropWhile (<= maxValue)
176-------------------------------------------------------------------------------
178-------------------------------------------------------------------------------
180zip src = transform $ S.zipWith (,) src src
183-------------------------------------------------------------------------------
185-------------------------------------------------------------------------------
187{-# INLINE compose #-}
188compose :: Monad m => (Stream m Int -> Stream m Int) -> Stream m Int -> m ()
189compose f = transform . f . f . f . f
191composeMapM = compose (S.mapM return)
192composeAllInFilters = compose (S.filter (<= maxValue))
193composeAllOutFilters = compose (S.filter (> maxValue))
194composeMapAllInFilter = compose (S.filter (<= maxValue) . fmap (subtract 1))
196{-# INLINABLE composeScaling #-}
197composeScaling :: Monad m => Int -> Stream m Int -> m ()
201 2 -> transform . f . f
202 3 -> transform . f . f . f
203 4 -> transform . f . f . f . f
205 where f = S.filter (<= maxValue)
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