Last active
August 29, 2015 14:13
-
-
Save zspencer/202f1381783d25ebe500 to your computer and use it in GitHub Desktop.
So, you want to define partials in javascript, eh?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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