Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IntegerInterval: add memberCount #44

Merged
merged 1 commit into from
Dec 21, 2023
Merged

Conversation

ncfavier
Copy link
Contributor

@ncfavier ncfavier commented Dec 19, 2023

Returns the number of integers that lie within an integer interval.

This is different from the width and cannot simply be computed as (+ 1) . width because of the null interval.

Addresses #39

EDIT: I initially named this magnitude because I misunderstood what Numeric.Interval.magnitude does. Feel free to suggest a better name.

@ncfavier ncfavier changed the title IntegerInterval: add magnitude IntegerInterval: add memberCount Dec 19, 2023
@Bodigrim
Copy link
Collaborator

Shall we make it total, returning Maybe Integer?

@ncfavier
Copy link
Contributor Author

In that case we might as well return Extended Integer, so that the memberCount of an unbounded interval is PosInf?

@Bodigrim
Copy link
Collaborator

Imagine yourself on the consuming end, where you have to pattern match on the output of memberCount. What would you do for NegInf? Throw error "this should never happen"? I'd prefer to receive Maybe.

Returns the number of integers that lie within an integer interval.
-- | How many integers lie within the (bounded) interval.
-- Equal to @Just (width + 1)@ for non-empty, bounded intervals.
-- The @memberCount@ of an unbounded interval is @Nothing@.
memberCount :: IntegerInterval -> Maybe Integer
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As usual, the shorter PR the longer bikeshedding :)

I wonder if Maybe Natural would be a better return type. Two benefits:

  • It's the smallest possible set of values, making memberCount surjective.
  • It's more distinguishing, e. g., one cannot replace memberCount with pickup.
    An obvious penalty:
  • Users are likely to need more fromIntegral / toInteger.

I'm split. @ncfavier what do you think? Let's do what you prefer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this were Agda I would definitely insist on returning Maybe ℕ, but in a low-level language like Haskell I don't think it's worth the trouble. You don't pattern match on Integer.

@Bodigrim Bodigrim merged commit 5155346 into msakai:master Dec 21, 2023
11 checks passed
@Bodigrim
Copy link
Collaborator

Thanks!

@ncfavier ncfavier deleted the magnitude branch December 21, 2023 00:03
@ncfavier
Copy link
Contributor Author

Thanks! A release including this change (and my last one from last year) would be appreciated.

@Bodigrim
Copy link
Collaborator

Done: https://hackage.haskell.org/package/data-interval-2.1.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants