Functional Validation
Fantasy Land Compliant!
Indigenous Complaint!
This is an implementation of the Validation monad from Haskell and from the Scalaz library. It is also naturally a functor, applictive and monoid.
Just like in scalaz, theres lots of fuss about if this is a true monad and monoid. The interfaces remain true but not all laws like the associativity law for monoid append hold true.
Examples
Monoid error concatenation:
var Success = ;var Failure = ; var { return n; }; var success1 = 1;var success2 = 2;var failure1 = "error1";var failure2 = "error2"; var combo1 = success1;console;// prints "Value: 1" var combo2 = success1;console;// prints "Value: ['error1']" var combo3 = failure1;console;//prints "Value: ['error1', 'error2']"
Functor composition:
var Success = ;var Failure = ; var { return n; }; var success = 1;var failure = "error"; var map1 = success;console;//prints "Value: 2" var map2 = failure;console;//prints "Value: ['error']"
Applicative composition:
var Validation = ;var Success = ;var Failure = ;var curry = curry; var { return n; }; var successString = "beep";var successNumber = 1;var successBoolean = true; var failure1 = "error1";var failure2 = "error2"; var lifted = Validation; var applied = lifted;console;//prints "Value: beep - 1 - true" var errored = lifted;consosle;//prints "Value: ['error1', 'error2']"
Monadic composition (flatMap is an alias for chain):
var Success = ;var Failure = ; var success = 1;var failure = "error"; var applied = success;console;//prints "Value: 2" var ignored = failure;console;//prints "Value: ['error']"