on master help sign in
use foldl' for length, sum, product
commit5ace3582a89d2654ce849830506ce8b1dab1d963 authorHarendra Kumar <harendra.kumar@gmail.com> dateMon 25 Jun 2018, 03:51 (8 years ago) parentde5c0ca
3 files changed+37−16
benchmark/Linear.hs+5 benchmark/LinearOps.hs+24−13 src/Streamly/Prelude.hs+8−3
benchmark/Linear.hs+5
@@ -61,6 +61,11 @@ main = do
6161 , benchIO "foldrM" Ops.foldrM
6262 , benchIO "foldl'" Ops.foldl
6363 , benchIO "last" Ops.last
64+ , benchIO "length" Ops.length
65+ , benchIO "sum" Ops.sum
66+ , benchIO "product" Ops.product
67+ , benchIO "maximum" Ops.maximum
68+ , benchIO "minimum" Ops.minimum
6469 ]
6570 , bgroup "transformation"
6671 [ benchIO "scan" Ops.scan
benchmark/LinearOps.hs+24−13
@@ -25,8 +25,10 @@ maxValue = value + 1000
2525-------------------------------------------------------------------------------
2626
2727{-# INLINE scan #-}
28+{-# INLINE mapM_ #-}
2829{-# INLINE map #-}
2930{-# INLINE fmap #-}
31+{-# INLINE mapMaybe #-}
3032{-# INLINE filterEven #-}
3133{-# INLINE filterAllOut #-}
3234{-# INLINE filterAllIn #-}
@@ -40,7 +42,7 @@ maxValue = value + 1000
4042{-# INLINE composeAllInFilters #-}
4143{-# INLINE composeAllOutFilters #-}
4244{-# INLINE composeMapAllInFilter #-}
43−scan, map, fmap, filterEven, filterAllOut,
45+scan, mapM_, map, fmap, mapMaybe, filterEven, filterAllOut,
4446 filterAllIn, takeOne, takeAll, takeWhileTrue, dropAll, dropWhileTrue, zip,
4547 concat, composeAllInFilters, composeAllOutFilters,
4648 composeMapAllInFilter
@@ -48,32 +50,36 @@ scan, map, fmap, filterEven, filterAllOut,
4850 => Stream m Int -> m ()
4951
5052{-# INLINE composeMapM #-}
51−composeMapM :: S.MonadAsync m => Stream m Int -> m ()
53+{-# INLINE zipAsync #-}
54+{-# INLINE mapMaybeM #-}
55+composeMapM, zipAsync, mapMaybeM :: S.MonadAsync m => Stream m Int -> m ()
5256
5357{-# INLINE toList #-}
5458{-# INLINE foldr #-}
5559{-# INLINE foldrM #-}
5660toList, foldr, foldrM :: Monad m => Stream m Int -> m [Int]
57−{-# INLINE foldl #-}
58−foldl :: Monad m => Stream m Int -> m Int
61+
5962{-# INLINE last #-}
60−last :: Monad m => Stream m Int -> m (Maybe Int)
63+{-# INLINE maximum #-}
64+{-# INLINE minimum #-}
65+last, minimum, maximum :: Monad m => Stream m Int -> m (Maybe Int)
66+
67+{-# INLINE foldl #-}
68+{-# INLINE length #-}
69+{-# INLINE sum #-}
70+{-# INLINE product #-}
71+foldl, length, sum, product :: Monad m => Stream m Int -> m Int
72+
6173{-# INLINE toNull #-}
6274toNull :: Monad m => (t m Int -> S.SerialT m Int) -> t m Int -> m ()
63−{-# INLINE mapM_ #-}
64−mapM_ :: Monad m => Stream m Int -> m ()
75+
6576{-# INLINE mapM #-}
6677mapM :: (S.IsStream t, S.MonadAsync m)
6778 => (t m Int -> S.SerialT m Int) -> t m Int -> m ()
68−{-# INLINE mapMaybe #-}
69−mapMaybe :: Monad m => Stream m Int -> m ()
70−{-# INLINE mapMaybeM #-}
71−mapMaybeM :: S.MonadAsync m => Stream m Int -> m ()
79+
7280{-# INLINE sequence #-}
7381sequence :: (S.IsStream t, S.MonadAsync m)
7482 => (t m Int -> S.SerialT m Int) -> t m (m Int) -> m ()
75−{-# INLINE zipAsync #-}
76−zipAsync :: S.MonadAsync m => Stream m Int -> m ()
7783
7884-------------------------------------------------------------------------------
7985-- Stream generation and elimination
@@ -154,6 +160,11 @@ foldr = S.foldr (:) []
154160foldrM = S.foldrM (\a xs -> return (a : xs)) []
155161foldl = S.foldl' (+) 0
156162last = S.last
163+length = S.length
164+sum = S.sum
165+product = S.product
166+maximum = S.maximum
167+minimum = S.minimum
157168
158169-------------------------------------------------------------------------------
159170-- Transformation
src/Streamly/Prelude.hs+8−3
@@ -558,14 +558,16 @@ any p m = go (toStream m)
558558-- | Determine the sum of all elements of a stream of numbers
559559--
560560-- @since 0.1.0
561+{-# INLINE sum #-}
561562sum :: (Monad m, Num a) => SerialT m a -> m a
562−sum = foldl (+) 0 id
563+sum = foldl' (+) 0
563564
564565-- | Determine the product of all elements of a stream of numbers
565566--
566567-- @since 0.1.1
568+{-# INLINE product #-}
567569product :: (Monad m, Num a) => SerialT m a -> m a
568−product = foldl (*) 1 id
570+product = foldl' (*) 1
569571
570572-- | Extract the first element of the stream, if any.
571573--
@@ -631,8 +633,9 @@ notElem e m = go (toStream m)
631633-- | Determine the length of the stream.
632634--
633635-- @since 0.1.0
636+{-# INLINE length #-}
634637length :: Monad m => SerialT m a -> m Int
635−length = foldl (\n _ -> n + 1) 0 id
638+length = foldl' (\n _ -> n + 1) 0
636639
637640-- | Returns the elements of the stream in reverse order.
638641-- The stream must be finite.
@@ -652,6 +655,7 @@ reverse m = fromStream $ go K.nil (toStream m)
652655-- | Determine the minimum element in a stream.
653656--
654657-- @since 0.1.0
658+{-# INLINE minimum #-}
655659minimum :: (Monad m, Ord a) => SerialT m a -> m (Maybe a)
656660minimum m = go Nothing (toStream m)
657661 where
@@ -669,6 +673,7 @@ minimum m = go Nothing (toStream m)
669673-- | Determine the maximum element in a stream.
670674--
671675-- @since 0.1.0
676+{-# INLINE maximum #-}
672677maximum :: (Monad m, Ord a) => SerialT m a -> m (Maybe a)
673678maximum m = go Nothing (toStream m)
674679 where