/* jsclass http://jsclass.jcoglan.com/ npm install jsclass */ require('jsclass'); JS.require('JS.Deferrable'); Promise = new JS.Class({ include: JS.Deferrable, initialize: function(value) { if (value !== undefined) this.succeed(value); } }); // foldl :: (a -> b -> a) -> a -> [b] -> a function foldl (g, a, bs) { var ret = a; for (var i = 0, n = bs.length; i < n; i++) { ret = g(ret, bs[i]); } return ret; }; // unit :: a -> Promise a function unit(x) { return new Promise(x); }; // wrap :: a -> Promise () -> void function wrap(x, p) { p.succeed(x); }; // bind :: Promise a -> (a -> Promise b) -> Promise b function bind(input, f) { var output = new Promise(); input.callback(function(x) { f(x).callback(function(y) { output.succeed(y); }); }); return output; }; exports.Promise = Promise; exports.unit = unit; exports.wrap = wrap; exports.bind = bind; exports.pipe = foldl.bind(null, bind);