3-- Copyright : (c) 2018 Harendra Kumar
6-- Maintainer : harendra.kumar@gmail.com
8{-# LANGUAGE FlexibleContexts #-}
10module StreamDOps where
13 (Monad, Int, (+), ($), (.), return, (>), even, (<=),
14 subtract, undefined, Maybe(..), not)
16import qualified Streamly.Streams.StreamD as S
20maxValue = value + 1000
22-------------------------------------------------------------------------------
24-------------------------------------------------------------------------------
27{-# INLINE nullHeadTail #-}
30{-# INLINE filterEven #-}
31{-# INLINE filterAllOut #-}
32{-# INLINE filterAllIn #-}
35{-# INLINE takeWhileTrue #-}
37{-# INLINE dropWhileTrue #-}
42{-# INLINE composeAllInFilters #-}
43{-# INLINE composeAllOutFilters #-}
44{-# INLINE composeMapAllInFilter #-}
45uncons, nullHeadTail, map, scan, filterEven, filterAllOut,
46 filterAllIn, takeOne, takeAll, takeWhileTrue, dropAll, dropWhileTrue, zip,
48 composeAllInFilters, composeAllOutFilters, composeMapAllInFilter
50 => Stream m Int -> m ()
52{-# INLINE composeMapM #-}
53composeMapM :: Monad m => Stream m Int -> m ()
56toList :: Monad m => Stream m Int -> m [Int]
58foldl :: Monad m => Stream m Int -> m Int
60last :: Monad m => Stream m Int -> m (Maybe Int)
64toNull, mapM :: Monad m => Stream m Int -> m ()
66-------------------------------------------------------------------------------
67-- Stream generation and elimination
68-------------------------------------------------------------------------------
70type Stream m a = S.Stream m a
72{-# INLINE sourceUnfoldr #-}
73sourceUnfoldr :: Monad m => Int -> Stream m Int
74sourceUnfoldr n = S.unfoldr step n
79 else (Just (cnt, cnt + 1))
81{-# INLINE sourceUnfoldrM #-}
82sourceUnfoldrM :: Monad m => Int -> Stream m Int
83sourceUnfoldrM n = S.unfoldrM step n
88 else return (Just (cnt, cnt + 1))
90{-# INLINE sourceFromEnum #-}
91sourceFromEnum :: Monad m => Int -> Stream m Int
92sourceFromEnum n = S.enumFromStepN n 1 value
94{-# INLINE sourceFromList #-}
95sourceFromList :: Monad m => Int -> Stream m Int
96sourceFromList n = S.fromList [n..n+value]
99source :: Monad m => Int -> Stream m Int
100source n = sourceUnfoldrM n
102-------------------------------------------------------------------------------
104-------------------------------------------------------------------------------
106{-# INLINE runStream #-}
107runStream :: Monad m => Stream m a -> m ()
108runStream = S.runStream
115 Just (_, t) -> uncons t
124 Just x -> nullHeadTail x
127foldl = S.foldl' (+) 0
130-------------------------------------------------------------------------------
132-------------------------------------------------------------------------------
134{-# INLINE transform #-}
135transform :: Monad m => Stream m a -> m ()
138scan = transform . S.scanlM' (\a b -> return (a + b)) 0
139map = transform . S.map (+1)
140mapM = transform . S.mapM return
141filterEven = transform . S.filter even
142filterAllOut = transform . S.filter (> maxValue)
143filterAllIn = transform . S.filter (<= maxValue)
144takeOne = transform . S.take 1
145takeAll = transform . S.take maxValue
146takeWhileTrue = transform . S.takeWhile (<= maxValue)
147dropAll = transform . S.drop maxValue
148dropWhileTrue = transform . S.dropWhile (<= maxValue)
150-------------------------------------------------------------------------------
152-------------------------------------------------------------------------------
154zip src = transform $ (S.zipWith (,) src src)
155-- concat _n = return ()
157-------------------------------------------------------------------------------
159-------------------------------------------------------------------------------
161{-# INLINE compose #-}
162compose :: Monad m => (Stream m Int -> Stream m Int) -> Stream m Int -> m ()
163compose f = transform . f . f . f . f
165composeMapM = compose (S.mapM return)
166composeAllInFilters = compose (S.filter (<= maxValue))
167composeAllOutFilters = compose (S.filter (> maxValue))
168composeMapAllInFilter = compose (S.filter (<= maxValue) . S.map (subtract 1))
170{-# INLINABLE composeScaling #-}
171composeScaling :: Monad m => Int -> Stream m Int -> m ()
175 2 -> transform . f . f
176 3 -> transform . f . f . f
177 4 -> transform . f . f . f . f
179 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