This repository has been archived by the owner on Jul 25, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 23b622a
Showing
3 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |