-
Notifications
You must be signed in to change notification settings - Fork 18
Add Parity
#27
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
Merged
Add Parity
#27
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,8 @@ module Data.Int | |
| , base36 | ||
| , fromStringAs | ||
| , toStringAs | ||
| , Parity(..) | ||
| , parity | ||
| , even | ||
| , odd | ||
| , pow | ||
|
|
@@ -77,6 +79,29 @@ foreign import toNumber :: Int -> Number | |
| fromString :: String -> Maybe Int | ||
| fromString = fromStringAs (Radix 10) | ||
|
|
||
| -- | A type for describing whether an integer is even or odd. | ||
| data Parity = Even | Odd | ||
|
|
||
| derive instance eqParity :: Eq Parity | ||
| derive instance ordParity :: Ord Parity | ||
|
|
||
| instance showParity :: Show Parity where | ||
| show Even = "Even" | ||
| show Odd = "Odd" | ||
|
|
||
| instance boundedParity :: Bounded Parity where | ||
| bottom = Even | ||
| top = Odd | ||
|
|
||
| -- | Returns whether an `Int` is `Even` or `Odd`. | ||
| -- | | ||
| -- | ``` purescript | ||
| -- | parity 0 == Even | ||
| -- | parity 1 == Odd | ||
| -- | ``` | ||
| parity :: Int -> Parity | ||
| parity n = if even n then Even else Odd | ||
|
|
||
| -- | Returns whether an `Int` is an even number. | ||
| -- | | ||
| -- | ``` purescript | ||
|
|
@@ -90,7 +115,7 @@ even x = x .&. 1 == 0 | |
| -- | | ||
| -- | ``` purescript | ||
| -- | odd 0 == false | ||
| -- | odd 1 == false | ||
| -- | odd 1 == true | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😱
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Haha yeah! 😆 |
||
| -- | ``` | ||
| odd :: Int -> Boolean | ||
| odd x = x .&. 1 /= 0 | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe
Showtoo?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.
Good thinking. I added
Boundedalso.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.
Also
CommutativeRing.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.
It's a field, isn't it, if we're going to give it numeric instances? If we treat Parity as Z/2Z with Even = [0] and Odd = [1]?
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.
I'm thinking of this as being an approximation to integers, not Z/2Z. Dividing an even integer by another even integer could be even or odd.
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.
Although I think sometimes "ring" is defined such that multiplication need only be a semigroup, not necessarily a monoid; in that case you might be able to find two such rings with two elements.
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.
Yeah your logic seems sound to me. I was going from this:
https://en.wikipedia.org/wiki/Finite_ring
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.
Does that suggest that we should give
BooleanaFieldinstance?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.
I don't think we should. Just because it has two elements doesn't mean we should think of it as Z/2 in my opinion. I would rather have a separate type.
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.
Makes sense, although we can make it into a field without thinking of it as Z/2Z - just by thinking of addition as
xorand multiplication as&&. But yeah in the absence of any concrete use case I'm inclined to leave it how it is too.