Last active
May 17, 2016 19:55
-
-
Save andredublin/78de258d07d97161c304a27150807960 to your computer and use it in GitHub Desktop.
either js
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
// Left represents error or bad case | |
var Left = function(x) { | |
this.__value = x; | |
}; | |
Left.of = function(x) { | |
return new Left(x); | |
}; | |
Left.prototype.map = function(f) { | |
return this; | |
}; | |
// Right represents success or good case | |
var Right = function(x) { | |
this.__value = x; | |
}; | |
Right.of = function(x) { | |
return new Right(x); | |
}; | |
Right.prototype.map = function(f) { | |
return Right.of(f(this.__value)); | |
} | |
// getAge :: Date -> User -> Either(String, Number) | |
var getAge = curry(function(now, user) { | |
var birthdate = computeDate(user.birthdate, 'YYYY-MM-DD'); | |
if (!birthdate.isValid()) return Left.of('Birth date could not be parsed'); | |
return Right.of(now.diff(birthdate, 'years')); | |
}); | |
getAge(moment(), { | |
birthdate: '2005-12-12', | |
}); | |
// Right(9) | |
getAge(moment(), { | |
birthdate: '20010704', | |
}); | |
// Left('Birth date could not be parsed') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment