Functional Fundamentals: Currying & Partial Application

This is part four in my series on functional programming fundamentals in Javascript. You can find the previous articles here: part one, part two, part three.

Functional programming is full of non-obvious terminology, e.g. currying, but this is an important one to know. Currying is similar to but different from partial application, so we will compare these two today.

Currying

Currying is converting a function that takes many arguments into a function that takes a single argument, and returns a function accepting yet another single argument. This facilitates creating a chain of function calls in place of a single call with many arguments.

function sum(a, b) {  
    return a + b;
}
var addThese = sum.curry();  
addThese(2); // function waiting for b  
addThese(4); // 6  

Partial Application

Partial application is supplying some arguments to a function and receiving a function back that can accept additional arguments.

function sum(a, b) {  
    return a + b;
}
var addThese = sum.partialApply(3);  
addThese(); // 3  
addThese(5, 7); // 15  
addThese(9); // 24  

The key distinction between currying and partial application is that partially-applied functions always return a value, whereas curried functions return a function until the expected number of arguments have been received.