Open In App

Currying vs Partial Application in JavaScript

Last Updated : 12 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Currying and partial application are both techniques used in functional programming languages to manipulate functions. They both involve breaking down functions with multiple arguments into functions that take fewer arguments. However, they have subtle differences in their implementation and applications.

What is Currying?

Currying is the process of transforming a function that takes multiple arguments into a sequence of functions that each take a single argument. In other words, it converts a function of n arguments into a chain of n functions, each taking one argument. This allows for partial application, as well as easier function composition and reusability.

Understanding currying practically

The below practical implementation uses the add() that takes a parameter x function that returns another function that accepts another parameter y which returns the product of the numbers x and y inside the curried function.

JavaScript
function add(x) {
    return function (y) {
        return x * y;
    };
}
const product = add(5)(3);
console.log(product);

Output
15

Applications and features of currying

  • Partial application: Currying allows for partial application of functions, which means providing a function with fewer arguments than it expects and getting back a new function that accepts the remaining arguments.
  • Function composition: Currying facilitates function composition by breaking down complex functions into smaller, composable units.
  • Reusability: Currying promotes code reusability by creating modular functions that can be easily reused in different contexts.

What is Partial Application?

Partial application is the process of fixing a number of arguments to a function, producing another function of smaller arity (number of arguments). Unlike currying, partial application involves fixing a subset of arguments of a function and returning a new function with those arguments already set.

Understanding partial application practically

In the below implementation, the partial application is explained practically with the help of the JavaScript code.

JavaScript
function greet(greeting, name) {
    return `${greeting}, ${name}!`;
}

const greetHello = greet.
    bind(null, "Hello");
console.log(greetHello("Alice"));

Output
Hello, Alice!

Applications and features of partial application

  • Creating specialized functions: Partial application allows for creating specialized versions of functions by fixing certain arguments, which can be useful in scenarios where certain arguments are often repeated.
  • Code simplification: It simplifies code by reducing the need for redundant argument passing, especially when some arguments are fixed for multiple function calls.
  • Improved readability: Partial application can improve code readability by making function calls more concise and focused on the specific task at hand.

Difference between currying and partial application

The below table lists some of the key differences between currying and partial application.

Feature

Currying

Partial Application

Argument Application

Converts a function taking multiple arguments into a sequence of functions each taking one argument

Fixes some arguments, leaving others open for later invocation

Return Value

Returns a new function until all arguments are provided

Returns a new function with some arguments pre-filled

Use Cases

Currying is particularly useful for creating functions with fixed initial arguments, where the remaining arguments can vary. It facilitates function composition and reusability.

Partial application is handy when you want to create specialized versions of functions with certain arguments pre-filled, leaving others open for later customization.

Performance:

Currying may lead to the creation of multiple function closures, potentially impacting memory usage and performance, especially when dealing with deeply nested curried functions.

Partial application, especially when achieved through methods like bind(), may have better performance as it directly binds arguments to the function without additional nesting.

Invocation

Requires calling the function once for each argument


Immediately applies some arguments, returns a new function

Conclusion

Currying and partial application are both powerful techniques used in functional programming to manipulate functions and facilitate code reuse and composition. While they share similarities, such as reducing the arity of functions, they differ in their approach and application. Currying breaks down functions into a sequence of single-argument functions, while partial application fixes a subset of arguments to produce specialized functions. Both techniques enhance code readability, reusability, and composability, making them valuable tools in functional programming paradigms.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads