DEV Community

Cover image for JavaScript Concept: Currying and its implementation in Redux
Alifa Ara Heya
Alifa Ara Heya

Posted on

JavaScript Concept: Currying and its implementation in Redux

πŸ” What is Currying?

Currying is a functional programming technique where a function with multiple arguments is transformed into a sequence of functions, each taking one argument at a time.

Instead of:

const add = (a, b) => a + b;
Enter fullscreen mode Exit fullscreen mode

You curry it as:

const curriedAdd = (a) => (b) => a + b;
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Why Use Currying?

  • Reusability: You can create specialized versions of functions.
  • Function composition: Useful in pipelines and functional chains.
  • Cleaner syntax for functional operations.

βœ… Examples

1. Normal Function

const add = (a, b) => a + b;
console.log(add(2, 3)); // 5
Enter fullscreen mode Exit fullscreen mode

2. Curried Function

const curriedAdd = (a) => (b) => a + b;
console.log(curriedAdd(3)(5)); // 8
Enter fullscreen mode Exit fullscreen mode

3. Multi-level Curried Function

function curriedAdd2(a) {
  return function (b) {
    return function (c) {
      return a + b + c;
    };
  };
}
console.log(curriedAdd2(4)(6)(2)); // 12
Enter fullscreen mode Exit fullscreen mode

4. Real-Life Example: Calculating Discount

Normal:

const totalPrice = (amount, discount) => amount - amount * discount;
console.log(totalPrice(100, 0.3)); // 70
Enter fullscreen mode Exit fullscreen mode

Curried:

const curriedTotalPrice = (discount) => (amount) => amount - amount * discount;
const withDiscount = curriedTotalPrice(0.3);

console.log(withDiscount(100)); // 70
console.log(withDiscount(200)); // 140
console.log(withDiscount(250)); // 175
Enter fullscreen mode Exit fullscreen mode

This helps you reuse the same discount logic without repeating the discount value every time.


πŸ›  Currying in Redux Middleware

Redux middleware often uses currying to handle the flow of store β†’ next β†’ action.

const logger = (store) => (next) => (action) => {
  console.group(action.type);
  console.info("previous state", store.getState());
  const result = next(action);
  console.info("next state", store.getState());
  console.groupEnd();
  return result;
};

export default logger;
Enter fullscreen mode Exit fullscreen mode

This is a triple-curried function:

  • (store) gives access to the state and dispatch
  • (next) passes control to the next middleware or reducer
  • (action) is the actual dispatched action

Used in store.ts like this:

import logger from "./middlewares/logger";

middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(logger);
Enter fullscreen mode Exit fullscreen mode

🧩 Summary

Concept Explanation
Currying Transforming a function so it takes arguments one at a time
Syntax f(a, b) β†’ f(a)(b)
Use Case Reusability, configuration, partial application
Redux Currying is used in middleware to access store, next, and action

--this article is written with the help of AI.

Top comments (0)