« Back to Index

Currying vs Partial Application

View original Gist on GitHub

Currying vs Partial Application.md

When we use Partial Application we always execute our function twice (regardless of the number of arguments).

When we use Currying we execute our function once for each argument.

Examples of Partial Application

// `partial` is a made up function
fn  = function (a, b, c) { return a + b + c }
foo = partial(fn, 'x', 'y')
foo('z') // => 'xyz'

In the next example we can see that it’s possible to change the arguments we partially apply:

// `partial` is a made up function
fn  = function (a, b, c) { return a + b + c }
foo = partial(fn, 'x')
foo('y', 'z') // => 'xyz'

Note how with Partial Application we make a function call twice (once when partially applying the arguments; and again when we fulfil the rest of the arguments). But remember: we can choose how many arguments we partially apply on the first call.

Examples of Currying

// `curry` is a made up function
fn  = function (a, b, c) { return a + b + c }
foo = curry(fn)
foo('x')('y')('z') // => 'xyz'
// `curry` is a made up function
fn  = function (a, b, c) { return a + b + c }
foo = curry(fn)
bar = foo('x')
bar('y')('z') // => 'xyz'

Note that a function that has been curried wont return the value of the function until each argument has been provided (i.e. satisfied). The arguments are manually partially applied one by one.

Functional Libraries