Skip to content

Commit

Permalink
Merge pull request #27 from purescript/parity
Browse files Browse the repository at this point in the history
Add `Parity`
  • Loading branch information
garyb committed Jun 30, 2017
2 parents a0332bb + 117f84e commit 4ef3a1e
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/Data/Int.purs
Expand Up @@ -14,6 +14,8 @@ module Data.Int
, base36
, fromStringAs
, toStringAs
, Parity(..)
, parity
, even
, odd
, pow
Expand Down Expand Up @@ -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
Expand All @@ -90,7 +115,7 @@ even x = x .&. 1 == 0
-- |
-- | ``` purescript
-- | odd 0 == false
-- | odd 1 == false
-- | odd 1 == true
-- | ```
odd :: Int -> Boolean
odd x = x .&. 1 /= 0
Expand Down

0 comments on commit 4ef3a1e

Please sign in to comment.