Facebook Pixel

Currying and Partial Application With Closures in JavaScript

Currying and partial application use closures to preset arguments. Here is how they work.

Currying and Partial Application With Closures in JavaScript

Currying and partial application are functional programming techniques that use closures to preset arguments. They let you create specialized functions from general ones.

Currying

Currying transforms a function that takes multiple arguments into a sequence of functions that each take one argument.

function curry(fn) { return function curried(...args) { if (args.length >= fn.length) return fn(...args); return (...next) => curried(...args, ...next); }; } const sum = curry((a, b, c) => a + b + c); sum(1)(2)(3); // 6 sum(1, 2)(3); // 6 sum(1)(2, 3); // 6 `` Each partial call returns a new function that closes over the accumulated arguments. When enough arguments are collected, the original function is called. ### Partial Application Partial application fixes some arguments and returns a function for the rest. ```js function partial(fn, ...presetArgs) { return (...laterArgs) => fn(...presetArgs, ...laterArgs); } const multiply = (a, b, c) => a * b * c; const double = partial(multiply, 2); double(3, 4); // 24 const triple = partial(multiply, 3); triple(3, 4); // 36 `` `double` closes over `presetArgs = [2]` and `fn = multiply`. When called with `(3, 4)`, it calls `multiply(2, 3, 4)`. ### Currying vs Partial Application - **Currying**: transforms `f(a, b, c)` into `f(a)(b)(c)` (one argument at a time). - **Partial application**: fixes some arguments and returns a function for the rest (`f(a, b, c)` -> `f'(b, c)` with `a` preset). ### Practical Example: Logging ```js const log = curry((level, message) => console.log(`[${level}] ${message}`)); const info = log("INFO"); const error = log("ERROR"); info("Started"); // [INFO] Started error("Crashed"); // [ERROR] Crashed `` `info` and `error` are specialized loggers created by currying. ### Practical Example: Event Handlers ```js const handler = curry((action, event) => { console.log(action, event.target); }); button.addEventListener("click", handler("clicked")); form.addEventListener("submit", handler("submitted")); `` ### The Takeaway Currying and partial application use closures to preset arguments. Currying transforms a multi-argument function into a chain of single-argument functions. Partial application fixes some arguments and returns a function for the rest. Both create specialized functions from general ones, powered by closures.

Transforming a function that takes multiple arguments into a sequence of functions that each take one argument. f(a, b, c) becomes f(a)(b)(c). Each partial call returns a new function that closes over the accumulated arguments.

Fixing some arguments of a function and returning a new function for the remaining arguments. f(a, b, c) with a preset becomes f'(b, c). The returned function closes over the preset arguments.

Currying transforms a function into a chain of single-argument functions (f(a)(b)(c)). Partial application fixes some arguments and returns a function for the rest (f(a, b, c) with a preset becomes f'(b, c)).

Each partial call returns a new function that closes over the accumulated arguments. When enough arguments are collected, the original function is called. The closures keep the intermediate arguments alive between calls.

Creating specialized functions from general ones. For example, currying a log function with a level parameter creates info and error loggers. Currying an event handler with an action creates click and submit handlers.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.