From bcf20c5903b99cb8f1fdc415b20fb4792e071a71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Na=C3=AFm=20Favier?= Date: Tue, 19 Dec 2023 13:14:19 +0100 Subject: [PATCH] IntegerInterval: add memberCount Returns the number of integers that lie within an integer interval. --- src/Data/IntegerInterval.hs | 12 ++++++++++++ test/TestIntegerInterval.hs | 14 ++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/Data/IntegerInterval.hs b/src/Data/IntegerInterval.hs index 37bbdbe..c9af8ae 100644 --- a/src/Data/IntegerInterval.hs +++ b/src/Data/IntegerInterval.hs @@ -50,6 +50,7 @@ module Data.IntegerInterval , lowerBound' , upperBound' , width + , memberCount -- * Universal comparison operators , (=!), (>!), (/=!) @@ -309,6 +310,17 @@ width x (Finite lb, Finite ub) -> ub - lb _ -> error "Data.IntegerInterval.width: unbounded interval" +-- | How many integers lie within the interval. +-- Equal to @width + 1@ for non-empty intervals. +-- @memberCount@ of an unbounded interval is @undefined@. +memberCount :: IntegerInterval -> Integer +memberCount x + | null x = 0 + | otherwise = + case (lowerBound x, upperBound x) of + (Finite lb, Finite ub) -> ub - lb + 1 + _ -> error "Data.IntegerInterval.memberCount: unbounded interval" + -- | pick up an element from the interval if the interval is not empty. pickup :: IntegerInterval -> Maybe Integer pickup x = diff --git a/test/TestIntegerInterval.hs b/test/TestIntegerInterval.hs index 9f00fdc..862e53a 100644 --- a/test/TestIntegerInterval.hs +++ b/test/TestIntegerInterval.hs @@ -275,6 +275,20 @@ prop_width_singleton = forAll arbitrary $ \x -> IntegerInterval.width (IntegerInterval.singleton x) == 0 +{-------------------------------------------------------------------- + memberCount +--------------------------------------------------------------------} + +case_memberCount_null = + IntegerInterval.memberCount IntegerInterval.empty @?= 0 + +case_memberCount_positive = + IntegerInterval.memberCount (0 <=..< 10) @?= 10 + +prop_memberCount_singleton = + forAll arbitrary $ \x -> + IntegerInterval.memberCount (IntegerInterval.singleton x) == 1 + {-------------------------------------------------------------------- map --------------------------------------------------------------------}