Created
November 7, 2013 17:54
-
-
Save johnhunter/7358910 to your computer and use it in GitHub Desktop.
Monad in Javascript
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
/* | |
From Doug Crockford's talk 'Monads and Gonads' | |
*/ | |
/* | |
Doug calls this a Macroid - a JavaScript version of a macro. | |
Its purpose is to create the monad unit function for a specific monad type | |
*/ | |
function MONAD (){ | |
return function unit (value) { | |
var monad = Object.create(null); | |
monad.bind = function (func) { | |
return func(value); | |
}; | |
return monad; | |
}; | |
} | |
// create an identity monad | |
var identity = MONAD(); | |
var monad = identity('Hello World'); | |
monad.bind(alert); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See promise implementation at https://github.com/douglascrockford/monad
and the lecture http://www.youtube.com/watch?v=dkZFtimgAcM
Further reading from the talk: