The difference between partial application and currying

Partial application and currying are often confused, but are in fact quite different.

Let's use lodash to illustrate the difference between partial application and currying.

function greetPerson(greeting, name) {  
  return greeting + ' ' + name; 
};

var hi = _.partial(greetPerson, 'hi');  

What happened here? Well, by calling _.partial; we have created a new function hi that when called will use the 2nd-n arguments passed in _.partial as pre-populated arguments.

hi('Joe'); // 'Hi Joe'  

Using _.partial, you can also pass in additional arguments, (Joe) to the reurned hi function and they will be appended to the saved arguments before the call.

Now let's look at the same function implemented with _.curry.

function greetPerson(greeting, name) {  
  return greeting + ' ' + name; 
};

var greeter = _.curry(greetPerson);  

Using _.curry, we have created a new greeter function. greeter will not call it's inner function until all the arguments have been satisifed, either all at once or n at a time.

greeter('Hello', 'Joe') // Hello Joe.

var hi = greeter('Hi'); // Args not satisfied.  
hi('Joe'); // Hi Joe.  

Looking at the first example, we can see that the inner function is executed since we have satisfied all the arguments.

The second example shows that the inner function will not be invoked until all arguments are satisfied, and in fact will return another function until they are all provided.

How is _.curry useful? One case can be when you don't know how much information you will have and want to keep prompting until you get all the info you need.

This is a quick look at _.partial v.s. _.curry and the differences between them.