diff --git a/README.md b/README.md index 5ca8ea2..4eb2511 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/bower.json b/bower.json index 0345a4c..0dfa19f 100644 --- a/bower.json +++ b/bower.json @@ -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" } } diff --git a/src/Data/Enum.purs b/src/Data/Enum.purs index a8f530d..467ed7b 100644 --- a/src/Data/Enum.purs +++ b/src/Data/Enum.purs @@ -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 @@ -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