Skip to content

Instantly share code, notes, and snippets.

@ithinkihaveacat
Created February 18, 2010 01:03
Show Gist options
  • Save ithinkihaveacat/307214 to your computer and use it in GitHub Desktop.
Save ithinkihaveacat/307214 to your computer and use it in GitHub Desktop.
function add(i, j) {
return i + j;
}
Function.prototype.continuation = function() {
arguments[arguments.length - 1](
this.apply(this, Array.prototype.slice.call(arguments, 0, arguments.length - 1))
);
}
console.log(add(1, 2));
// -> 3
add.continuation(1, 2, function(x) { console.log(x); });
// -> 3
Function.prototype.continuable = function() {
var t = this, a = arguments;
return function(callback) {
callback(t.apply(t, a));
}
}
add.continuable(3, 4)(function (x) { console.log(x); });
// -> 7
Function.prototype.chainable = function() {
var t = this, a = arguments;
return function(self) {
return function(callback) {
callback(t.apply(self, a));
}
}
};
Array.prototype.map.chainable(function(x) { return x + 1; })([4, 3, 2])(function (x) { console.log(x); })
// -> [5, 4, 3]
Do.chain(
Array.prototype.filter.chainable(function(x) { return x > 3 })([1, 2, 3, 4, 5]),
Array.prototype.map.chainable(function(x) { return x + 2 })
)(function(x) {
sys.debug(sys.inspect(x));
});
// -> [6, 7]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment