Advanced Static Types in TypeScript

This course explores the capabilities of TypeScript’s type system and shows how to use advanced static types in practice.

Watch the Course

Partially Applying Functions in JavaScript Using Underscore.js

The popular Underscore.js library recently enhanced its capabilities to deal with partially applied functions. Starting with version 1.6.0 which was released in February 2014, the _.partial function is now a lot more flexible and allows for some interesting use cases.

A Refresher: Partially Applied Functions

First, let's have a look at Wikipedia's definition of partially applied functions:

Partial function application refers to the process of fixing a number of arguments to a function, producing another function of smaller arity.

Or, put differently: The idea behind partial function application is to create a new function by specifying some (but not all) arguments of an existing function. That will return a new function accepting the remaining arguments. Once that new function is called and all parameters are provided, the original function is called with the complete parameter list.

The motivation behind partially applied functions lies in the realization that fixing some parameters of a function results in another useful function, as the next section shows.

Basic Partial Function Application in Underscore.js

Underscore defines a function named _.partial which accepts as a parameter a function and arbitrarily many of its arguments. The return value of _.partial is a new function that, when called, will pass both its own parameters and the initially provided arguments to the original function.

Here's how you can use Underscore's _.partial function to derive a function which increments a value from an existing function that adds two values:

var add = function(a, b) {
    return a + b;
};

var increment = _.partial(add, 1);

You can then call the increment function like this:

var six = increment(5); // 6

For the add function in the above example, the parameter a is fixed to the value 1. The first argument that's passed to increment will be passed to add as the b parameter. All following parameters will only be accessible through the special arguments variable because there are no more named arguments that the values could be bound to.

In general, the arguments provided to _.partial fix the parameters of the function in order of declaration, meaning from left to right. Depending on how much thought went into the order of the parameters of a function, a partial function application is more or less useful.

For the longest time, it was only possible to fix a particular argument if all arguments preceding it had been fixed as well. Skipping an argument was impossible — and then, Underscore.js 1.6.0 was released.

Partial Function Application Using Placeholders

With the enhanced _.partial method, you're not required to fix arguments strictly from left to right anymore. In fact, you can fix any combination of arguments by supplying a placeholder for the ones you'd like to fill in later:

var cube = _.partial(Math.pow, _, 3);

// Evaluates Math.pow(4, 3)
var x = cube(4); // 64

As you can see, the symbol used for setting a placeholder is the _. That is the global Underscore object itself, which just so happens to look like a blank line indicating a missing value.

Well played, Underscore!

Learn Node