Course contentsShow
← Computer Science
Lesson 2138 of 2,87248. Programming ParadigmsFree lesson

Currying and Partial Application

Transform multi-argument functions into chains of single-argument functions.

Currying and Partial Application

What you'll learn: How to transform multi-argument functions into chains of single-argument functions and create specialized versions of existing functions.

The Core Idea

Imagine you have a function that takes three arguments at once: add(a, b, c). Currying transforms this into a chain where each step takes one argument and returns a new function: add(a)(b)(c). Partial application is simpler—it fixes some arguments upfront and returns a function waiting for the rest.

Think of currying like a vending machine where you insert one coin at a time, each action unlocking the next slot. Partial application is like pre-loading some coins and handing the machine to someone else to finish.

Currying

A curried function breaks multi-argument calls into nested single-argument functions:

// Regular function
function add(a, b, c) {
  return a + b + c;
}
add(1, 2, 3);  // 6

// Curried version
function curriedAdd(a) {
  return function(b) {
    return function(c) {
      return a + b + c;
    };
  };
}
curriedAdd(1)(2)(3);  // 6

Each call returns a closure capturing the previous arguments.

Partial Application

Partial application fixes some arguments now, creating a specialized function:

function multiply(a, b, c) {
  return a * b * c;
}

// Partial application: fix first argument
function multiplyBy2(b, c) {
  return multiply(2, b, c);
}
multiplyBy2(3, 4);  // 24

You've created a more specific function from a general one—perfect for callbacks or reusable utilities.

Why It Matters

Both techniques leverage closures to create flexible, composable functions. Currying enables elegant function composition by standardizing to single-argument functions. Partial application creates specialized tools from general ones without repetition.

Key Takeaway: Currying transforms f(a, b, c) into f(a)(b)(c) for composition; partial application fixes some arguments early to create specialized functions—both unlock powerful functional programming patterns.