3-- Copyright : (c) 2018 Harendra Kumar
6-- Maintainer : harendra.kumar@gmail.com
8{-# LANGUAGE FlexibleContexts #-}
10module StreamDOps where
12import Control.Monad (when)
14 (Monad, Int, (+), ($), (.), return, (>), even, (<=),
15 subtract, undefined, Maybe(..), not, mapM_, (>>=))
17import qualified Streamly.Streams.StreamD as S
21maxValue = value + 1000
23-------------------------------------------------------------------------------
25-------------------------------------------------------------------------------
28{-# INLINE nullHeadTail #-}
31{-# INLINE filterEven #-}
32{-# INLINE filterAllOut #-}
33{-# INLINE filterAllIn #-}
36{-# INLINE takeWhileTrue #-}
38{-# INLINE dropWhileTrue #-}
43{-# INLINE composeAllInFilters #-}
44{-# INLINE composeAllOutFilters #-}
45{-# INLINE composeMapAllInFilter #-}
46uncons, nullHeadTail, map, scan, filterEven, filterAllOut,
47 filterAllIn, takeOne, takeAll, takeWhileTrue, dropAll, dropWhileTrue, zip,
49 composeAllInFilters, composeAllOutFilters, composeMapAllInFilter
51 => Stream m Int -> m ()
53{-# INLINE composeMapM #-}
54composeMapM :: Monad m => Stream m Int -> m ()
57toList :: Monad m => Stream m Int -> m [Int]
59foldl :: Monad m => Stream m Int -> m Int
61last :: Monad m => Stream m Int -> m (Maybe Int)
65toNull, mapM :: Monad m => Stream m Int -> m ()
67-------------------------------------------------------------------------------
68-- Stream generation and elimination
69-------------------------------------------------------------------------------
71type Stream m a = S.Stream m a
73{-# INLINE sourceUnfoldr #-}
74sourceUnfoldr :: Monad m => Int -> Stream m Int
75sourceUnfoldr n = S.unfoldr step n
80 else Just (cnt, cnt + 1)
82{-# INLINE sourceUnfoldrM #-}
83sourceUnfoldrM :: Monad m => Int -> Stream m Int
84sourceUnfoldrM n = S.unfoldrM step n
89 else return (Just (cnt, cnt + 1))
91{-# INLINE sourceFromEnum #-}
92sourceFromEnum :: Monad m => Int -> Stream m Int
93sourceFromEnum n = S.enumFromStepN n 1 value
95{-# INLINE sourceFromList #-}
96sourceFromList :: Monad m => Int -> Stream m Int
97sourceFromList n = S.fromList [n..n+value]
100source :: Monad m => Int -> Stream m Int
101source = sourceUnfoldrM
103-------------------------------------------------------------------------------
105-------------------------------------------------------------------------------
107{-# INLINE runStream #-}
108runStream :: Monad m => Stream m a -> m ()
109runStream = S.runStream
116 Just (_, t) -> uncons t
121 S.tail s >>= mapM_ nullHeadTail
123foldl = S.foldl' (+) 0
126-------------------------------------------------------------------------------
128-------------------------------------------------------------------------------
130{-# INLINE transform #-}
131transform :: Monad m => Stream m a -> m ()
134scan = transform . S.scanlM' (\a b -> return (a + b)) 0
135map = transform . S.map (+1)
136mapM = transform . S.mapM return
137filterEven = transform . S.filter even
138filterAllOut = transform . S.filter (> maxValue)
139filterAllIn = transform . S.filter (<= maxValue)
140takeOne = transform . S.take 1
141takeAll = transform . S.take maxValue
142takeWhileTrue = transform . S.takeWhile (<= maxValue)
143dropAll = transform . S.drop maxValue
144dropWhileTrue = transform . S.dropWhile (<= maxValue)
146-------------------------------------------------------------------------------
148-------------------------------------------------------------------------------
150zip src = transform $ S.zipWith (,) src src
151-- concat _n = return ()
153-------------------------------------------------------------------------------
155-------------------------------------------------------------------------------
157{-# INLINE compose #-}
158compose :: Monad m => (Stream m Int -> Stream m Int) -> Stream m Int -> m ()
159compose f = transform . f . f . f . f
161composeMapM = compose (S.mapM return)
162composeAllInFilters = compose (S.filter (<= maxValue))
163composeAllOutFilters = compose (S.filter (> maxValue))
164composeMapAllInFilter = compose (S.filter (<= maxValue) . S.map (subtract 1))
166{-# INLINABLE composeScaling #-}
167composeScaling :: Monad m => Int -> Stream m Int -> m ()
171 2 -> transform . f . f
172 3 -> transform . f . f . f
173 4 -> transform . f . f . f . f
175 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