-
Notifications
You must be signed in to change notification settings - Fork 9
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
Conversation
Shall we make it total, returning |
In that case we might as well return |
Imagine yourself on the consuming end, where you have to pattern match on the output of |
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 |
There was a problem hiding this comment.
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
withpickup
.
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.
There was a problem hiding this comment.
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
.
Thanks! |
Thanks! A release including this change (and my last one from last year) would be appreciated. |
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 whatNumeric.Interval.magnitude
does. Feel free to suggest a better name.