Skip to content

Commit

Permalink
IntegerInterval: add magnitude
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 aba61f1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Data/IntegerInterval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,16 @@ width x
(Finite lb, Finite ub) -> ub - lb
_ -> error "Data.IntegerInterval.width: unbounded interval"

-- | Magnitude of an interval: how many integers lie within the interval.
-- Magnitude of an unbounded interval is @undefined@.
magnitude :: IntegerInterval -> Integer
magnitude x

Check warning on line 315 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: ‘magnitude’
| null x = 0
| otherwise =
case (lowerBound x, upperBound x) of
(Finite lb, Finite ub) -> ub - lb + 1
_ -> error "Data.IntegerInterval.magnitude: 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

{--------------------------------------------------------------------
magnitude
--------------------------------------------------------------------}

case_magnitude_null =
IntegerInterval.magnitude 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.magnitude’

case_magnitude_positive =
IntegerInterval.magnitude (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.magnitude’

prop_magnitude_singleton =
forAll arbitrary $ \x ->
IntegerInterval.magnitude (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.magnitude’

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

0 comments on commit aba61f1

Please sign in to comment.