Skip to content

Instantly share code, notes, and snippets.

@Gwash3189
Created September 30, 2015 00:33
Show Gist options
  • Save Gwash3189/4d1ee663e21c7be20070 to your computer and use it in GitHub Desktop.
Save Gwash3189/4d1ee663e21c7be20070 to your computer and use it in GitHub Desktop.
either - left - right
const Left = function(x) {
this.__value = x;
};
Left.of = function(x) {
return new Left(x);
};
Left.prototype.map = function(f) {
return this;
};
const 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));
};
const Either = function(x) {
if (x instanceof Right) {
this.right = x
} else {
this.left = x
}
};
Either.prototype.map = function(f) {
return this.right
? this.right.map(f)
: this.left.map(f);
};
Either.of = function(x) {
return new Either(x);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment