javascript / intermediate
Snippet
Functional Currying
Currying is a functional programming technique where a function with multiple arguments is transformed into a sequence of functions, each taking a single argument. This allows for partial application and creating specialized reusable functions.
snippet.js
1
2
3
4
const multiply = (a) => (b) => a * b;const double = multiply(2);console.log(double(5)); // 10console.log(multiply(3)(4)); // 12
Breakdown
1
const multiply = (a) => (b) => a * b;
A higher-order function that returns another function.
2
const double = multiply(2);
Creates a new function where 'a' is permanently set to 2.
3
console.log(double(5));
Invokes the inner function with 'b' as 5, resulting in 2 * 5.