Skip to content

Instantly share code, notes, and snippets.

@johnhunter
Created November 7, 2013 17:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnhunter/7358910 to your computer and use it in GitHub Desktop.
Save johnhunter/7358910 to your computer and use it in GitHub Desktop.
Monad in Javascript
/*
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);