Skip to content

Commit

Permalink
added Enum instance for Either
Browse files Browse the repository at this point in the history
  • Loading branch information
jdegoes committed Oct 18, 2014
1 parent 15e4f2a commit adbdf45
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
16 changes: 14 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,25 @@ module.exports = function(grunt) {
src: "src/**/*.purs",
dest: "README.md"
}
},
jsvalidate: {
options:{
globals: {},
esprimaOptions: {},
verbose: false
},
targetName:{
files:{
src: ['output/Data.Enum/*.js']
}
}
}

});

grunt.loadNpmTasks('grunt-jsvalidate');
grunt.loadNpmTasks("grunt-contrib-clean");
grunt.loadNpmTasks("grunt-purescript");

grunt.registerTask("make", ["pscMake", "dotPsci", "docgen"]);
grunt.registerTask("make", ["pscMake", "dotPsci", "docgen", "jsvalidate"]);
grunt.registerTask("default", ["make"]);
};
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

instance enumChar :: Enum Char

instance enumEither :: (Enum a, Enum b) => Enum (Either a b)

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

instance enumTuple :: (Enum a, Enum b) => Enum (Tuple a b)
Expand Down
1 change: 1 addition & 0 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"dependencies": {
"purescript-maybe": "~0.2.1",
"purescript-tuples": "~0.2.1",
"purescript-either": "~0.1.3",
"purescript-strings": "~0.3.2"
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"dependencies": {
"grunt": "~0.4.4",
"grunt-purescript": "~0.5.1",
"grunt-contrib-clean": "~0.5.0"
"grunt-contrib-clean": "~0.5.0",
"grunt-jsvalidate": "~0.2.2"
}
}
19 changes: 18 additions & 1 deletion src/Data/Enum.purs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module Data.Enum
) where

import Data.Maybe
import Data.Either
import Data.Tuple
import Data.Char
import Data.Maybe.Unsafe
Expand Down Expand Up @@ -105,4 +106,20 @@ module Data.Enum
pred (Tuple a b) = maybe (flip Tuple firstEnum <$> pred a) (Just <<< Tuple a) (pred b)

tupleCardinality :: forall a b. (Enum a, Enum b) => Cardinality a -> Cardinality b -> Cardinality (Tuple a b)
tupleCardinality l r = Cardinality $ (runCardinality l) * (runCardinality r)
tupleCardinality l r = Cardinality $ (runCardinality l) * (runCardinality r)

instance enumEither :: (Enum a, Enum b) => Enum (Either a b) where
cardinality = eitherCardinality cardinality cardinality

firstEnum = Left firstEnum

lastEnum = Right lastEnum

succ (Left a) = maybe (Just $ Right firstEnum) (Just <<< Left) (succ a)
succ (Right b) = maybe (Nothing) (Just <<< Right) (succ b)

pred (Left a) = maybe (Nothing) (Just <<< Left) (pred a)
pred (Right b) = maybe (Just $ Left lastEnum) (Just <<< Right) (pred b)

eitherCardinality :: forall a b. (Enum a, Enum b) => Cardinality a -> Cardinality b -> Cardinality (Either a b)
eitherCardinality l r = Cardinality $ (runCardinality l) + (runCardinality r)

0 comments on commit adbdf45

Please sign in to comment.