documentcloud/underscore-contrib — pull request #19 by raganwald (2013-05-03 15:23:16+00:00)
Title: Proposed: _.arity for forcing a function's arity

The use case is writing combinators that respect the arity of the functions they decorate. e.g.

function reverse (list) {
return [].reduce.call(list, function (acc, element) {
acc.unshift(element);
return acc;
}, []);
}

function naiveFlip (fun) {
return function () {
return fun.apply(this, reverse(arguments));
}
}

function echo (a, b, c) { return [a, b, c]; }

deepEqual(naiveFlip(echo)(1, 2, 3), [3, 2, 1], "naive flip flips its arguments");
notEqual(naiveFlip(echo).length, echo.length, "naiveFlip gets its arity wrong");

function flipWithArity (fun) {
return _.arity(fun.length, naiveFlip(fun));
}

deepEqual(flipWithArity(echo)(1, 2, 3), [3, 2, 1], "flipWithArity flips its arguments");
equal(flipWithArity(echo).length, echo.length, "flipWithArity gets its arity correct");