Skip to content

Instantly share code, notes, and snippets.

@zspencer
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zspencer/202f1381783d25ebe500 to your computer and use it in GitHub Desktop.
Save zspencer/202f1381783d25ebe500 to your computer and use it in GitHub Desktop.
So, you want to define partials in javascript, eh?
// Given a function and a set of arguments, returns a function that, when called,
// uses the originally given arguments as well as the new arguments. Heavily inspired by
// Clojure: https://clojuredocs.org/clojure.core/partial
//
// Example:
// > function logger() { console.log(arguments); }
// > var prefixedLogger = partial("hi", "there);
// > prefixedLogger("mom");
// { '0': 'hi', '1': 'there', '2': 'mom' }
function partial(fn, others) {
// This is inefficient. See https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#32-leaking-arguments
var others = Array.prototype.slice.call(arguments, 1);
return function() {
return fn.apply(this, others.concat(Array.prototype.slice.call(arguments)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment