Skip to content

Commit

Permalink
Merge pull request #3 from jdegoes/ready/char-instance
Browse files Browse the repository at this point in the history
add Enum instance for Char, fix all versions
  • Loading branch information
paf31 committed Oct 16, 2014
2 parents da48810 + 15e4f2a commit d28e6fc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

instance enumBoolean :: Enum Boolean

instance enumChar :: Enum Char

instance enumMaybe :: (Enum a) => Enum (Maybe a)

instance enumTuple :: (Enum a, Enum b) => Enum (Tuple a b)
Expand Down
5 changes: 3 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"package.json"
],
"dependencies": {
"purescript-maybe": "*",
"purescript-tuples": "*"
"purescript-maybe": "~0.2.1",
"purescript-tuples": "~0.2.1",
"purescript-strings": "~0.3.2"
}
}
12 changes: 12 additions & 0 deletions src/Data/Enum.purs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Data.Enum

import Data.Maybe
import Data.Tuple
import Data.Char
import Data.Maybe.Unsafe

newtype Cardinality a = Cardinality Number
Expand Down Expand Up @@ -55,6 +56,17 @@ module Data.Enum
maybeCardinality :: forall a. (Enum a) => Cardinality a -> Cardinality (Maybe a)
maybeCardinality c = Cardinality $ 1 + (runCardinality c)

instance enumChar :: Enum Char where
cardinality = Cardinality (65535 + 1)

firstEnum = fromCharCode 0

lastEnum = fromCharCode 65535

succ c = if c == lastEnum then Nothing else Just $ (fromCharCode <<< ((+) 1) <<< toCharCode) c

pred c = if c == firstEnum then Nothing else Just $ (fromCharCode <<< ((+) (-1)) <<< toCharCode) c

instance enumMaybe :: (Enum a) => Enum (Maybe a) where
cardinality = maybeCardinality cardinality

Expand Down

0 comments on commit d28e6fc

Please sign in to comment.