2-- Module : BenchmarkOps
3-- Copyright : (c) 2018 Harendra Kumar
6-- Maintainer : harendra.kumar@gmail.com
9{-# LANGUAGE FlexibleContexts #-}
13import Control.Monad (when)
14import Data.Maybe (fromJust)
16 (Monad, Int, (+), ($), (.), return, fmap, even, (>), (<=), (==), (<=),
17 subtract, undefined, Maybe(..), odd, Bool, not, (>>=), mapM_, curry)
19import qualified Streamly as S
20import qualified Streamly.Prelude as S
30-------------------------------------------------------------------------------
32-------------------------------------------------------------------------------
34-------------------------------------------------------------------------------
35-- Stream generation and elimination
36-------------------------------------------------------------------------------
38type Stream m a = S.SerialT m a
41source :: (S.MonadAsync m, S.IsStream t) => Int -> t m Int
42source n = S.serially $ sourceUnfoldrM n
43-- source n = S.serially $ sourceFromList n
45{-# INLINE sourceFromList #-}
46sourceFromList :: (Monad m, S.IsStream t) => Int -> t m Int
47sourceFromList n = S.fromList [n..n+value]
49{-# INLINE sourceFromListM #-}
50sourceFromListM :: (S.MonadAsync m, S.IsStream t) => Int -> t m Int
51sourceFromListM n = S.fromListM (Prelude.fmap return [n..n+value])
53{-# INLINE sourceFromFoldable #-}
54sourceFromFoldable :: S.IsStream t => Int -> t m Int
55sourceFromFoldable n = S.fromFoldable [n..n+value]
57{-# INLINE sourceFromFoldableM #-}
58sourceFromFoldableM :: (S.IsStream t, S.MonadAsync m) => Int -> t m Int
59sourceFromFoldableM n = S.fromFoldableM (Prelude.fmap return [n..n+value])
61{-# INLINE sourceFoldMapWith #-}
62sourceFoldMapWith :: (S.IsStream t, S.Semigroup (t m Int))
64sourceFoldMapWith n = S.foldMapWith (S.<>) S.yield [n..n+value]
66{-# INLINE sourceFoldMapWithM #-}
67sourceFoldMapWithM :: (S.IsStream t, Monad m, S.Semigroup (t m Int))
69sourceFoldMapWithM n = S.foldMapWith (S.<>) (S.yieldM . return) [n..n+value]
71{-# INLINE sourceUnfoldr #-}
72sourceUnfoldr :: (Monad m, S.IsStream t) => Int -> t m Int
73sourceUnfoldr n = S.unfoldr step n
78 else Just (cnt, cnt + 1)
80{-# INLINE sourceUnfoldrM #-}
81sourceUnfoldrM :: (S.IsStream t, S.MonadAsync m) => Int -> t m Int
82sourceUnfoldrM n = S.unfoldrM step n
87 else return (Just (cnt, cnt + 1))
89{-# INLINE sourceUnfoldrMN #-}
90sourceUnfoldrMN :: (S.IsStream t, S.MonadAsync m) => Int -> Int -> t m Int
91sourceUnfoldrMN m n = S.unfoldrM step n
96 else return (Just (cnt, cnt + 1))
98{-# INLINE sourceUnfoldrMAction #-}
99sourceUnfoldrMAction :: (S.IsStream t, S.MonadAsync m) => Int -> t m (m Int)
100sourceUnfoldrMAction n = S.serially $ S.unfoldrM step n
105 else return (Just (return cnt, cnt + 1))
107-------------------------------------------------------------------------------
109-------------------------------------------------------------------------------
111{-# INLINE runStream #-}
112runStream :: Monad m => Stream m a -> m ()
113runStream = S.runStream
118toList, foldr, foldrM :: Monad m => Stream m Int -> m [Int]
121{-# INLINE maximum #-}
122{-# INLINE minimum #-}
124{-# INLINE findIndex #-}
125{-# INLINE elemIndex #-}
126{-# INLINE foldl1' #-}
128last, minimum, maximum, find, findIndex, elemIndex, foldl1', foldr1 :: Monad m => Stream m Int -> m (Maybe Int)
133{-# INLINE product #-}
134foldl', length, sum, product :: Monad m => Stream m Int -> m Int
141{-# INLINE notElem #-}
142elem, notElem, all, any, and, or :: Monad m => Stream m Int -> m Bool
145toNull :: Monad m => (t m Int -> S.SerialT m Int) -> t m Int -> m ()
146toNull t = runStream . t
149uncons :: Monad m => Stream m Int -> m ()
154 Just (_, t) -> uncons t
157init :: Monad m => Stream m a -> m ()
158init s = S.init s >>= Prelude.mapM_ S.runStream
161tail :: Monad m => Stream m a -> m ()
162tail s = S.tail s >>= Prelude.mapM_ tail
164{-# INLINE nullHeadTail #-}
165nullHeadTail :: Monad m => Stream m Int -> m ()
170 S.tail s >>= Prelude.mapM_ nullHeadTail
172mapM_ = S.mapM_ (\_ -> return ())
174foldr = S.foldr (:) []
176foldrM = S.foldrM (\a xs -> return (a : xs)) []
177foldl' = S.foldl' (+) 0
178foldl1' = S.foldl1' (+)
180elem = S.elem maxValue
181notElem = S.notElem maxValue
183all = S.all (<= maxValue)
184any = S.any (> maxValue)
185and = S.and . S.map (<= maxValue)
186or = S.or . S.map (> maxValue)
187find = S.find (== maxValue)
188findIndex = S.findIndex (== maxValue)
189elemIndex = S.elemIndex maxValue
195-------------------------------------------------------------------------------
197-------------------------------------------------------------------------------
199{-# INLINE transform #-}
200transform :: Monad m => Stream m a -> m ()
207{-# INLINE mapMaybe #-}
208{-# INLINE filterEven #-}
209{-# INLINE filterAllOut #-}
210{-# INLINE filterAllIn #-}
211{-# INLINE takeOne #-}
212{-# INLINE takeAll #-}
213{-# INLINE takeWhileTrue #-}
214{-# INLINE takeWhileMTrue #-}
215{-# INLINE dropAll #-}
216{-# INLINE dropWhileTrue #-}
217{-# INLINE dropWhileMTrue #-}
218{-# INLINE findIndices #-}
219{-# INLINE elemIndices #-}
220scan, mapM_, map, fmap, mapMaybe, filterEven, filterAllOut,
221 filterAllIn, takeOne, takeAll, takeWhileTrue, takeWhileMTrue, dropAll,
222 dropWhileTrue, dropWhileMTrue,
223 findIndices, elemIndices
225 => Stream m Int -> m ()
227{-# INLINE mapMaybeM #-}
228mapMaybeM :: S.MonadAsync m => Stream m Int -> m ()
231mapM :: (S.IsStream t, S.MonadAsync m)
232 => (t m Int -> S.SerialT m Int) -> t m Int -> m ()
234{-# INLINE sequence #-}
235sequence :: (S.IsStream t, S.MonadAsync m)
236 => (t m Int -> S.SerialT m Int) -> t m (m Int) -> m ()
238scan = transform . S.scanl' (+) 0
239fmap = transform . Prelude.fmap (+1)
240map = transform . S.map (+1)
241mapM t = transform . t . S.mapM return
242mapMaybe = transform . S.mapMaybe
243 (\x -> if Prelude.odd x then Nothing else Just ())
244mapMaybeM = transform . S.mapMaybeM
245 (\x -> if Prelude.odd x then return Nothing else return $ Just ())
246sequence t = transform . t . S.sequence
247filterEven = transform . S.filter even
248filterAllOut = transform . S.filter (> maxValue)
249filterAllIn = transform . S.filter (<= maxValue)
250takeOne = transform . S.take 1
251takeAll = transform . S.take maxValue
252takeWhileTrue = transform . S.takeWhile (<= maxValue)
253takeWhileMTrue = transform . S.takeWhileM (return . (<= maxValue))
254dropAll = transform . S.drop maxValue
255dropWhileTrue = transform . S.dropWhile (<= maxValue)
256dropWhileMTrue = transform . S.dropWhileM (return . (<= maxValue))
257findIndices = transform . S.findIndices (== maxValue)
258elemIndices = transform . S.elemIndices maxValue
260-------------------------------------------------------------------------------
262-------------------------------------------------------------------------------
267zip, zipM, concat :: Monad m => Stream m Int -> m ()
269{-# INLINE zipAsync #-}
270{-# INLINE zipAsyncM #-}
271zipAsync, zipAsyncM :: S.MonadAsync m => Stream m Int -> m ()
275 let src1 = fromJust r
276 transform (S.zipWith (,) src src1)
279 let src1 = fromJust r
280 transform (S.zipWithM (curry return) src src1)
283 let src1 = fromJust r
284 transform (S.zipAsyncWith (,) src src1)
287 let src1 = fromJust r
288 transform (S.zipAsyncWithM (curry return) src src1)
291-------------------------------------------------------------------------------
293-------------------------------------------------------------------------------
295{-# INLINE compose #-}
296compose :: Monad m => (Stream m Int -> Stream m Int) -> Stream m Int -> m ()
297compose f = transform . f . f . f . f
299{-# INLINE composeMapM #-}
300{-# INLINE composeAllInFilters #-}
301{-# INLINE composeAllOutFilters #-}
302{-# INLINE composeMapAllInFilter #-}
303composeAllInFilters, composeAllOutFilters,
304 composeMapAllInFilter
306 => Stream m Int -> m ()
307composeMapM :: S.MonadAsync m => Stream m Int -> m ()
309composeMapM = compose (S.mapM return)
310composeAllInFilters = compose (S.filter (<= maxValue))
311composeAllOutFilters = compose (S.filter (> maxValue))
312composeMapAllInFilter =
313 compose (S.filter (<= maxValue) . Prelude.fmap (subtract 1))
315{-# INLINABLE composeScaling #-}
316composeScaling :: Monad m => Int -> Stream m Int -> m ()
320 2 -> transform . f . f
321 3 -> transform . f . f . f
322 4 -> transform . f . f . f . f
324 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