10 Partial application
Lesson goal Understand how partial application differs from currying, and learn to fix some of a function's arguments in advance to derive new functions from it.
Partial application is one of the techniques used in functional programming, in which some of a function's arguments are fixed in advance, producing a new function that waits for the remaining arguments.
Partial application is very similar to currying, but there are differences.
- Partial application fixes some of the arguments in one step: the result is a function that takes the remaining arguments in a single call and immediately computes the result. A curried function returns a new function after each call until it has received all of its arguments.
- The number of arguments passed to the function in partial application can be several, whereas in classic currying it is usually one.
// example of functions with partial application
// partialSumA :: Number -> (Number, Number) -> Number
const partialSumA = (a) => (b, c) => a + b + c
// partialSumB :: (Number, Number) -> Number -> Number
const partialSumB = (a, b) => (c) => a + b + c
// now let's call these functions
console.log ( partialSumA (1) (1, 1) ) // 3
console.log ( partialSumB (1, 1) (1) ) // 3
// Now let's write a function that applies
// partial application to any regular function
// partial :: ((a, b, c, ..., n) -> x) -> (a, b, c, ...) -> ((d, e, f, ..., n) -> x)
const partial = (fn) => {
// number of parameters of the original function
const lengthArgs = fn.length
// the returned function
const innerPartial = (...firstArgs) => {
// if the number of incoming arguments
// is greater than or equal to the number of fn's parameters,
// we can call it right away
if (firstArgs.length >= lengthArgs) {
return fn (...firstArgs)
}
// otherwise the second call
// runs the function no matter what
return (...secondArgs) => fn (...firstArgs, ...secondArgs)
}
return innerPartial
}
// we can use our function
// that applies partial application
// to any function
// sum :: (Number, Number, Number) -> Number
const sum = (a, b, c) => a + b + c
// we get a function whose arguments can be passed in two calls
const sumPartial = partial (sum)
// now we can call the returned function
console.log ( sumPartial (1) (1, 1) ) // 3
console.log ( sumPartial (1, 1) (1) ) // 3Key points
- Partial application fixes some of a function's arguments in advance, producing a new function that waits for the remaining arguments.
- The function produced by partial application takes the remaining arguments in a single call and immediately computes the result, whereas a curried function returns a new function after each call until it has received all of its arguments.
- In partial application several arguments can be passed to the function at once, whereas in classic currying it is usually one.
- The partial function from the lesson applies partial application to any regular function: if there are enough arguments, it calls the function right away; otherwise the second call runs it no matter what.
Exercises
Exercise 1
Use partial to fix two arguments of formatMoney(currency, digits, amount) at once — the currency and the number of decimal places — and get toUsd and toBtc. Then get toEur by fixing only the currency: pass it the number of decimal places and the amount in a single call. Print the amounts from the starter.
Starter code
// partial from the lesson, written more compactly
// partial :: ((a, b, c, ..., n) -> x) -> (a, b, c, ...) -> ((d, e, f, ..., n) -> x)
const partial = (fn) => (...firstArgs) =>
firstArgs.length >= fn.length
? fn(...firstArgs)
: (...secondArgs) => fn(...firstArgs, ...secondArgs)
// formatMoney :: (String, Number, Number) -> String
const formatMoney = (currency, digits, amount) => `${amount.toFixed(digits)} ${currency}`
// your code
console.log(toUsd(19.9))
console.log(toUsd(5))
console.log(toBtc(0.0015))
console.log(toEur(2, 7.5))Expected output
19.90 USD 5.00 USD 0.001500 BTC 7.50 EUR
Show solution
// partial from the lesson, written more compactly
// partial :: ((a, b, c, ..., n) -> x) -> (a, b, c, ...) -> ((d, e, f, ..., n) -> x)
const partial = (fn) => (...firstArgs) =>
firstArgs.length >= fn.length
? fn(...firstArgs)
: (...secondArgs) => fn(...firstArgs, ...secondArgs)
// formatMoney :: (String, Number, Number) -> String
const formatMoney = (currency, digits, amount) => `${amount.toFixed(digits)} ${currency}`
// the currency and the number of decimal places are fixed in one call
const toUsd = partial(formatMoney)('USD', 2)
const toBtc = partial(formatMoney)('BTC', 6)
// only the currency is fixed: the decimal places and the amount come in one call
const toEur = partial(formatMoney)('EUR')
console.log(toUsd(19.9))
console.log(toUsd(5))
console.log(toBtc(0.0015))
console.log(toEur(2, 7.5))Exercise 2
Compare partial application and currying on volume(length, width, height). Get volumePartial with partial and volumeCurried with curry from Ramda. Print the volume 2 × 3 × 4 computed first with volumePartial and then with volumeCurried: pass 2 in the first call and the remaining arguments the way each function allows. Then print volumePartial(2)(3) and the type of volumeCurried(2)(3), and explain why the results differ.
Starter code
// partial from the lesson, written more compactly
// partial :: ((a, b, c, ..., n) -> x) -> (a, b, c, ...) -> ((d, e, f, ..., n) -> x)
const partial = (fn) => (...firstArgs) =>
firstArgs.length >= fn.length
? fn(...firstArgs)
: (...secondArgs) => fn(...firstArgs, ...secondArgs)
// volume :: (Number, Number, Number) -> Number
const volume = (length, width, height) => length * width * height
// your codeExpected output
24 24 NaN function
Show solution
// partial from the lesson, written more compactly
// partial :: ((a, b, c, ..., n) -> x) -> (a, b, c, ...) -> ((d, e, f, ..., n) -> x)
const partial = (fn) => (...firstArgs) =>
firstArgs.length >= fn.length
? fn(...firstArgs)
: (...secondArgs) => fn(...firstArgs, ...secondArgs)
// volume :: (Number, Number, Number) -> Number
const volume = (length, width, height) => length * width * height
const volumePartial = partial(volume)
const volumeCurried = R.curry(volume)
// the remaining arguments in a single call
console.log(volumePartial(2)(3, 4))
// one argument per call
console.log(volumeCurried(2)(3)(4))
// the second call of partial runs volume no matter what:
// height = undefined, and 2 * 3 * undefined is NaN
console.log(volumePartial(2)(3))
// the curried function waits for all arguments and returns a new function for now
console.log(typeof volumeCurried(2)(3))Playground
Write and run JavaScript right here — Ramda (R) and Sanctuary (S) are available.