Created
October 22, 2014 01:02
-
-
Save L8D/6e40d55b99e570c815d5 to your computer and use it in GitHub Desktop.
Fantasy-land compliant and JIT-optimizable Maybe
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
// 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.Maybe = factory(); | |
} | |
})(this, function() { | |
function Maybe(value) { | |
if (value == null) { | |
return Nothing(); | |
} else { | |
return Just(value); | |
} | |
}; | |
Maybe.of = Maybe.Just = Just; | |
Maybe.empty = Maybe.Nothing = Nothing; | |
function Just(value) { | |
if (!(this instanceof Just)) { | |
return new Just(value); | |
} | |
this.value = value; | |
}; | |
Just.prototype = { | |
map: function map(fn) { | |
return Just(fn(this.value)); | |
}, | |
chain: function chain(fn) { | |
return fn(this.value); | |
}, | |
equals: function equals(other) { | |
return other instanceof Just && compare(this.value, other.value); | |
}, | |
concat: function concat(other) { | |
return this; | |
}, | |
empty: function empty() { | |
return Nothing(); | |
}, | |
ap: function ap(maybe) { | |
return this.map(function(fn) { | |
return maybe.map(fn); | |
}); | |
}, | |
fold: function fold(whenJust, whenNothing) { | |
return whenJust(this.value); | |
}, | |
or: function or(value) { | |
return this.value; | |
}, | |
toString: function toString() { | |
return Just.name + '(' + this.value + ')'; | |
} | |
}; | |
function Nothing() { | |
if (!(this instanceof Nothing)) { | |
return new Nothing(); | |
} | |
this.value = null; | |
} | |
Nothing.prototype = { | |
map: function map(fn) { | |
return this; | |
}, | |
chain: function chain(fn) { | |
return this; | |
}, | |
equals: function equals(other) { | |
return other instanceof Nothing; | |
}, | |
concat: function concat(other) { | |
return other; | |
}, | |
ap: function ap(other) { | |
return this; | |
}, | |
fold: function fold(whenJust, whenNothing) { | |
return whenNothing(); | |
}, | |
or: function or(value) { | |
return value; | |
}, | |
toString: function toString() { | |
return Nothing.name; | |
} | |
}; | |
function compare(left, right) { | |
return left === right || | |
(left != null && right != null) && | |
(typeof left.equals === 'function') && | |
left.equals(right); | |
} | |
return Maybe; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment