Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax: _
3.2.
Parameters:
3.3.
Return Value: 
4.
Examples 
4.1.
Limiting Arguments for a Callback Function:
4.2.
JavaScript
4.3.
Using with Array.map:
4.4.
JavaScript
4.5.
Limiting Event Handler Arguments:
4.6.
JavaScript
4.7.
Restricting Arguments in a Higher-Order Function:
4.8.
JavaScript
5.
Frequently Asked Questions 
5.1.
Can _.ary() handle default parameters in the capped function?
5.2.
What happens if n is larger than the number of parameters in func?
5.3.
Is _.ary() commonly used in functional programming?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.ary() Method

Author Riya Singh
0 upvote
Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

Introduction

In JavaScript, controlling the number of arguments passed to a function can be crucial for maintaining stability and ensuring expected behavior, especially when dealing with callbacks and higher-order functions. Lodash offers a solution with the _.ary() method. This function creates a version of a given function that accepts up to n arguments, ignoring any additional arguments. 

Lodash _.ary() Method

This article will explore _.ary(), its functionality, use cases, and advantages, illustrated through examples and FAQs.

Why This Function is Used

The _.ary() function is used to cap the number of arguments passed to a function. This is particularly useful when a function is used as a callback or passed to other functions that might provide more arguments than it can handle or expects. By restricting the number of arguments, _.ary() helps maintain the function's intended behavior and prevents potential issues arising from unexpected arguments.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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

JavaScript

var _ = require('lodash');

function sum(a, b) {

 return a + b;

}

var cappedSum = _.ary(sum, 2);

console.log(cappedSum(1, 2, 3));

Output: 

3 


(ignores the third argument)

Demonstrates limiting a sum function to only two arguments.

Using with Array.map:

  • JavaScript

JavaScript

var numbers = [1, 2, 3];

var cappedParseInt = _.ary(parseInt, 1);

var parsedNumbers = numbers.map(cappedParseInt);

console.log(parsedNumbers);

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

JavaScript

function logEvent(event, element) {

 console.log('Event type:', event.type, 'on element:', element);

}

var cappedLogEvent = _.ary(logEvent, 1);

document.addEventListener('click', cappedLogEvent);

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

JavaScript

function multiply(x, y, z) {

 return x * y * z;

}

var double = _.ary(multiply, 2);

console.log(double(2, 3));

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 DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass