on 5cbcd5b help sign in
streamly/benchmark/StreamDOps.hs
175 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 Control.Monad (when)
13import Prelude
14 (Monad, Int, (+), ($), (.), return, (>), even, (<=),
15 subtract, undefined, Maybe(..), not, mapM_, (>>=))
17import qualified Streamly.Streams.StreamD as S
19value, maxValue :: Int
20value = 100000
21maxValue = value + 1000
23-------------------------------------------------------------------------------
24-- Benchmark ops
25-------------------------------------------------------------------------------
27{-# INLINE uncons #-}
28{-# INLINE nullHeadTail #-}
29{-# INLINE scan #-}
30{-# INLINE map #-}
31{-# INLINE filterEven #-}
32{-# INLINE filterAllOut #-}
33{-# INLINE filterAllIn #-}
34{-# INLINE takeOne #-}
35{-# INLINE takeAll #-}
36{-# INLINE takeWhileTrue #-}
37{-# INLINE dropAll #-}
38{-# INLINE dropWhileTrue #-}
39{-# INLINE zip #-}
40{-
41{-# INLINE concat #-}
42-}
43{-# INLINE composeAllInFilters #-}
44{-# INLINE composeAllOutFilters #-}
45{-# INLINE composeMapAllInFilter #-}
46uncons, nullHeadTail, map, scan, filterEven, filterAllOut,
47 filterAllIn, takeOne, takeAll, takeWhileTrue, dropAll, dropWhileTrue, zip,
48 -- concat,
49 composeAllInFilters, composeAllOutFilters, composeMapAllInFilter
50 :: Monad m
51 => Stream m Int -> m ()
53{-# INLINE composeMapM #-}
54composeMapM :: Monad m => Stream m Int -> m ()
56{-# INLINE toList #-}
57toList :: Monad m => Stream m Int -> m [Int]
58{-# INLINE foldl #-}
59foldl :: Monad m => Stream m Int -> m Int
60{-# INLINE last #-}
61last :: Monad m => Stream m Int -> m (Maybe Int)
63{-# INLINE toNull #-}
64{-# INLINE mapM #-}
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
76 where
77 step cnt =
78 if cnt > n + value
79 then Nothing
80 else Just (cnt, cnt + 1)
82{-# INLINE sourceUnfoldrM #-}
83sourceUnfoldrM :: Monad m => Int -> Stream m Int
84sourceUnfoldrM n = S.unfoldrM step n
85 where
86 step cnt =
87 if cnt > n + value
88 then return Nothing
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]
99{-# INLINE source #-}
100source :: Monad m => Int -> Stream m Int
101source = sourceUnfoldrM
103-------------------------------------------------------------------------------
104-- Elimination
105-------------------------------------------------------------------------------
107{-# INLINE runStream #-}
108runStream :: Monad m => Stream m a -> m ()
109runStream = S.runStream
111toNull = runStream
112uncons s = do
113 r <- S.uncons s
114 case r of
115 Nothing -> return ()
116 Just (_, t) -> uncons t
117nullHeadTail s = do
118 r <- S.null s
119 when (not r) $ do
120 _ <- S.head s
121 S.tail s >>= mapM_ nullHeadTail
122toList = S.toList
123foldl = S.foldl' (+) 0
124last = S.last
126-------------------------------------------------------------------------------
127-- Transformation
128-------------------------------------------------------------------------------
130{-# INLINE transform #-}
131transform :: Monad m => Stream m a -> m ()
132transform = runStream
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-------------------------------------------------------------------------------
147-- Zipping and concat
148-------------------------------------------------------------------------------
150zip src = transform $ S.zipWith (,) src src
151-- concat _n = return ()
153-------------------------------------------------------------------------------
154-- Composition
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 ()
168composeScaling m =
169 case m of
170 1 -> transform . f
171 2 -> transform . f . f
172 3 -> transform . f . f . f
173 4 -> transform . f . f . f . f
174 _ -> undefined
175 where f = S.filter (<= maxValue)