Skip to content

Instantly share code, notes, and snippets.

@apburnes
Created November 20, 2013 22:15
Show Gist options
  • Save apburnes/7572089 to your computer and use it in GitHub Desktop.
Save apburnes/7572089 to your computer and use it in GitHub Desktop.
Ellipses
var __slice = Array.prototype.slice;
function variadic (fn) {
var fnLength = fn.length;
if (fnLength < 1) {
return fn;
}
else if (fnLength === 1) {
return function () {
return fn.call(
this, __slice.call(arguments, 0))
}
}
else {
return function () {
var numberOfArgs = arguments.length,
namedArgs = __slice.call(
arguments, 0, fnLength - 1),
numberOfMissingNamedArgs = Math.max(
fnLength - numberOfArgs - 1, 0),
argPadding = new Array(numberOfMissingNamedArgs),
variadicArgs = __slice.call(
arguments, fn.length - 1);
return fn.apply(
this, namedArgs
.concat(argPadding)
.concat([variadicArgs]));
}
}
};
function unary(first) {
return first;
}
unary('coo', 'lee', 'oh')
variadic(unary)('why', 'hello', 'you')
function bianary(first, rest) {
return [first, rest];
}
bianary('why', 'hello', 'you')
variadic(bianary)('why', 'hello', 'you')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment