Skip to content

Instantly share code, notes, and snippets.

@PatrickJS
Last active January 27, 2016 03:01
Show Gist options
  • Save PatrickJS/5844383 to your computer and use it in GitHub Desktop.
Save PatrickJS/5844383 to your computer and use it in GitHub Desktop.
Maybe Monad
function MONAD(modifier) {
var prototype = Object.create(null);
function unit(value) {
var monad = Object.create(prototype);
monad.bind = function(func, args) {
return func.apply(undefined, [value].concat(Array.prototype.slice.apply(args || [])));
};
if (typeof modifier === 'function') {
modifier(monad, value);
}
return monad;
}
return unit;
}
=======
var maybe = MONAF(function (monad, value) {
if (value === null || value === undefined) {
monad.is_null = true;
monad.bind = function() {
return monad;
}
}
}
var monad = maybe(null);
monad.bind(alert);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment