Skip to content

Instantly share code, notes, and snippets.

@samleb
Created November 16, 2008 07:49
Show Gist options
  • Save samleb/25430 to your computer and use it in GitHub Desktop.
Save samleb/25430 to your computer and use it in GitHub Desktop.
// original `Function#delay`, seems we could refactor it using `curry` right ?
function delay(timeout) {
var __method = this, args = slice.call(arguments, 1);
timeout = timeout * 1000
return window.setTimeout(function() {
return __method.apply(__method, args);
}, timeout);
}
// Two ways of doing it !
// Rewrite 1 : using curry with "early" binding
// Ensures `curry` implementation is Prototype's one
function delay(timeout) {
var args = slice.call(arguments, 1);
__method = curry.apply(this, args);
return window.setTimeout(__method, timeout * 1000);
}
// Rewrite 2: using curry with "late" binding
// Uses own receiver's version of `curry`
function delay(timeout) {
var args = slice.call(arguments, 1);
__method = this.curry.apply(this, args);
return window.setTimeout(__method, timeout * 1000);
}
// From now say we're in client code, and some folk wrapped
// `curry`, because he's curious and wants to know when curry is called.
// (TextMate feature request : I need an Emacs psychiatrist clone !)
Function.prototype.curry = Function.prototype.curry.wrap(function(curry) {
var args = $A(arguments).slice(1);
console.log(this + " was curried");
return curry.apply(this, args);
});
// with rewrite 1:
(function() {}).delay(1,2,3);
// Nothing happens
// with rewrite 2:
(function() {}).delay(1,2,3);
// "function() { } was curried"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment