2-- Module : BenchmarkOps
3-- Copyright : (c) 2018 Harendra Kumar
6-- Maintainer : harendra.kumar@gmail.com
8{-# LANGUAGE FlexibleContexts #-}
13 (Monad, Int, (+), ($), (.), return, fmap, even, (>), (<=),
14 subtract, undefined, Maybe(..), Monoid, foldMap)
16import qualified Streamly as S
17import qualified Streamly.Prelude as S
19value, appendValue, maxValue :: Int
22maxValue = value + 1000
24-------------------------------------------------------------------------------
26-------------------------------------------------------------------------------
34{-# INLINE filterEven #-}
36{-# INLINE filterAllOut #-}
37{-# INLINE filterAllIn #-}
40{-# INLINE takeWhileTrue #-}
42{-# INLINE dropWhileTrue #-}
45{-# INLINE composeMapM #-}
46{-# INLINE composeAllInFilters #-}
47{-# INLINE composeAllOutFilters #-}
48{-# INLINE composeMapAllInFilter #-}
49scan, map, filterEven, filterAllOut,
50 filterAllIn, takeOne, takeAll, takeWhileTrue, dropAll, dropWhileTrue, zip,
51 concat, composeAllInFilters, composeAllOutFilters,
54 => Stream m Int -> m ()
56mapM, composeMapM :: S.MonadAsync m => Stream m Int -> m ()
57toList :: Monad m => Stream m Int -> m [Int]
58foldl :: Monad m => Stream m Int -> m Int
59last :: Monad m => Stream m Int -> m (Maybe Int)
61toNull :: Monad m => (t m Int -> S.SerialT m Int) -> t m Int -> m ()
63-------------------------------------------------------------------------------
64-- Stream generation and elimination
65-------------------------------------------------------------------------------
67type Stream m a = S.SerialT m a
70source :: S.IsStream t => Int -> t m Int
71source n = S.unfoldr step n
76 else (Just (cnt, cnt + 1))
78{-# INLINE sourceFromFoldable #-}
79sourceFromFoldable :: S.IsStream t => Int -> t m Int
80sourceFromFoldable n = S.fromFoldable [n..n+value]
82{-# INLINE sourceFromFoldableM #-}
83sourceFromFoldableM :: (S.IsStream t, S.MonadAsync m) => Int -> t m Int
84sourceFromFoldableM n = S.fromFoldableM (Prelude.fmap return [n..n+value])
86{-# INLINE sourceFoldMapWith #-}
87sourceFoldMapWith :: (S.IsStream t, Monad (t m), S.Semigroup (t m Int))
89sourceFoldMapWith n = S.foldMapWith (S.<>) return [n..n+value]
91{-# INLINE sourceFoldMapWithM #-}
92sourceFoldMapWithM :: (S.IsStream t, Monad m, S.Semigroup (t m Int))
94sourceFoldMapWithM n = S.foldMapWith (S.<>) (S.once . return) [n..n+value]
96{-# INLINE sourceUnfoldrM #-}
97sourceUnfoldrM :: (S.IsStream t, S.MonadAsync m) => Int -> t m Int
98sourceUnfoldrM n = S.unfoldrM step n
103 else return (Just (cnt, cnt + 1))
105{-# INLINE runStream #-}
106runStream :: Monad m => Stream m a -> m ()
107runStream = S.runStream
109-------------------------------------------------------------------------------
111-------------------------------------------------------------------------------
113toNull t = runStream . t
115foldl = S.foldl' (+) 0
118-------------------------------------------------------------------------------
120-------------------------------------------------------------------------------
122{-# INLINE transform #-}
123transform :: Monad m => Stream m a -> m ()
126scan = transform . S.scanl' (+) 0
127map = transform . fmap (+1)
128mapM = transform . S.mapM return
129filterEven = transform . S.filter even
130filterAllOut = transform . S.filter (> maxValue)
131filterAllIn = transform . S.filter (<= maxValue)
132takeOne = transform . S.take 1
133takeAll = transform . S.take maxValue
134takeWhileTrue = transform . S.takeWhile (<= maxValue)
135dropAll = transform . S.drop maxValue
136dropWhileTrue = transform . S.dropWhile (<= maxValue)
138-------------------------------------------------------------------------------
140-------------------------------------------------------------------------------
142zip src = transform $ (S.zipWith (,) src src)
145-------------------------------------------------------------------------------
147-------------------------------------------------------------------------------
151 :: (Monoid (t m Int), Monad m, Monad (t m))
152 => (t m Int -> S.SerialT m Int) -> Int -> m ()
153append t n = runStream $ t $ foldMap return [n..n+appendValue]
155-------------------------------------------------------------------------------
157-------------------------------------------------------------------------------
159{-# INLINE compose #-}
160compose :: Monad m => (Stream m Int -> Stream m Int) -> Stream m Int -> m ()
161compose f = transform . f . f . f . f
163composeMapM = compose (S.mapM return)
164composeAllInFilters = compose (S.filter (<= maxValue))
165composeAllOutFilters = compose (S.filter (> maxValue))
166composeMapAllInFilter = compose (S.filter (<= maxValue) . fmap (subtract 1))
168{-# INLINABLE composeScaling #-}
169composeScaling :: Monad m => Int -> Stream m Int -> m ()
173 2 -> transform . f . f
174 3 -> transform . f . f . f
175 4 -> transform . f . f . f . f
177 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