Created
September 30, 2015 00:33
-
-
Save Gwash3189/4d1ee663e21c7be20070 to your computer and use it in GitHub Desktop.
either - left - right
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
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