3-- Copyright : (c) 2018 Harendra Kumar
6-- Maintainer : harendra.kumar@gmail.com
8{-# LANGUAGE FlexibleContexts #-}
9{-# LANGUAGE ScopedTypeVariables #-}
11module StreamDOps where
13import Control.Monad (when)
14import Data.Maybe (isJust)
16 (Monad, Int, (+), ($), (.), return, (>), even, (<=), div,
17 subtract, undefined, Maybe(..), not, mapM_, (>>=),
19import qualified Prelude as P
21import qualified Streamly.Streams.StreamD as S
25maxValue = value + 1000
27-------------------------------------------------------------------------------
29-------------------------------------------------------------------------------
32{-# INLINE nullTail #-}
33{-# INLINE headTail #-}
38uncons, nullTail, headTail, zip
41 => Stream m Int -> m ()
44toList :: Monad m => Stream m Int -> m [Int]
46foldl :: Monad m => Stream m Int -> m Int
48last :: Monad m => Stream m Int -> m (Maybe Int)
51toNull :: Monad m => Stream m Int -> m ()
53-------------------------------------------------------------------------------
54-- Stream generation and elimination
55-------------------------------------------------------------------------------
57type Stream m a = S.Stream m a
59{-# INLINE sourceUnfoldr #-}
60sourceUnfoldr :: Monad m => Int -> Stream m Int
61sourceUnfoldr n = S.unfoldr step n
66 else Just (cnt, cnt + 1)
68{-# INLINE sourceUnfoldrMN #-}
69sourceUnfoldrMN :: Monad m => Int -> Int -> Stream m Int
70sourceUnfoldrMN m n = S.unfoldrM step n
75 else return (Just (cnt, cnt + 1))
77{-# INLINE sourceUnfoldrM #-}
78sourceUnfoldrM :: Monad m => Int -> Stream m Int
79sourceUnfoldrM n = S.unfoldrM step n
84 else return (Just (cnt, cnt + 1))
86{-# INLINE sourceFromEnum #-}
87sourceFromEnum :: Monad m => Int -> Stream m Int
88sourceFromEnum n = S.enumFromStepN n 1 value
90{-# INLINE sourceFromList #-}
91sourceFromList :: Monad m => Int -> Stream m Int
92sourceFromList n = S.fromList [n..n+value]
95source :: Monad m => Int -> Stream m Int
96source = sourceUnfoldrM
98-------------------------------------------------------------------------------
100-------------------------------------------------------------------------------
102{-# INLINE runStream #-}
103runStream :: Monad m => Stream m a -> m ()
104runStream = S.runStream
112 Just (_, t) -> uncons t
115tail :: Monad m => Stream m a -> m ()
116tail s = S.tail s >>= mapM_ tail
120 when (not r) $ S.tail s >>= mapM_ nullTail
124 when (isJust h) $ S.tail s >>= mapM_ headTail
127foldl = S.foldl' (+) 0
130-------------------------------------------------------------------------------
132-------------------------------------------------------------------------------
134{-# INLINE transform #-}
135transform :: Monad m => Stream m a -> m ()
138{-# INLINE composeN #-}
141 => Int -> (Stream m Int -> Stream m Int) -> Stream m Int -> m ()
145 2 -> transform . f . f
146 3 -> transform . f . f . f
147 4 -> transform . f . f . f . f
154{-# INLINE mapMaybe #-}
155{-# INLINE mapMaybeM #-}
156{-# INLINE filterEven #-}
157{-# INLINE filterAllOut #-}
158{-# INLINE filterAllIn #-}
159{-# INLINE takeOne #-}
160{-# INLINE takeAll #-}
161{-# INLINE takeWhileTrue #-}
162{-# INLINE takeWhileMTrue #-}
163{-# INLINE dropOne #-}
164{-# INLINE dropAll #-}
165{-# INLINE dropWhileTrue #-}
166{-# INLINE dropWhileMTrue #-}
167{-# INLINE dropWhileFalse #-}
168scan, map, fmap, mapM, mapMaybe, mapMaybeM, filterEven, filterAllOut,
169 filterAllIn, takeOne, takeAll, takeWhileTrue, takeWhileMTrue, dropOne,
170 dropAll, dropWhileTrue, dropWhileMTrue, dropWhileFalse
172 => Int -> Stream m Int -> m ()
174scan n = composeN n $ S.scanl' (+) 0
175fmap n = composeN n $ Prelude.fmap (+1)
176map n = composeN n $ S.map (+1)
177mapM n = composeN n $ S.mapM return
178mapMaybe n = composeN n $ S.mapMaybe
179 (\x -> if Prelude.odd x then Nothing else Just x)
180mapMaybeM n = composeN n $ S.mapMaybeM
181 (\x -> if Prelude.odd x then return Nothing else return $ Just x)
182filterEven n = composeN n $ S.filter even
183filterAllOut n = composeN n $ S.filter (> maxValue)
184filterAllIn n = composeN n $ S.filter (<= maxValue)
185takeOne n = composeN n $ S.take 1
186takeAll n = composeN n $ S.take maxValue
187takeWhileTrue n = composeN n $ S.takeWhile (<= maxValue)
188takeWhileMTrue n = composeN n $ S.takeWhileM (return . (<= maxValue))
189dropOne n = composeN n $ S.drop 1
190dropAll n = composeN n $ S.drop maxValue
191dropWhileTrue n = composeN n $ S.dropWhile (<= maxValue)
192dropWhileMTrue n = composeN n $ S.dropWhileM (return . (<= maxValue))
193dropWhileFalse n = composeN n $ S.dropWhile (> maxValue)
195-------------------------------------------------------------------------------
197-------------------------------------------------------------------------------
199iterStreamLen, maxIters :: Int
203{-# INLINE iterateSource #-}
206 => (Stream m Int -> Stream m Int) -> Int -> Int -> Stream m Int
207iterateSource g i n = f i (sourceUnfoldrMN iterStreamLen n)
210 f x m = g (f (x P.- 1) m)
212{-# INLINE iterateMapM #-}
213{-# INLINE iterateScan #-}
214{-# INLINE iterateFilterEven #-}
215{-# INLINE iterateTakeAll #-}
216{-# INLINE iterateDropOne #-}
217{-# INLINE iterateDropWhileFalse #-}
218{-# INLINE iterateDropWhileTrue #-}
219iterateMapM, iterateScan, iterateFilterEven, iterateTakeAll, iterateDropOne,
220 iterateDropWhileFalse, iterateDropWhileTrue
222 => Int -> Stream m Int
225iterateScan = iterateSource (S.scanl' (+) 0) (maxIters `div` 10)
226iterateDropWhileFalse = iterateSource (S.dropWhile (> maxValue))
229iterateMapM = iterateSource (S.mapM return) maxIters
230iterateFilterEven = iterateSource (S.filter even) maxIters
231iterateTakeAll = iterateSource (S.take maxValue) maxIters
232iterateDropOne = iterateSource (S.drop 1) maxIters
233iterateDropWhileTrue = iterateSource (S.dropWhile (<= maxValue)) maxIters
235-------------------------------------------------------------------------------
237-------------------------------------------------------------------------------
239zip src = transform $ S.zipWith (,) src src
240-- concat _n = return ()
242-------------------------------------------------------------------------------
244-------------------------------------------------------------------------------
246{-# INLINE scanMap #-}
247{-# INLINE dropMap #-}
248{-# INLINE dropScan #-}
249{-# INLINE takeDrop #-}
250{-# INLINE takeScan #-}
251{-# INLINE takeMap #-}
252{-# INLINE filterDrop #-}
253{-# INLINE filterTake #-}
254{-# INLINE filterScan #-}
255{-# INLINE filterMap #-}
256scanMap, dropMap, dropScan, takeDrop, takeScan, takeMap, filterDrop,
257 filterTake, filterScan, filterMap
258 :: Monad m => Int -> Stream m Int -> m ()
260scanMap n = composeN n $ S.map (subtract 1) . S.scanl' (+) 0
261dropMap n = composeN n $ S.map (subtract 1) . S.drop 1
262dropScan n = composeN n $ S.scanl' (+) 0 . S.drop 1
263takeDrop n = composeN n $ S.drop 1 . S.take maxValue
264takeScan n = composeN n $ S.scanl' (+) 0 . S.take maxValue
265takeMap n = composeN n $ S.map (subtract 1) . S.take maxValue
266filterDrop n = composeN n $ S.drop 1 . S.filter (<= maxValue)
267filterTake n = composeN n $ S.take maxValue . S.filter (<= maxValue)
268filterScan n = composeN n $ S.scanl' (+) 0 . S.filter (<= maxBound)
269filterMap n = composeN n $ S.map (subtract 1) . 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