Skip to content

promiseT Monad Transformer: lifts any monad to operate on promises

License

Notifications You must be signed in to change notification settings

briancavalier/promiseT

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

promiseT Monad Transformer

Instead of creating a promise monad or merging the promise and monad interfaces into a larger, more confusing interface, I created a promiseT monad transformer, along the lines of the Haskell stateT monad transformer. Haskell does not have a State monad, but rather uses stateT to add stateful behavior to any other monad. For example, the monad returned by the state function is actually stateT applied to the identity monad.

The examples apply promiseT to a Javascript Array (which is not a Monad, but Array.prototype.map is a Functor, so we can work with that) and a simple Identity monad, yielding new promise-aware types in each case. These new types are not promises. They don't have then, but they can operate on promise values and their functor, applicative, and chain accept functions that deal with promises.

Both the Identity monad and it's promiseT-ified version obey the functor, applicative, chain, and monad laws, as they are stated in fantasy-land.

promiseT is independent of any particular promise implementation. It relies only on the Promises/A+ then API. The examples here use when.js, but my original gist version used avow. Switching required no changes to promiseT

Why?

There is currently a gap between the Promise and Monad communities, and I've been thinking about ways to bridge that. The two concepts are powerful and elegant, and I think it would be awesome for them to work together, and for the communities to converge.

Running examples

  1. Fork/clone
  2. npm install
  3. npm test - runs functor, applicative, chain, and monad law tests against the Id and promiseT(Id) monads
  4. node examples/list.js - lifts Javascript Array to be promise-aware and does some gymnastics with it.
  5. node examples/errors.js - shows how error handling might work in a way that is similar to Haskell's Either/catchError/throwError error handling approach.

Copyright © 2013 Brian Cavalier, MIT License

About

promiseT Monad Transformer: lifts any monad to operate on promises

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published