Skip to content
This repository has been archived by the owner on Jul 25, 2018. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
puffnfresh committed May 31, 2013
0 parents commit 23b622a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
24 changes: 24 additions & 0 deletions package.json
@@ -0,0 +1,24 @@
{
"name": "fantasy-validations",
"version": "0.0.0",
"description": "Validation data structure.",
"main": "validation.js",
"scripts": {},
"repository": {
"type": "git",
"url": "git://github.com/puffnfresh/fantasy-validations.git"
},
"keywords": [
"fantasyland",
"validation",
"applicative"
],
"author": "Brian McKenna",
"license": "MIT",
"bugs": {
"url": "https://github.com/puffnfresh/fantasy-validations/issues"
},
"dependencies": {
"daggy": "0.0.1"
}
}
62 changes: 62 additions & 0 deletions validation.js
@@ -0,0 +1,62 @@
var daggy = require('daggy'),
Validation = daggy.taggedSum({
Success: ['s'],
Failure: ['f']
});

function identity(a) {
return a;
}

// Methods
Validation.prototype.fold = function(f, g) {
return this.cata({
Failure: f,
Success: g
});
};
Validation.of = Validation.Success;
Validation.prototype.bimap = function(f, g) {
return this.fold(
function(a) {
return Validation.Failure(f(a));
},
function(b) {
return Validation.Success(g(b));
}
);
};
Validation.prototype.map = function(f) {
return this.bimap(identity, f);
};
Validation.prototype.ap = function(b) {
return this.fold(
function(f) {
return Validation.Failure(f);
},
function(s) {
return b.map(s);
}
);
};
Validation.prototype.concat = function(b) {
return this.fold(
function(f) {
return b.bimap(
function(g) {
return f.concat(g);
},
identity
);
},
function(s) {
return b.map(function(d) {
return s.concat(d);
});
}
);
};

// Export
if(typeof module != 'undefined')
module.exports = Validation;

0 comments on commit 23b622a

Please sign in to comment.