From 766e7c934469b1e38ec809e93286911d13fc6ee9 Mon Sep 17 00:00:00 2001 From: Max Bolingbroke Date: Mon, 9 Feb 2009 21:29:40 +0000 Subject: [PATCH] Implement minimum, maximum, sum and product --- Data/Vector/IVector.hs | 18 ++++++++++++++++++ tests/Properties.hs | 12 ++++++------ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/Data/Vector/IVector.hs b/Data/Vector/IVector.hs index 7fa877c..2947a65 100644 --- a/Data/Vector/IVector.hs +++ b/Data/Vector/IVector.hs @@ -50,6 +50,7 @@ module Data.Vector.IVector ( -- * Specialised folds and, or, concatMap, + sum, product, maximum, minimum, -- * Enumeration enumFromTo, enumFromThenTo, @@ -98,6 +99,7 @@ import Prelude hiding ( length, null, elem, notElem, foldl, foldl1, foldr, foldr1, and, or, concatMap, + sum, product, maximum, minimum, enumFromTo, enumFromThenTo ) -- | Class of immutable vectors. @@ -513,6 +515,22 @@ concatMap :: (IVector v a, IVector v b) => (a -> v b) -> v a -> v b {-# INLINE concatMap #-} concatMap f = unstream . Stream.concatMap (stream . f) . stream +sum :: (IVector v a, Num a) => v a -> a +{-# INLINE sum #-} +sum = Stream.foldl' (+) 0 . stream + +product :: (IVector v a, Num a) => v a -> a +{-# INLINE product #-} +product = Stream.foldl' (*) 1 . stream + +maximum :: (IVector v a, Ord a) => v a -> a +{-# INLINE maximum #-} +maximum = Stream.foldl1' max . stream + +minimum :: (IVector v a, Ord a) => v a -> a +{-# INLINE minimum #-} +minimum = Stream.foldl1' min . stream + -- Enumeration -- ----------- diff --git a/tests/Properties.hs b/tests/Properties.hs index 9d94111..ecc8eda 100644 --- a/tests/Properties.hs +++ b/tests/Properties.hs @@ -155,11 +155,11 @@ testPolymorphicFunctions _ = \$(testProperties [ -- vlength, vnew testOrdFunctions :: forall a v. (COMMON_CONTEXT(a, v), Ord a, Ord (v a)) => v a -> [Test] -testOrdFunctions _ = \$(testProperties ['prop_compare]) +testOrdFunctions _ = \$(testProperties ['prop_compare, 'prop_maximum, 'prop_minimum]) where prop_compare = (compare :: v a -> v a -> Ordering) `eq2` compare - --prop_maximum = (V.maximum :: v a -> a) `eqNotNull1` maximum - --prop_minimum = (V.minimum :: v a -> a) `eqNotNull1` minimum + prop_maximum = (V.maximum :: v a -> a) `eqNotNull1` maximum + prop_minimum = (V.minimum :: v a -> a) `eqNotNull1` minimum testEnumFunctions :: forall a v. (COMMON_CONTEXT(a, v), Enum a) => v a -> [Test] testEnumFunctions _ = \$(testProperties ['prop_enumFromTo, 'prop_enumFromThenTo]) @@ -174,10 +174,10 @@ testBoolFunctions _ = \$(testProperties ['prop_and, 'prop_or]) prop_or = (V.or :: v Bool -> Bool) `eq1` or testNumFunctions :: forall a v. (COMMON_CONTEXT(a, v), Num a) => v a -> [Test] -testNumFunctions _ = \$(testProperties []) +testNumFunctions _ = \$(testProperties ['prop_sum, 'prop_product]) where - --prop_sum = (V.sum :: v Int -> Int) `eq1` sum - --prop_product = (V.product :: v Int -> Int) `eq1` product + prop_sum = (V.sum :: v a -> a) `eq1` sum + prop_product = (V.product :: v a -> a) `eq1` product testNestedVectorFunctions :: forall a v. (COMMON_CONTEXT(a, v)) => v a -> [Test] testNestedVectorFunctions _ = \$(testProperties []) -- 1.9.1