Skip to content

Instantly share code, notes, and snippets.

@andredublin
Last active May 17, 2016 19:55
Show Gist options
  • Save andredublin/78de258d07d97161c304a27150807960 to your computer and use it in GitHub Desktop.
Save andredublin/78de258d07d97161c304a27150807960 to your computer and use it in GitHub Desktop.
either js
// 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