Open In App

How to create a function that invokes function with partials prepended arguments in JavaScript ?

In this article, we will see how to create a function that invokes functions with partials prepended to the arguments it receives in JavaScript. Before understanding the problem statement and approaching the solution for the same, let’s, first of all, know what a function (also called a method) is and how a function gets executed.

A function (or a method) is a block of code that performs a particular task for which the function is made. A function can be called any number of times inside the program to perform that particular task. Dividing a large-sized code into a small block of code not only increases the readability of the code but also helps the developer to find the bug in the particular section of the code. This makes the program net & clean as well as makes it more authentic. Basically, for executing a particular task for more than one time, we can write our logic part in a separate function (method) and can use that function whenever we need it in a program.



There are many ways by which we can create a simple function (method), like by using the function keyword or creating arrow functions, the syntax would be a little different in both cases, but eventually, the result will remain the same. The following is the syntax by which we can declare our function (method) and execute it further-




function displayName (name) {
    return name;
}
console.log(displayName("GeeksforGeeks"));

 



The output of the above code would be as follows-

GeeksforGeeks

We may declare our method or function in the form of arrow functions (ES6 version format) by using the following syntax-




let displayName = (name) =>{
    return name;
}
console.log(displayName("GeeksforGeeks"));

The output of the above code would be as follows-

GeeksforGeeks

Now let’s understand the problem statement, for which we need to find out the particulars too. The following pictorial representation describes the problem statement-

We need to create a function that is responsible for invoking another function which consists of several partials (contents) prepended to the arguments in it. A user thus uses the main function to display the result which will indirectly use another function containing information. Now that we have understood the problem statement, then we will move further. We will now see different approaches to solve this particular problem.

The following are the approaches through which we could solve our problem with ease-

Approach 1:




function partial(fn, ...partials) {
  return function (...args) {
    return fn(...partials, ...args);
  };
}
  
function greet(greeting, name) {
  return greeting + " " + name;
}
  
let greetMessage = partial(greet, "Hello");
  
console.log(greetMessage("GeeksforGeeks"));

The output of the above code snippet would be as follows-

Hello GeeksforGeeks

Approach 2:




let partialFunction = (fn, ...partials) => (
    ...args) => fn(...partials, ...args);
  
let greet = (greeting, name) => greeting + " " + name;
  
let greetMessage = partialFunction(greet, "Hello");
  
console.log(greetMessage("GeeksforGeeks"));

The output of the above code snippet would be as follows-

Hello GeeksforGeeks

Article Tags :