Skip to content

Instantly share code, notes, and snippets.

@tel
Created Jun 4, 2015
Embed
What would you like to do?
Continuation monad in Javascript
function Cont(contHandler) {
this.contHandler = contHandler;
};
Cont.unit = function(value) {
return new Cont(function (continue) { return continue(value); });
};
Cont.prototype.run = function(resume) {
return this.contHandler(resume);
};
Cont.prototype.escape = function() {
return this.run(function(x) { return x; });
};
Cont.prototype.doNothing = function () {
var self = this;
return new Cont(function(resume) {
return self.run(function(value) {
return resume(value);
});
});
};
Cont.prototype.doNothing = function () {
var self = this;
return new Cont(function(resume) { return self.run(resume); });
};
Cont.prototype.bind = function (transform) {
var self = this;
return new Cont(function(resume) {
return self.run(function (value) {
return transform(value).run(resume);
});
});
};
// > Cont.unit(3).bind(function(x) { return Cont.unit(4).bind(function (y) { return Cont.unit(x + y); }); }).escape();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment