Syntax, Parameter and Return Value
Syntax: _
.ary(func, [n=func.length])
Parameters:
-
func (Function): The function to cap arguments for.
- [n=func.length] (number): The arity cap.
Return Value:
(Function) - Returns the new capped function.
Examples
Limiting Arguments for a Callback Function:
JavaScript
var _ = require('lodash');
function sum(a, b) {
return a + b;
}
var cappedSum = _.ary(sum, 2);
console.log(cappedSum(1, 2, 3));

You can also try this code with Online Javascript Compiler
Run Code
Output:
3
(ignores the third argument)
Demonstrates limiting a sum function to only two arguments.
Using with Array.map:
JavaScript
var numbers = [1, 2, 3];
var cappedParseInt = _.ary(parseInt, 1);
var parsedNumbers = numbers.map(cappedParseInt);
console.log(parsedNumbers);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[1, 2, 3]
(avoids parseInt's second argument - radix)
Shows using _.ary() to avoid unexpected behavior with parseInt in Array.map.
Limiting Event Handler Arguments:
JavaScript
function logEvent(event, element) {
console.log('Event type:', event.type, 'on element:', element);
}
var cappedLogEvent = _.ary(logEvent, 1);
document.addEventListener('click', cappedLogEvent);

You can also try this code with Online Javascript Compiler
Run Code
Logs only the event object, ignoring the additional arguments provided by `addEventListener`.
An example of using _.ary() to limit arguments passed to an event handler.
Restricting Arguments in a Higher-Order Function:
JavaScript
function multiply(x, y, z) {
return x * y * z;
}
var double = _.ary(multiply, 2);
console.log(double(2, 3));

You can also try this code with Online Javascript Compiler
Run Code
Output:
NaN (as third argument z is undefined)
Demonstrates capping a function to two arguments when used in a higher-order context.
Frequently Asked Questions
Can _.ary() handle default parameters in the capped function?
Yes, _.ary() can handle default parameters. However, it will only pass the first n arguments, and any default parameters beyond these will be used if needed.
What happens if n is larger than the number of parameters in func?
If n is larger than the function's parameters, _.ary() will still pass only the actual arguments provided to the function, up to n.
Is _.ary() commonly used in functional programming?
Yes, _.ary() is a common utility in functional programming to control function arity, especially when functions are used as arguments or callbacks in higher-order functions.
Conclusion
Lodash's _.ary() method is a useful tool for managing the arity of functions, particularly in scenarios involving callbacks, higher-order functions, or events. It ensures that functions only receive the number of arguments they are designed to handle, contributing to more predictable and stable code execution.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.