Douglas Crockford's monad library as UMD/ES6 module
Make sure Node.js is installed. Then run:
npm install monad-js --save
Following bundles are available:
monad-js.js
- UMD ES5 version for use in browser, node, etc.monad-js.min.js
- minified version ofmonad-js.js
monad-js.esm.js
- ES6 module version, which can be used by bundlers likerollup
orwebpack
The package could also be downloaded directly from: https://registry.npmjs.org/monad-js/-/monad-js-1.0.6.tgz
A monad is, depending on the language and implementation, an object
/ class
/ interface
/ type
with two required operations:
unit
(orreturn
,inject
) operation which sticks/shoves some arbitrary dataa
into a monadM a
. It could be viewed as a constructor or factory taking ina
and returningM a
. In short:unit = a => M a
bind
(orpipe
,>>=
) operation taking in a monadM a
and a functiona => M b
and combining them to return a new monadM b
. In short:bind = (M a, a => M b) => M b
Reason for existence of monads is to be able to compose functions that otherwise could not be composed, as they may be working on different domains. See Brian Beckman: Don't fear the Monad (video) for an excellent explanation.
See also:
Douglas Crockford on Monads (video)
Notes from Crockford on Monads
The Marvels of Monads by Wes Dyer
Eric Lippert on Monads in .NET
I wanted to be able to easily add Douglas Crockford's monad work to my projects
via npm
.
You are welcomed to improve this implementation or provide feedback. Please feel free to Fork, create a Pull Request or submit Issues. Thank you!
npm install
npm run build
The Identity Monad
const monad = identity("Hello world.");
monad.bind(alert);
Kind: static property of monadJs
Maybe monad is used to guard against Null Pointer Exceptions.
const monad = maybe(null);
monad.bind(alert); // Nothing happens.
Kind: static property of monadJs
The makeMonad
function is a macroid that produces monad constructor
functions. It can take an optional modifier
function, which is a function
that is allowed to modify new monads at the end of the construction processes.
A monad constructor (sometimes called unit
or return
in some mythologies)
comes with three methods, lift
, liftValue
, and method
, all of which can
add methods and properties to the monad's prototype.
A monad has a bind
method that takes a function that receives a value and
is usually expected to return a monad.
const identity = makeMonad();
const monad = identity("Hello world.");
monad.bind(alert);
const ajax = makeMonad()
.lift('alert', alert);
const monad = ajax("Hello world.");
monad.alert();
const maybe = makeMonad(function (monad, value) {
if (value === null || value === undefined) {
monad.isNull = true;
monad.bind = function () {
return monad;
};
return null;
}
return value;
});
const monad = maybe(null);
monad.bind(alert); // Nothing happens.
Kind: static property of monadJs
Create a vow
object, used to handle promises
NOTE: ES6 has native support for Promises. This implementation could still be used to create promises in ES5 though.
Kind: static constant of monadJs