javascript / intermediate
Snippet
Function Currying
Currying is a pattern where a function with multiple arguments is transformed into a sequence of nesting functions, each taking a single argument. It is useful for partial application and creating reusable utility functions.
snippet.js
1
2
3
4
5
6
7
const multiply = (a) => (b) => (c) => a * b * c;const double = multiply(2);const doubleAndTriple = double(3);console.log(doubleAndTriple(4)); // 24 (2 * 3 * 4)console.log(multiply(2)(3)(4)); // 24
Breakdown
1
const multiply = (a) => (b) => (c) => ...
A series of arrow functions that capture arguments through closures.
2
const double = multiply(2);
Creates a specialized function where the first argument 'a' is locked to 2.