🟑 MediumFunctionsπŸ“– Theory Question

What is partial application and how does it differ from currying?

πŸ’‘

Hint

Partial application = pre-fill some args, return function waiting for the rest; currying = always one arg at a time

Full Answer

Both techniques create specialized functions from general ones β€” but differ in how arguments are collected.

// Partial Application β€” pre-fill SOME args
function partial(fn, ...presetArgs) {
  return function(...laterArgs) {
    return fn(...presetArgs, ...laterArgs);
  };
}

const add = (a, b, c) => a + b + c;
const add10 = partial(add, 10);         // pre-fill first arg
const add10and20 = partial(add, 10, 20); // pre-fill two args

add10(5, 3);     // 18 β€” takes remaining 2 args AT ONCE
add10and20(7);   // 37 β€” takes remaining 1 arg

// Currying β€” always ONE arg at a time
const curriedAdd = a => b => c => a + b + c;
curriedAdd(1)(2)(3); // 6 β€” strictly one at a time

// Practical partial application with bind()
function greet(greeting, punct, name) {
  return `${greeting}, ${name}${punct}`;
}
const hello = greet.bind(null, 'Hello', '!'); // partial via bind
hello('Alice'); // 'Hello, Alice!'
hello('Bob');   // 'Hello, Bob!'

Summary:

  • Currying: f(a, b, c) β†’ f(a)(b)(c) β€” each call takes exactly ONE argument
  • Partial application: f(a, b, c) β†’ f(a, b)(c) β€” pre-fill any number of args
πŸ’‘ In practice, curried functions support partial application too (you can call with multiple args and they accumulate). The distinction is mostly theoretical.

More Functions Questions

🟒 EasyWhat is the difference between call, apply, and bind?β†’πŸŸ’ EasyHow do arrow functions differ from regular functions?β†’πŸŸ’ EasyWhat is a pure function and why does it matter?β†’πŸŸ’ EasyWhat are Higher-Order Functions (HOF)?β†’

Practice this in a timed sprint β†’

5 free questions, no signup required

⚑ Start Sprint