on ec08681 help sign in
streamly/benchmark/StreamDOps.hs
179 lines 5.3 kB history raw
1-- |
2-- Module : StreamDOps
3-- Copyright : (c) 2018 Harendra Kumar
4--
5-- License : BSD3
6-- Maintainer : harendra.kumar@gmail.com
8{-# LANGUAGE FlexibleContexts #-}
10module StreamDOps where
12import Prelude
13 (Monad, Int, (+), ($), (.), return, (>), even, (<=),
14 subtract, undefined, Maybe(..), not)
16import qualified Streamly.Streams.StreamD as S
18value, maxValue :: Int
19value = 100000
20maxValue = value + 1000
22-------------------------------------------------------------------------------
23-- Benchmark ops
24-------------------------------------------------------------------------------
26{-# INLINE uncons #-}
27{-# INLINE nullHeadTail #-}
28{-# INLINE scan #-}
29{-# INLINE map #-}
30{-# INLINE filterEven #-}
31{-# INLINE filterAllOut #-}
32{-# INLINE filterAllIn #-}
33{-# INLINE takeOne #-}
34{-# INLINE takeAll #-}
35{-# INLINE takeWhileTrue #-}
36{-# INLINE dropAll #-}
37{-# INLINE dropWhileTrue #-}
38{-# INLINE zip #-}
39{-
40{-# INLINE concat #-}
41-}
42{-# INLINE composeAllInFilters #-}
43{-# INLINE composeAllOutFilters #-}
44{-# INLINE composeMapAllInFilter #-}
45uncons, nullHeadTail, map, scan, filterEven, filterAllOut,
46 filterAllIn, takeOne, takeAll, takeWhileTrue, dropAll, dropWhileTrue, zip,
47 -- concat,
48 composeAllInFilters, composeAllOutFilters, composeMapAllInFilter
49 :: Monad m
50 => Stream m Int -> m ()
52{-# INLINE composeMapM #-}
53composeMapM :: Monad m => Stream m Int -> m ()
55{-# INLINE toList #-}
56toList :: Monad m => Stream m Int -> m [Int]
57{-# INLINE foldl #-}
58foldl :: Monad m => Stream m Int -> m Int
59{-# INLINE last #-}
60last :: Monad m => Stream m Int -> m (Maybe Int)
62{-# INLINE toNull #-}
63{-# INLINE mapM #-}
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
75 where
76 step cnt =
77 if cnt > n + value
78 then Nothing
79 else (Just (cnt, cnt + 1))
81{-# INLINE sourceUnfoldrM #-}
82sourceUnfoldrM :: Monad m => Int -> Stream m Int
83sourceUnfoldrM n = S.unfoldrM step n
84 where
85 step cnt =
86 if cnt > n + value
87 then return Nothing
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]
98{-# INLINE source #-}
99source :: Monad m => Int -> Stream m Int
100source n = sourceUnfoldrM n
102-------------------------------------------------------------------------------
103-- Elimination
104-------------------------------------------------------------------------------
106{-# INLINE runStream #-}
107runStream :: Monad m => Stream m a -> m ()
108runStream = S.runStream
110toNull = runStream
111uncons s = do
112 r <- S.uncons s
113 case r of
114 Nothing -> return ()
115 Just (_, t) -> uncons t
116nullHeadTail s = do
117 r <- S.null s
118 if not r
119 then do
120 _ <- S.head s
121 t <- S.tail s
122 case t of
123 Nothing -> return ()
124 Just x -> nullHeadTail x
125 else return ()
126toList = S.toList
127foldl = S.foldl' (+) 0
128last = S.last
130-------------------------------------------------------------------------------
131-- Transformation
132-------------------------------------------------------------------------------
134{-# INLINE transform #-}
135transform :: Monad m => Stream m a -> m ()
136transform = runStream
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-------------------------------------------------------------------------------
151-- Zipping and concat
152-------------------------------------------------------------------------------
154zip src = transform $ (S.zipWith (,) src src)
155-- concat _n = return ()
157-------------------------------------------------------------------------------
158-- Composition
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 ()
172composeScaling m =
173 case m of
174 1 -> transform . f
175 2 -> transform . f . f
176 3 -> transform . f . f . f
177 4 -> transform . f . f . f . f
178 _ -> undefined
179 where f = S.filter (<= maxValue)