Created
November 23, 2016 02:20
-
-
Save o0/41c54e604f55acddc638c78b033fb268 to your computer and use it in GitHub Desktop.
Validation monad with errors accumulation. Naïve version. Requires monet.js library
This file contains hidden or 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
'use strict'; | |
const User = { | |
age: 14, | |
female: false, | |
isPretty: false | |
}; | |
const isAdult = user => user.age > 18 ? | |
Success(user) : | |
Fail([`User ain't adult`]); | |
const isFemale = user => user.female ? | |
Success(user) : | |
Fail([`User ain't female`]); | |
const isPretty = user => user.isPretty ? | |
Success(user) : | |
Fail([`User ain't pretty`]); | |
const validity = isAdult(User).ap( | |
isFemale(User).ap( | |
isPretty(User).acc() | |
) | |
); | |
console.log(validity); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment