Open In App

Currying vs Partial Application in JavaScript

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.

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

Output
15

Applications and features of currying

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.

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

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.

Article Tags :