Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptHow to uncurry a function up to depth n in JavaScript ?

How to uncurry a function up to depth n in JavaScript ?

The following approach covers how to uncurry a function up to depth n in JavaScript. Uncurry a function is a process of wrapping function arguments and passing all the arguments at once. Uncurry up-to-depth n means we have passed only n arguments in all arguments. 

This can be done by following methods:

Using reduce() Method: Reduce method is used to apply an operation on all the elements of the list and returns the result. It takes a callback that applies to all the elements. To slice args up to n we use the slice() method. The slice() method extracts part of the elements from the list. The slice() takes starting index and end index up to what we want to extract. Starting index is by default 0 if not provide any args. End index we can provide up to the end of the list.

Example: 

Javascript




<script>
// Uncurry that unwrap function
let uncurry = (f, n) => (...args) => {
 
    // Function that apply function to all elements
    let func = f => a => a.reduce((l, m) => l(m), f)
 
    // If args are less than n return
    if (n > args.length)
        return " less args provide ";
 
    // Apply function up to n
    return func(f)(args.slice(0, n));
}
 
let n = 3;
// Function sum three values
const sum = x => y => z => x + y + z;
let t = uncurry(sum, n);
let ans = t(1, 2, 4, 5);
 
console.log(`Sum of ${n} args are ${ans}`);
</script>


 Output:

Sum of 3 args are 7

Using for-of loop Method: In this method, we use for-of loop. The for-of loop is used to iterate over all the elements of the list. Following are the implementation of the method:

  • Define n and sum function that add all provided arguments.
  • Call uncurry function with the sum, n, and all list of numbers.
  • The uncurry function checks if n is greater than arguments then returns fewer args provide.
  • Else it iterates the overall argument and applies function sum to each argument.
  • In for loop, we count iteration. When iteration becomes equal to n break the loop.
  • At the end returns f which sum of all passed elements.

Example: 

Javascript




// Sum function that we have to uncurry
const sum = x => y => z => x + y + z;
 
// Function that uncurry  function up-to
// n depth  with for loop
const uncurry = f => n => {
    let k = 0;
 
    return (...args) => {
 
        if (n > args.length) {
            return "n";
        }
        for (let arg of args) {
 
            if (k === n)
                break;
            k++
            f = f(arg);
        }
        return f
    };
 
}
 
// Creating uncurry function with sum function and 3 value
let n = 3;
const uncurried = uncurry(sum)(n);
 
let ans = uncurried(0, 1, 7, 2, 4);
console.log(`Sum of ${n} args is ${ans}`);


 Output:

Sum of 3 args is 8

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments