Skip to content

Instantly share code, notes, and snippets.

@ForbesLindesay
Last active December 16, 2015 03:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ForbesLindesay/ce6567689fb848d23cda to your computer and use it in GitHub Desktop.
Save ForbesLindesay/ce6567689fb848d23cda to your computer and use it in GitHub Desktop.
These are my notes after watching http://www.youtube.com/watch?v=dkZFtimgAcM and then attempting to establish whether promises for promises must be allowed in order to not restrict our ability to reuse code.

These examples assume a mythical ==== object which means "functionally equivallent". In this case, the same type of monad with the same internal state.

The unit function for the Monad example should take a value and return a monad for that value.

The f and g functions in the monad example take values and return monads, in the promise example they take values and return promies.

The axioms in the promise example hold only if value is not a promise. I still can't come up with a situation where this matters.

unit(value).bind(f) ==== f(value)
monad.bind(unit) ==== monad
monad.bind(f).bind(g) ==== monad.bind(function (value) { return f(value).bind(g) });
function unit(value) {
return new Promise(function (resolve) { resolve(value) });
}
unit(value).then(f) ==== f(value);
promise.then(unit) ==== promise;
promise.then(f).then(g) ==== promise.then(function (value) { return f(value).then(g); });
@Twisol
Copy link

Twisol commented Apr 12, 2013

FWIW, the left identity on Promises (the first in your code) is broken in the presence of side-effects - see here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment