2-- Module : BenchmarkOps
3-- Copyright : (c) 2018 Harendra Kumar
6-- Maintainer : harendra.kumar@gmail.com
9{-# LANGUAGE FlexibleContexts #-}
10{-# LANGUAGE ScopedTypeVariables #-}
14import Control.Monad (when)
15import Data.Maybe (fromJust)
17 (Monad, Int, (+), ($), (.), return, fmap, even, (>), (<=), (==), (<=),
18 subtract, undefined, Maybe(..), odd, Bool, not, (>>=), mapM_, curry,
20import qualified Prelude as P
22import qualified Streamly as S
23import qualified Streamly.Prelude as S
33-------------------------------------------------------------------------------
35-------------------------------------------------------------------------------
37-------------------------------------------------------------------------------
38-- Stream generation and elimination
39-------------------------------------------------------------------------------
41type Stream m a = S.SerialT m a
44source :: (S.MonadAsync m, S.IsStream t) => Int -> t m Int
45source n = S.serially $ sourceUnfoldrM n
46-- source n = S.serially $ sourceFromList n
48{-# INLINE sourceFromList #-}
49sourceFromList :: (Monad m, S.IsStream t) => Int -> t m Int
50sourceFromList n = S.fromList [n..n+value]
52{-# INLINE sourceFromListM #-}
53sourceFromListM :: (S.MonadAsync m, S.IsStream t) => Int -> t m Int
54sourceFromListM n = S.fromListM (Prelude.fmap return [n..n+value])
56{-# INLINE sourceFromFoldable #-}
57sourceFromFoldable :: S.IsStream t => Int -> t m Int
58sourceFromFoldable n = S.fromFoldable [n..n+value]
60{-# INLINE sourceFromFoldableM #-}
61sourceFromFoldableM :: (S.IsStream t, S.MonadAsync m) => Int -> t m Int
62sourceFromFoldableM n = S.fromFoldableM (Prelude.fmap return [n..n+value])
64{-# INLINE sourceFoldMapWith #-}
65sourceFoldMapWith :: (S.IsStream t, S.Semigroup (t m Int))
67sourceFoldMapWith n = S.foldMapWith (S.<>) S.yield [n..n+value]
69{-# INLINE sourceFoldMapWithM #-}
70sourceFoldMapWithM :: (S.IsStream t, Monad m, S.Semigroup (t m Int))
72sourceFoldMapWithM n = S.foldMapWith (S.<>) (S.yieldM . return) [n..n+value]
74{-# INLINE sourceUnfoldr #-}
75sourceUnfoldr :: (Monad m, S.IsStream t) => Int -> t m Int
76sourceUnfoldr n = S.unfoldr step n
81 else Just (cnt, cnt + 1)
83{-# INLINE sourceUnfoldrM #-}
84sourceUnfoldrM :: (S.IsStream t, S.MonadAsync m) => Int -> t m Int
85sourceUnfoldrM n = S.unfoldrM step n
90 else return (Just (cnt, cnt + 1))
92{-# INLINE sourceUnfoldrMN #-}
93sourceUnfoldrMN :: (S.IsStream t, S.MonadAsync m) => Int -> Int -> t m Int
94sourceUnfoldrMN m n = S.unfoldrM step n
99 else return (Just (cnt, cnt + 1))
101{-# INLINE sourceUnfoldrMAction #-}
102sourceUnfoldrMAction :: (S.IsStream t, S.MonadAsync m) => Int -> t m (m Int)
103sourceUnfoldrMAction n = S.serially $ S.unfoldrM step n
108 else return (Just (return cnt, cnt + 1))
110-------------------------------------------------------------------------------
112-------------------------------------------------------------------------------
114{-# INLINE runStream #-}
115runStream :: Monad m => Stream m a -> m ()
116runStream = S.runStream
121toList, foldr, foldrM :: Monad m => Stream m Int -> m [Int]
124{-# INLINE maximum #-}
125{-# INLINE minimum #-}
127{-# INLINE findIndex #-}
128{-# INLINE elemIndex #-}
129{-# INLINE foldl1' #-}
131last, minimum, maximum, find, findIndex, elemIndex, foldl1', foldr1 :: Monad m => Stream m Int -> m (Maybe Int)
136{-# INLINE product #-}
137foldl', length, sum, product :: Monad m => Stream m Int -> m Int
144{-# INLINE notElem #-}
145elem, notElem, all, any, and, or :: Monad m => Stream m Int -> m Bool
148toNull :: Monad m => (t m Int -> S.SerialT m Int) -> t m Int -> m ()
149toNull t = runStream . t
152uncons :: Monad m => Stream m Int -> m ()
157 Just (_, t) -> uncons t
160init :: Monad m => Stream m a -> m ()
161init s = S.init s >>= Prelude.mapM_ S.runStream
164tail :: Monad m => Stream m a -> m ()
165tail s = S.tail s >>= Prelude.mapM_ tail
167{-# INLINE nullHeadTail #-}
168nullHeadTail :: Monad m => Stream m Int -> m ()
173 S.tail s >>= Prelude.mapM_ nullHeadTail
176mapM_ :: Monad m => Stream m Int -> m ()
177mapM_ = S.mapM_ (\_ -> return ())
180foldr = S.foldr (:) []
182foldrM = S.foldrM (\a xs -> return (a : xs)) []
183foldl' = S.foldl' (+) 0
184foldl1' = S.foldl1' (+)
186elem = S.elem maxValue
187notElem = S.notElem maxValue
189all = S.all (<= maxValue)
190any = S.any (> maxValue)
191and = S.and . S.map (<= maxValue)
192or = S.or . S.map (> maxValue)
193find = S.find (== maxValue)
194findIndex = S.findIndex (== maxValue)
195elemIndex = S.elemIndex maxValue
201-------------------------------------------------------------------------------
203-------------------------------------------------------------------------------
205{-# INLINE transform #-}
206transform :: Monad m => Stream m a -> m ()
209{-# INLINE composeN #-}
212 => Int -> (Stream m Int -> Stream m Int) -> Stream m Int -> m ()
216 2 -> transform . f . f
217 3 -> transform . f . f . f
218 4 -> transform . f . f . f . f
221-- polymorphic stream version of composeN
222{-# INLINE composeN' #-}
224 :: (S.IsStream t, Monad m)
225 => Int -> (t m Int -> Stream m Int) -> t m Int -> m ()
229 2 -> transform . f . S.adapt . f
230 3 -> transform . f . S.adapt . f . S.adapt . f
231 4 -> transform . f . S.adapt . f . S.adapt . f . S.adapt . f
237{-# INLINE mapMaybe #-}
238{-# INLINE filterEven #-}
239{-# INLINE filterAllOut #-}
240{-# INLINE filterAllIn #-}
241{-# INLINE takeOne #-}
242{-# INLINE takeAll #-}
243{-# INLINE takeWhileTrue #-}
244{-# INLINE takeWhileMTrue #-}
245{-# INLINE dropOne #-}
246{-# INLINE dropAll #-}
247{-# INLINE dropWhileTrue #-}
248{-# INLINE dropWhileMTrue #-}
249{-# INLINE dropWhileFalse #-}
250{-# INLINE findIndices #-}
251{-# INLINE elemIndices #-}
252scan, map, fmap, mapMaybe, filterEven, filterAllOut,
253 filterAllIn, takeOne, takeAll, takeWhileTrue, takeWhileMTrue, dropOne,
254 dropAll, dropWhileTrue, dropWhileMTrue, dropWhileFalse,
255 findIndices, elemIndices
257 => Int -> Stream m Int -> m ()
259{-# INLINE mapMaybeM #-}
260mapMaybeM :: S.MonadAsync m => Int -> Stream m Int -> m ()
263mapM :: (S.IsStream t, S.MonadAsync m)
264 => (t m Int -> S.SerialT m Int) -> Int -> t m Int -> m ()
266{-# INLINE sequence #-}
267sequence :: (S.IsStream t, S.MonadAsync m)
268 => (t m Int -> S.SerialT m Int) -> t m (m Int) -> m ()
270scan n = composeN n $ S.scanl' (+) 0
271fmap n = composeN n $ Prelude.fmap (+1)
272map n = composeN n $ S.map (+1)
273mapM t n = composeN' n $ t . S.mapM return
274mapMaybe n = composeN n $ S.mapMaybe
275 (\x -> if Prelude.odd x then Nothing else Just x)
276mapMaybeM n = composeN n $ S.mapMaybeM
277 (\x -> if Prelude.odd x then return Nothing else return $ Just x)
278sequence t = transform . t . S.sequence
279filterEven n = composeN n $ S.filter even
280filterAllOut n = composeN n $ S.filter (> maxValue)
281filterAllIn n = composeN n $ S.filter (<= maxValue)
282takeOne n = composeN n $ S.take 1
283takeAll n = composeN n $ S.take maxValue
284takeWhileTrue n = composeN n $ S.takeWhile (<= maxValue)
285takeWhileMTrue n = composeN n $ S.takeWhileM (return . (<= maxValue))
286dropOne n = composeN n $ S.drop 1
287dropAll n = composeN n $ S.drop maxValue
288dropWhileTrue n = composeN n $ S.dropWhile (<= maxValue)
289dropWhileMTrue n = composeN n $ S.dropWhileM (return . (<= maxValue))
290dropWhileFalse n = composeN n $ S.dropWhile (> maxValue)
291findIndices n = composeN n $ S.findIndices (== maxValue)
292elemIndices n = composeN n $ S.elemIndices maxValue
294-------------------------------------------------------------------------------
296-------------------------------------------------------------------------------
298iterStreamLen, maxIters :: Int
302{-# INLINE iterateSource #-}
305 => (Stream m Int -> Stream m Int) -> Int -> Int -> Stream m Int
306iterateSource g i n = f i (sourceUnfoldrMN iterStreamLen n)
309 f x m = g (f (x P.- 1) m)
311{-# INLINE iterateMapM #-}
312{-# INLINE iterateScan #-}
313{-# INLINE iterateFilterEven #-}
314{-# INLINE iterateTakeAll #-}
315{-# INLINE iterateDropOne #-}
316{-# INLINE iterateDropWhileFalse #-}
317{-# INLINE iterateDropWhileTrue #-}
318iterateMapM, iterateScan, iterateFilterEven, iterateTakeAll, iterateDropOne,
319 iterateDropWhileFalse, iterateDropWhileTrue
321 => Int -> Stream m Int
324iterateScan = iterateSource (S.scanl' (+) 0) (maxIters `div` 10)
326iterateMapM = iterateSource (S.mapM return) maxIters
327iterateFilterEven = iterateSource (S.filter even) maxIters
328iterateTakeAll = iterateSource (S.take maxValue) maxIters
329iterateDropOne = iterateSource (S.drop 1) maxIters
330iterateDropWhileFalse = iterateSource (S.dropWhile (> maxValue)) maxIters
331iterateDropWhileTrue = iterateSource (S.dropWhile (<= maxValue)) maxIters
333-------------------------------------------------------------------------------
335-------------------------------------------------------------------------------
340zip, zipM, concat :: Monad m => Stream m Int -> m ()
344 let src1 = fromJust r
345 transform (S.zipWith (,) src src1)
348 let src1 = fromJust r
349 transform (S.zipWithM (curry return) src src1)
351{-# INLINE zipAsync #-}
352{-# INLINE zipAsyncM #-}
353zipAsync, zipAsyncM :: S.MonadAsync m => Stream m Int -> m ()
357 let src1 = fromJust r
358 transform (S.zipAsyncWith (,) src src1)
361 let src1 = fromJust r
362 transform (S.zipAsyncWithM (curry return) src src1)
365-------------------------------------------------------------------------------
367-------------------------------------------------------------------------------
369{-# INLINE scanMap #-}
370{-# INLINE dropMap #-}
371{-# INLINE dropScan #-}
372{-# INLINE takeDrop #-}
373{-# INLINE takeScan #-}
374{-# INLINE takeMap #-}
375{-# INLINE filterDrop #-}
376{-# INLINE filterTake #-}
377{-# INLINE filterScan #-}
378{-# INLINE filterMap #-}
379scanMap, dropMap, dropScan, takeDrop, takeScan, takeMap, filterDrop,
380 filterTake, filterScan, filterMap
381 :: Monad m => Int -> Stream m Int -> m ()
383scanMap n = composeN n $ S.map (subtract 1) . S.scanl' (+) 0
384dropMap n = composeN n $ S.map (subtract 1) . S.drop 1
385dropScan n = composeN n $ S.scanl' (+) 0 . S.drop 1
386takeDrop n = composeN n $ S.drop 1 . S.take maxValue
387takeScan n = composeN n $ S.scanl' (+) 0 . S.take maxValue
388takeMap n = composeN n $ S.map (subtract 1) . S.take maxValue
389filterDrop n = composeN n $ S.drop 1 . S.filter (<= maxValue)
390filterTake n = composeN n $ S.take maxValue . S.filter (<= maxValue)
391filterScan n = composeN n $ S.scanl' (+) 0 . S.filter (<= maxBound)
392filterMap n = composeN n $ S.map (subtract 1) . 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