Created
August 29, 2017 08:12
-
-
Save dinox0r/5324908bef309d5c9eb0ff48e229ff6d to your computer and use it in GitHub Desktop.
either.js
This file contains hidden or 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
| 'use strict'; | |
| let Left = x => this.__value = x; | |
| Left.of = x => new Left(x); | |
| Left.prototype.isRight = () => false; | |
| Left.prototype.isLeft = () => true; | |
| Left.prototype.map = f => this; | |
| let Right = x => this.__value = x; | |
| Right.of = x => new Right(x); | |
| Left.prototype.isRight = () => true; | |
| Left.prototype.isLeft = () => false; | |
| Right.prototype.map = f => Right.of(f(this.__value)); | |
| Right.prototype.flatMap = f => { | |
| if (this.empty()) { | |
| return Option.empty(); | |
| } else { | |
| let fx = f(this.__value); | |
| return fx instanceof Right ? fx : Option.empty(); | |
| } | |
| }; | |
| Either.of = x => Right.of(x); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment