@@ -0,0 +1,127 @@
2+-- Module : BenchmarkOps
3+-- Copyright : (c) 2018 Harendra Kumar
6+-- Maintainer : harendra.kumar@gmail.com
8+module BenchmarkOps where
11+ (Monad, Int, (+), id, ($), (.), return, fmap, even, (>), (<=),
12+ subtract, undefined, Maybe)
14+import qualified Streamly as S
15+import qualified Streamly.Prelude as S
17+value, maxValue :: Int
19+maxValue = value + 1000
21+-------------------------------------------------------------------------------
23+-------------------------------------------------------------------------------
31+{-# INLINE filterEven #-}
33+{-# INLINE filterAllOut #-}
34+{-# INLINE filterAllIn #-}
35+{-# INLINE takeOne #-}
36+{-# INLINE takeAll #-}
37+{-# INLINE takeWhileTrue #-}
38+{-# INLINE dropAll #-}
39+{-# INLINE dropWhileTrue #-}
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
51+ => Stream m Int -> m ()
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)
57+-------------------------------------------------------------------------------
58+-- Stream generation and elimination
59+-------------------------------------------------------------------------------
61+type Stream m a = S.StreamT m a
63+source :: Int -> Stream m Int
64+source n = S.each [n..n+value]
66+{-# INLINE runStream #-}
67+runStream :: Monad m => Stream m a -> m ()
68+runStream = S.runStreamT
70+-------------------------------------------------------------------------------
72+-------------------------------------------------------------------------------
76+foldl = S.foldl (+) 0 id
79+-------------------------------------------------------------------------------
81+-------------------------------------------------------------------------------
83+{-# INLINE transform #-}
84+transform :: Monad m => Stream m a -> m ()
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)
99+-------------------------------------------------------------------------------
100+-- Zipping and concat
101+-------------------------------------------------------------------------------
103+zip src = transform $ (S.zipWith (,) src src)
104+concat _n = return ()
106+-------------------------------------------------------------------------------
108+-------------------------------------------------------------------------------
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
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))
119+composeScaling :: Monad m => Int -> Stream m Int -> m ()
123+ 2 -> transform . f . f
124+ 3 -> transform . f . f . f
125+ 4 -> transform . f . f . f . f
127+ where f = S.filter (<= maxValue)