JavaScript
is filled with cool features, and one of the most mind-blowing concepts is currying. Donβt worry if it sounds fancyβby the end of this blog, youβll not only understand currying but also be able to impress your developer buddies! π»π₯
π§ What is Currying?
Currying is a way to transform a function with multiple arguments into a sequence of functions, each taking a single argument. Itβs like serving a meal one dish at a time instead of all at once! π½οΈ
Read this once again !!
π° Why Use Currying?
- Reusability: You can create specialized versions of functions.
- Readability: Makes your code look cleaner and more modular.
- Functional Programming Vibes: Functional programming fans π currying.
π Examples
Letβs jump straight into some examples:
Basic Example
// Normal function
function add(a, b) {
return a + b;
}
console.log(add(2, 3)); // 5
// Curried version
function curriedAdd(a) {
return function (b) {
return a + b;
};
}
console.log(curriedAdd(2)(3)); // 5
π Boom! Now you can call curriedAdd(2)
and get a reusable function to add 2 to anything!
const add2 = curriedAdd(2);
console.log(add2(5)); // 7
console.log(add2(10)); // 12
Currying with Arrow Functions πΉ
Who doesnβt love short, clean arrow functions?
const multiply = (a) => (b) => a * b;
console.log(multiply(3)(4)); // 12
// Make a multiplier of 3
const triple = multiply(3);
console.log(triple(5)); // 15
console.log(triple(10)); // 30
Real-World Example π
Imagine a filter function for a shopping app:
const filterByCategory = (category) => (product) => product.category === category;
const products = [
{ name: "Shoes", category: "Fashion" },
{ name: "Laptop", category: "Electronics" },
{ name: "T-shirt", category: "Fashion" },
];
const isFashion = filterByCategory("Fashion");
console.log(products.filter(isFashion));
// Output: [ { name: "Shoes", category: "Fashion" }, { name: "T-shirt", category: "Fashion" } ]
Breaking Down Complex Problems π§©
Currying makes it easy to break problems into smaller, manageable parts.
const greet = (greeting) => (name) => `${greeting}, ${name}!`;
const sayHello = greet("Hello");
console.log(sayHello("Alice")); // Hello, Alice!
console.log(sayHello("Bob")); // Hello, Bob!
const sayGoodMorning = greet("Good Morning");
console.log(sayGoodMorning("Charlie")); // Good Morning, Charlie!
π Advanced Currying with Utility Functions
Donβt want to manually curry functions? Letβs write a helper function:
const curry = (fn) => (...args) =>
args.length >= fn.length
? fn(...args)
: curry(fn.bind(null, ...args));
// Example:
const sum = (a, b, c) => 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
π€ Tricky Question Time!
Here's a fun one to tease your brain π§ :
const add = (a) => (b) => b ? add(a + b) : a;
console.log(add(1)(2)(3)(4)()); // β
What do you think the output is? Share your thoughts below! π
π Wrapping Up
Currying is a powerful technique that simplifies your code, makes it reusable, and adds a touch of elegance. So, start currying your functions and bring the spice of functional programming to your JavaScript code! πΆοΈ
Happy coding! π»β¨
Top comments (0)