Last active
January 27, 2016 03:01
-
-
Save PatrickJS/5844383 to your computer and use it in GitHub Desktop.
Maybe Monad
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
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