monad-js v1.0.6
monad-js
Douglas Crockford's monad library as UMD/ES6 module
Installation
Make sure Node.js is installed. Then run:
npm install monad-js --saveFollowing bundles are available:
monad-js.js- UMD ES5 version for use in browser, node, etc.monad-js.min.js- minified version ofmonad-js.jsmonad-js.esm.js- ES6 module version, which can be used by bundlers likerolluporwebpack
The package could also be downloaded directly from: https://registry.npmjs.org/monad-js/-/monad-js-1.0.6.tgz
More information
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 dataainto a monadM a. It could be viewed as a constructor or factory taking inaand returningM a. In short:unit = a => M abind(orpipe,>>=) operation taking in a monadM aand a functiona => M band 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
License
Motivation
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!
Development
npm installnpm run buildAPI Reference
monadJs.identity
The Identity Monad
const monad = identity("Hello world.");
monad.bind(alert);Kind: static property of monadJs
monadJs.maybe
Maybe monad is used to guard against Null Pointer Exceptions.
const monad = maybe(null);
monad.bind(alert); // Nothing happens.Kind: static property of monadJs
monadJs.makeMonad
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
monadJs.vow
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