JavaScript Currying

JavaScript Currying

JavaScript Currying

Currying is a functional programming technique in JavaScript where a function is transformed into a series of nested functions, each taking a single argument.

šŸ”¹ 1. What is Currying?

A curried function does not take all arguments at once. Instead, it returns a new function that takes the next argument and continues until all arguments are provided.

Example: Normal Function vs. Curried Function

Normal Function

function multiply(a, b, c) { return a * b * c; } console.log(multiply(2, 3, 4)); // 24

Curried Function

function curriedMultiply(a) { return function(b) { return function(c) { return a * b * c; }; }; } console.log(curriedMultiply(2)(3)(4)); // 24

Each function takes one argument and returns another function until all arguments are provided.

šŸ”¹ 2. Why Use Currying?

Reusability: Create reusable functions with fixed arguments.
Avoid Repetition: Store partial functions to reduce redundancy.
Functional Programming: Encourages pure functions and immutability.
Improved Readability: Functions become more modular and readable.

šŸ”¹ 3. Writing a General Curried Function

Instead of manually nesting functions, we can create a generic currying function.

function curry(fn) { return function curried(...args) { if (args.length >= fn.length) { return fn.apply(this, args); } else { return function (...nextArgs) { return curried(...args, ...nextArgs); }; } }; } // Example usage function sum(a, b, c) { return a + b + c; } const curriedSum = curry(sum); console.log(curriedSum(1)(2)(3)); // 6 console.log(curriedSum(1, 2)(3)); // 6 console.log(curriedSum(1)(2, 3)); // 6

Supports partial application, meaning you can call it with some or all arguments at different times.

šŸ”¹ 4. Partial Application vs. Currying

Partial Application

  • Pre-fills some arguments, returning a function with fewer parameters.
function partial(fn, ...presetArgs) { return function(...laterArgs) { return fn(...presetArgs, ...laterArgs); }; } const multiply = (a, b, c) => a * b * c; const double = partial(multiply, 2); console.log(double(3, 4)); // 24

Partial application locks some arguments, currying breaks them into single-argument calls.

šŸ”¹ 5. Use Cases of Currying

1. Custom Configuration

const log = (level) => (message) => console.log(`[${level}] ${message}`); const errorLog = log("ERROR"); const warningLog = log("WARNING"); errorLog("Something went wrong!"); // [ERROR] Something went wrong! warningLog("This is a warning."); // [WARNING] This is a warning.

Creates reusable logging functions.

2. Filtering Data

const filterBy = (property) => (value) => (array) => array.filter(item => item[property] === value); const filterByCategory = filterBy("category"); const electronicsFilter = filterByCategory("electronics"); const products = [ { name: "Laptop", category: "electronics" }, { name: "Shoes", category: "fashion" } ]; console.log(electronicsFilter(products)); // [{ name: "Laptop", category: "electronics" }]

Reusable and modular filtering function.

šŸ”¹ 6. Conclusion

Currying transforms functions to handle arguments one at a time.
Useful for functional programming, reusability, and clean code.
Can be implemented manually or using a curry() helper function.

šŸš€ Start using currying to write more modular and reusable code!

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close