Skip to content

Instantly share code, notes, and snippets.

@dinox0r
Created August 29, 2017 08:12
Show Gist options
  • Save dinox0r/5324908bef309d5c9eb0ff48e229ff6d to your computer and use it in GitHub Desktop.
Save dinox0r/5324908bef309d5c9eb0ff48e229ff6d to your computer and use it in GitHub Desktop.
either.js
'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