Skip to content

Commit

Permalink
IntegerInterval: add memberCount
Browse files Browse the repository at this point in the history
Returns the number of integers that lie within an integer interval.
  • Loading branch information
ncfavier committed Dec 19, 2023
1 parent 21d7a82 commit b14e6b0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Data/IntegerInterval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,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

Check warning on line 316 in src/Data/IntegerInterval.hs

View workflow job for this annotation

GitHub Actions / data-interval (8.10.2, ubuntu-latest, stack-8.10.2.yaml)

Defined but not used: ‘memberCount’
| 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 =
Expand Down
14 changes: 14 additions & 0 deletions test/TestIntegerInterval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check failure on line 283 in test/TestIntegerInterval.hs

View workflow job for this annotation

GitHub Actions / data-interval (8.10.2, ubuntu-latest, stack-8.10.2.yaml)

Not in scope: ‘IntegerInterval.memberCount’

case_memberCount_positive =
IntegerInterval.memberCount (0 <=..< 10) @?= 10

Check failure on line 286 in test/TestIntegerInterval.hs

View workflow job for this annotation

GitHub Actions / data-interval (8.10.2, ubuntu-latest, stack-8.10.2.yaml)

Not in scope: ‘IntegerInterval.memberCount’

prop_memberCount_singleton =
forAll arbitrary $ \x ->
IntegerInterval.memberCount (IntegerInterval.singleton x) == 1

Check failure on line 290 in test/TestIntegerInterval.hs

View workflow job for this annotation

GitHub Actions / data-interval (8.10.2, ubuntu-latest, stack-8.10.2.yaml)

Not in scope: ‘IntegerInterval.memberCount’

{--------------------------------------------------------------------
map
--------------------------------------------------------------------}
Expand Down

0 comments on commit b14e6b0

Please sign in to comment.