Course contentsShow
← JavaScript
Lesson 483 of 2,72312. Higher-Order FunctionsFree lesson

Composition with Arity and Currying

How curried functions enable better composition by ensuring unary function signatures.

Composition with Arity and Currying

You'll learn how curried functions make composition smoother by standardizing function signatures to accept one argument at a time.

Why Arity Matters in Composition

When composing functions, each function's output becomes the next function's input. This works beautifully when functions accept exactly one argument (unary functions). However, many useful functions naturally take multiple arguments, creating a mismatch.

Arity is the number of arguments a function expects. A function with arity 2 (binary) can't slot cleanly into a composition chain without adaptation.

How Currying Enables Composition

Currying transforms multi-argument functions into chains of unary functions. Instead of add(2, 3), you get add(2)(3). Each step takes one input and returns one output—perfect for composition.

Consider a scenario where you want to compose operations that require configuration:

// Without currying: awkward composition
const multiply = (a, b) => a * b;
// Can't easily compose this!

// With currying: smooth composition
const multiply = a => b => a * b;
const double = multiply(2);
const triple = multiply(3);

const pipe = (...fns) => x => fns.reduce((acc, fn) => fn(acc), x);

const doubleThenTriple = pipe(double, triple);
doubleThenTriple(5); // 30

Each curried function returns another function, maintaining the unary signature needed for clean composition. You configure the behavior first (multiply(2) creates double), then that configured function slots perfectly into your composition pipeline.

Key Takeaway: Currying converts multi-argument functions into unary function chains, making them composable without awkward wrappers or parameter juggling—each function takes one value, transforms it, and passes it along.