/monad-js

Douglas Crockford's monad library as UMD/ES6 module

Primary LanguageJavaScriptMIT LicenseMIT

monad-js

npm version npm module downloads Build Status License: MIT Dependency Status devDependencies Status Coverage Status

Douglas Crockford's monad library as UMD/ES6 module

Installation

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 of monad-js.js
  • monad-js.esm.js - ES6 module version, which can be used by bundlers like rollup or webpack

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 (or return, inject) operation which sticks/shoves some arbitrary data a into a monad M a. It could be viewed as a constructor or factory taking in a and returning M a. In short: unit = a => M a
  • bind (or pipe, >>=) operation taking in a monad M a and a function a => M b and combining them to return a new monad M 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's code

Douglas Crockford on Monads (video)

Notes from Crockford on Monads

The Marvels of Monads by Wes Dyer

Eric Lippert on Monads in .NET

License

MIT

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 install
npm run build

API 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