Currying And Function Composition Using Lodash Flow
Last Updated :
01 Oct, 2024
Currying and Function composition are essential concepts in functional programming that enhance code reusability and readability.
- Currying transforms a function with multiple parameters into a series of functions, each taking a single parameter.
- Function composition allows us to combine multiple functions into one, where the output of one function becomes the input for the next.
Lodash provides more powerful utilities to implement both concepts effectively. In this article, we will understand curry and function composition using Lodash flow.
Currying with Lodash
Currying is a technique of breaking down a function that accepts multiple arguments into a sequence of functions that each take a single argument. This allows for partial application of arguments.
Syntax:
const curriedFunction = _.curry(originalFunction);
Example: In this example, we will add function takes three arguments but is transformed into a curried function using _.curry(). This allows us to create a partially applied function, addFive which takes the first argument (5) and later accepts the remaining arguments (3 and 2) to compute the final result (10).
JavaScript
// Requiring the lodash library
const _ = require("lodash");
// A simple function that adds three numbers
const add = (a, b, c) => a + b + c;
const curriedAdd = _.curry(add);
const addFive = curriedAdd(5);
const result = addFive(3, 2);
console.log(result);
Output
10
Function Composition with Lodash's _.flow().
Function composition is the process of combining multiple functions to create a new function. The new function executes the provided functions in sequence.
Syntax:
const composedFunction = _.flow([function1, function2, ...]);
Example: Here, the double function and addFive the function is composed using _.flow() creating a new function, doubleThenAddFive. When we call this composed function with an input of 3, it first doubles the value (resulting in 6) and then adds five, yielding the final output of 11.
JavaScript
// Requiring the lodash library
const _ = require("lodash");
const double = (x) => x * 2;
const addFive = (x) => x + 5;
// Composing the functions using _.flow
const doubleThenAddFive = _.flow([double, addFive]);
// Using the composed function
const result = doubleThenAddFive(3);
console.log(result);
Output
11
Combining Currying and Function Composition
We can also combine currying with function composition for more complex scenarios, allowing for flexible function applications.
Syntax:
const composedFunction = _.flow([
_.curry(functionToCurry),
functionToCompose
]);
Example: Here, the curried multiply the function is partially applied with the first argument (2) and then composed with the addOne Function. When called with additional arguments (3 and 4), it first multiplies them (2 * 3 * 4 = 24) and then adds one, resulting in the final output of 25.
JavaScript
// Import Lodash
import { curry, flow } from 'lodash';
// A curried function that multiplies three numbers
const multiply = curry((a, b, c) => a * b * c);
const addOne = (x) => x + 1;
// Composing the curried multiply function with addOne
const multiplyThenAddOne = flow([
multiply(2),
// Partially applying the first argument
addOne
]);
// Using the composed function
const result = multiplyThenAddOne(3, 4);
console.log(result);
Output
25
Similar Reads
Scala | Function Composition Function composition is a way in which a function is mixed with other functions. During the composition the one function holds the reference to another function in order to fulfill it's mission. There are some different ways in which a function composition can take place, as below :- compose : Compo
3 min read
Lodash Function Complete Reference Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc. It provides us with various inbuilt functions and uses a functional programming approach which makes coding in javascript easier to understand because instead o
2 min read
A Quick Introduction to pipe() and compose() in JavaScript Functional programming is gaining popularity in JavaScript development. Two fundamental concepts in functional programming are function composition and piping, which are implemented using compose() and pipe() respectively. These concepts allow for the creation of complex operations by combining simp
3 min read
What is Currying Function in JavaScript? Currying is used in JavaScript to break down complex function calls into smaller, more manageable steps. It transforms a function with multiple arguments into a series of functions, each taking a single argument.It converts a function with multiple parameters into a sequence of functions.Each functi
3 min read
Difference between lodash and Underscore The lodash and UnderScore both are utility libraries from JavaScript which helps make it easier by providing utils which makes, working with arrays, numbers, objects, and strings much easier. They provide a group of tools used for common programming operations having a strong functional programming
3 min read
When should one use Arrow functions in ES6 ? In this article, we will try to understand when should one use Arrow functions in ES6 instead of the traditional function's syntax with the help of some examples. Before analyzing why we should use arrow functions, let us first understand the basic details which are associated with the arrow functio
4 min read