Skip to content

Instantly share code, notes, and snippets.

@L8D
Created October 22, 2014 01:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save L8D/abe1a119b5074272cc02 to your computer and use it in GitHub Desktop.
Save L8D/abe1a119b5074272cc02 to your computer and use it in GitHub Desktop.
Fantasy-land compliant and JIT-optimizable Either
// Copyright (c) 2014 Tenor Biel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
;(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define(factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.Either = factory();
}
})(this, function() {
function Either(left, right) {
if (right == null) {
return Left(left);
} else {
return Right(right);
}
}
Either.of = Either.Right = Right;
Either.Left = Left;
function Right(value) {
if (!(this instanceof Right)) {
return new Right(value);
}
this.left = null;
this.right = value;
}
Right.prototype = {
map: function map(fn) {
return Right(fn(this.right));
},
chain: function chain(fn) {
return fn(this.right);
},
equals: function equals(other) {
return other instanceof Right && compare(this.right, other.right);
},
swap: function() {
return Left(this.right);
},
ap: function(other) {
return this.map(function(fn) {
return other.map(fn);
});
},
fold: function(left, right) {
return right(this.right);
},
toString: function() {
return Right.name + '(' + this.value + ')';
}
};
function Left(value) {
if (!(this instanceof Left)) {
return new Left(value);
}
this.left = value;
this.right = null;
}
Left.prototype = {
map: function map(fn) {
return this;
},
chain: function chain(fn) {
return this;
},
equals: function equals(other) {
return other instanceof Left && compare(this.left, other.left);
},
swap: function() {
return Right(this.left);
},
ap: function(other) {
return this;
},
fold: function(left, right) {
return left(this.left);
},
toString: function() {
return Left.name + '(' + this.value + ')';
}
};
function compare(left, right) {
return left === right ||
(left != null && right != null) &&
(typeof left.equals === 'function') &&
left.equals(right);
}
return Either;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment