Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 6 of 10 · Functions and Arrow Functions
Interview question

What is function currying and what are its benefits? Function currying क्या है और इसके क्या फायदे हैं?

Answer

Currying is a technique where a function that takes multiple arguments is transformed into a sequence of functions that each take a single argument. This enables partial application and function reuse.

// Regular function
function add(a, b, c) {
    return a + b + c;
}
console.log(add(1, 2, 3)); // 6

// Curried version - manual
function curriedAdd(a) {
    return function(b) {
        return function(c) {
            return a + b + c;
        };
    };
}
console.log(curriedAdd(1)(2)(3)); // 6

// Curried version - using arrow functions
const curriedAdd2 = a => b => c => a + b + c;
console.log(curriedAdd2(1)(2)(3)); // 6

// Partial application - a key benefit of currying
const add1 = curriedAdd2(1);
const add1And2 = add1(2);
const result = add1And2(3); // 6

// Or more concisely
const add5ThenAddMore = curriedAdd2(5);
console.log(add5ThenAddMore(3)(2)); // 10

// Real-world example: creating specialized functions
const multiply = (a) => (b) => a * b;
const double = multiply(2);
const triple = multiply(3);
const quadruple = multiply(4);

console.log(double(5)); // 10
console.log(triple(5)); // 15
console.log(quadruple(5)); // 20

// Curry helper function
function curry(fn) {
    return function curried(...args) {
        if (args.length >= fn.length) {
            return fn(...args);
        } else {
            return (...nextArgs) => curried(...args, ...nextArgs);
        }
    };
}

const curriedSum = curry((a, b, c) => a + b + c);
console.log(curriedSum(1)(2)(3)); // 6
console.log(curriedSum(1, 2)(3)); // 6 - can still use multiple args

Interview tip: Explain that currying is mainly used for partial application (creating specialized functions from general ones) and function composition. It improves code reusability and readability in functional programming.

Currying एक technique है जहाँ multiple arguments लेने वाले function को एक sequence of functions में transform किया जाता है जो हर एक single argument लेते हैं। Partial application enable करता है।

// Regular function
function add(a, b, c) {
    return a + b + c;
}
add(1, 2, 3); // 6

// Curried - manual
function curriedAdd(a) {
    return function(b) {
        return function(c) {
            return a + b + c;
        };
    };
}
curriedAdd(1)(2)(3); // 6

// Arrow functions से
const curry2 = a => b => c => a + b + c;
curry2(1)(2)(3); // 6

// Partial application - currying का key benefit
const add1 = curry2(1);
const add1And2 = add1(2);
add1And2(3); // 6

// Real-world: specialized functions बनाना
const multiply = (a) => (b) => a * b;
const double = multiply(2);
const triple = multiply(3);

console.log(double(5)); // 10
console.log(triple(5)); // 15

// Curry helper
function curry(fn) {
    return function curried(...args) {
        if (args.length >= fn.length) {
            return fn(...args);
        } else {
            return (...nextArgs) => curried(...args, ...nextArgs);
        }
    };
}

इंटरव्यू टिप: समझाएं currying mainly partial application के लिए use होता है (general functions से specialized बनाना) और function composition के लिए। Code reusability और readability को improve करता है।

Was this answer clear?