Open In App

Difference Between Partial Application and Currying in javascript

Last Updated : 11 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Partial Application

Partial Application is a technique used to fix or ‘partially’ apply some of the arguments of a function, creating a new function that expects the remaining arguments. This simplifies function calls and provides greater flexibility, making it easier to reuse and customize functions for specific use cases.

Example: In this example, we partially apply the greet function by fixing the greeting as “Good morning”. We obtain a new function greetMorning that only expects the name parameter.

Javascript




function GFG(greeting, name) {
    return `${greeting}, ${name}!`;
}
// Creating a partially applied function
const greetMorning = GFG.bind(null, 'Good morning');
console.log(greetMorning('Kumar'));


Output

Good morning, Kumar!

Currying

Currying is a specialized technique related to partial application. It involves transforming a function that accepts multiple arguments into a series of unary functions. Each unary function takes one argument and returns another unary function. This process continues iteratively until all the original function’s arguments are processed. The Currying is particularly useful in functional programming for creating more modular and reusable code, as it allows for the step-by-step application of arguments, making it easier to compose and customize functions.

Example: In this example, we create a curried function curryAdd to add two numbers and provide the arguments step by step and we achieve partial application through currying.

Javascript




function GFG(x) {
    return function(y) {
        return x + y;
    };
}
const add = GFG(5)(3);
// Partial application using currying
console.log(add);


Output

8

Difference Between Partial Application and Currying in javascript:

Characteristics

Partial Application

Currying

Argument Fixing

The Fixes a specific number of arguments and generates a new function.

The Transforms a multi-argument function into a series of the unary functions.

Function Return

The Returns a new function that expects remaining arguments to be passed later.

The Returns unary functions that each process one argument and return the another unary function.

Usage Simplicity

Simplifies function calls by the breaking them into multiple steps.

Requires more concise function calls with the one argument per function call.

Specialization

The Offers a more general approach and suitable for the various use cases.

Provides a specialized form of the Partial Application with the specific data transformation purpose.

Implementation

The Achieved using the methods like Function.prototype.bind or custom functions.

Implemented by the creating nested functions or custom currying functions.

Parameter Processing

The Accepts all required arguments at once making the some arguments optional.

Expects one argument at a time enforcing a specific order of the parameter processing.

Readability

The Generally more readable for the functions with the optional parameters.

The Requires understanding the concept of the unary functions and may be less intuitive for the some developers.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads