Skip to Content
Course content
Click on the "Edit" button in the top corner of the screen to edit your slide content.

You've likely seen functions that take three or four arguments. They work fine, but they can get repetitive if you find yourself passing the same first two arguments over and over again. That's where currying comes in. It's not magic; it's just a way of rearranging how your functions receive their data.

What exactly is currying, and why not just pass all the arguments at once?

At its simplest, currying is the process of taking a function that accepts multiple arguments and turning it into a series of functions that each take one argument. Instead of f(a, b, c), you get f(a)(b)(c).

Now, you're probably thinking, "Why on earth would I want to call a function three times instead of once?" The power is in partial application. It allows you to "lock in" some arguments and create a new, more specialized function that you can reuse throughout your app.

// The "standard" way
const multiply = (a, b) => a * b;
console.log(multiply(2, 5)); // 10
console.log(multiply(2, 10)); // 20

// The curried way
const curriedMultiply = (a) => (b) => a * b;
const double = curriedMultiply(2); // 'a' is now locked as 2

console.log(double(5)); // 10
console.log(double(10)); // 20

In the example above, double is a specialized version of curriedMultiply. I didn't have to pass the 2 every single time. I created a tool specifically for doubling numbers.

How do I actually write this without getting lost in the syntax?

If you're using older function declarations, it looks like a set of Russian nesting dolls. But with arrow functions, it becomes a very clean, one-line chain. I almost always use arrow functions for currying because it makes the "flow" of data much easier to visualize.

Let's look at a real-world scenario: a logging system. Imagine you want to log messages, but you want to specify the severity (INFO, WARN, ERROR) and the component (API, UI, AUTH) before you actually log the message.

const logger = (severity) => (component) => (message) => {
  return `[${severity}] [${component}]: ${message}`;
};

// We can create a specialized logger for API errors
const apiError = logger('ERROR')('API');

console.log(apiError('Connection timeout')); // [ERROR] [API]: Connection timeout
console.log(apiError('Invalid API Key'));    // [ERROR] [API]: Invalid API Key

// Or a logger for UI info
const uiInfo = logger('INFO')('UI');
console.log(uiInfo('Button clicked'));       // [INFO] [UI]: Button clicked

Is this just a fancy academic trick, or does it actually save time?

It's definitely not just for showing off in interviews. I use this constantly when dealing with configuration or event handling. Whenever you have a set of "configuration" arguments that rarely change, and "data" arguments that change every time the function is called, currying is your best friend.

Think about it like this: currying lets you build a pipeline. You configure the function once at the top level of your application, and then you pass that configured function down to your components or helper modules. Those components don't need to know about the configuration; they just provide the final piece of data.

One thing to keep in mind: don't overdo it. If you're only ever going to call a function once with all its arguments, currying just adds unnecessary boilerplate. Use it when you see a pattern of repetition.




📋 Practical Task

Build a Regional Tax Calculator

You are building a checkout system for a store that operates in multiple countries. Each country has a different tax rate, and each product category has a different luxury surcharge. You need to create a curried function to handle this without repeating the tax rates for every single item.

Requirements:

  • Create a curried function called calculatePrice that takes three arguments in this order: taxRate, surcharge, and price.
  • The final returned value should be: price + (price * taxRate) + surcharge.
  • Use this curried function to create a specialized function called ukElectronics. This specialized function should have a taxRate of 0.20 (20%) and a surcharge of 5.
  • Test your ukElectronics function by passing it a price of 100. The result should be 125.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.