@@ -0,0 +1,127 @@
1+-- |
2+-- Module : BenchmarkOps
3+-- Copyright : (c) 2018 Harendra Kumar
4+--
5+-- License : MIT
6+-- Maintainer : harendra.kumar@gmail.com
7+
8+module BenchmarkOps where
9+
10+import Prelude
11+ (Monad, Int, (+), id, ($), (.), return, fmap, even, (>), (<=),
12+ subtract, undefined, Maybe)
13+
14+import qualified Streamly as S
15+import qualified Streamly.Prelude as S
16+
17+value, maxValue :: Int
18+value = 1000000
19+maxValue = value + 1000
20+
21+-------------------------------------------------------------------------------
22+-- Benchmark ops
23+-------------------------------------------------------------------------------
24+
25+{-# INLINE toNull #-}
26+{-# INLINE toList #-}
27+{-# INLINE foldl #-}
28+{-# INLINE last #-}
29+{-# INLINE scan #-}
30+{-# INLINE map #-}
31+{-# INLINE filterEven #-}
32+{-# INLINE mapM #-}
33+{-# INLINE filterAllOut #-}
34+{-# INLINE filterAllIn #-}
35+{-# INLINE takeOne #-}
36+{-# INLINE takeAll #-}
37+{-# INLINE takeWhileTrue #-}
38+{-# INLINE dropAll #-}
39+{-# INLINE dropWhileTrue #-}
40+{-# INLINE zip #-}
41+{-# INLINE concat #-}
42+{-# INLINE composeMapM #-}
43+{-# INLINE composeAllInFilters #-}
44+{-# INLINE composeAllOutFilters #-}
45+{-# INLINE composeMapAllInFilter #-}
46+toNull, scan, map, filterEven, mapM, filterAllOut,
47+ filterAllIn, takeOne, takeAll, takeWhileTrue, dropAll, dropWhileTrue, zip,
48+ concat, composeMapM, composeAllInFilters, composeAllOutFilters,
49+ composeMapAllInFilter
50+ :: Monad m
51+ => Stream m Int -> m ()
52+
53+toList :: Monad m => Stream m Int -> m [Int]
54+foldl :: Monad m => Stream m Int -> m Int
55+last :: Monad m => Stream m Int -> m (Maybe Int)
56+
57+-------------------------------------------------------------------------------
58+-- Stream generation and elimination
59+-------------------------------------------------------------------------------
60+
61+type Stream m a = S.StreamT m a
62+
63+source :: Int -> Stream m Int
64+source n = S.each [n..n+value]
65+
66+{-# INLINE runStream #-}
67+runStream :: Monad m => Stream m a -> m ()
68+runStream = S.runStreamT
69+
70+-------------------------------------------------------------------------------
71+-- Elimination
72+-------------------------------------------------------------------------------
73+
74+toNull = runStream
75+toList = S.toList
76+foldl = S.foldl (+) 0 id
77+last = S.last
78+
79+-------------------------------------------------------------------------------
80+-- Transformation
81+-------------------------------------------------------------------------------
82+
83+{-# INLINE transform #-}
84+transform :: Monad m => Stream m a -> m ()
85+transform = runStream
86+
87+scan = transform . S.scan (+) 0 id
88+map = transform . fmap (+1)
89+mapM = transform . S.mapM return
90+filterEven = transform . S.filter even
91+filterAllOut = transform . S.filter (> maxValue)
92+filterAllIn = transform . S.filter (<= maxValue)
93+takeOne = transform . S.take 1
94+takeAll = transform . S.take maxValue
95+takeWhileTrue = transform . S.takeWhile (<= maxValue)
96+dropAll = transform . S.drop maxValue
97+dropWhileTrue = transform . S.dropWhile (<= maxValue)
98+
99+-------------------------------------------------------------------------------
100+-- Zipping and concat
101+-------------------------------------------------------------------------------
102+
103+zip src = transform $ (S.zipWith (,) src src)
104+concat _n = return ()
105+
106+-------------------------------------------------------------------------------
107+-- Composition
108+-------------------------------------------------------------------------------
109+
110+{-# INLINE compose #-}
111+compose :: Monad m => (Stream m Int -> Stream m Int) -> Stream m Int -> m ()
112+compose f = transform . f . f . f . f
113+
114+composeMapM = compose (S.mapM return)
115+composeAllInFilters = compose (S.filter (<= maxValue))
116+composeAllOutFilters = compose (S.filter (> maxValue))
117+composeMapAllInFilter = compose (S.filter (<= maxValue) . fmap (subtract 1))
118+
119+composeScaling :: Monad m => Int -> Stream m Int -> m ()
120+composeScaling m =
121+ case m of
122+ 1 -> transform . f
123+ 2 -> transform . f . f
124+ 3 -> transform . f . f . f
125+ 4 -> transform . f . f . f . f
126+ _ -> undefined
127+ where f = S.filter (<= maxValue)