The goal of this post is to be able to modify any JavaScript function so that it can be partially applied. This process is also known as **currying**. An example of currying could be a simple add:

functionadd (x, y, z) {returnx + y + z }

We would like to be able to do the following:

// yields a function that needs 2 more argumentsletpartialAdd = add(1)// yields 6letresult = partialAdd(2, 3)

It means that we can call the function however we want (fully or partially). If the function is partially called, it returns a function that expects the remaining arguments:

```
add(1, 2, 3) // yields 6
add(1)(2)(3) // yields 6
```

## Starting point

We will start by looking at a function found online here.

**function** schonfinkelize(fn) {
**var** slice = Array.prototype.slice,
stored_args = slice.call(arguments, 1);
**return** **function** () {
**var** new_args = slice.call(arguments),
args = stored_args.concat(new_args);
**return** fn.apply(null, args);
};
}

This is an interesting function and a good attempt at making functions partially in JavaScript. The basic idea of this function is that it takes the original arguments and returns a function that waits for the remaining arguments.

You can use it this way:

// yields a functionletaddSecondHalf = schonfinkelize(add, 1)// yields 6 addSecondHalf(2, 3)

The issue with this function is that we can’t do something like:

```
// Doesn't work
schonfinkelize(add, 1)(2)(3)
```

The reason it doesn’t work is because schonfinkelize is not called recursively by looking if we have enough arguments to call the function. In order to make the function work, we would have to do something like the following:

```
// yields 6
schonfinkelize(schonfinkelize(add, 1)(2))(3)
```

## Solving it recursively

The first thing we need to ask ourselves is how we know if we have enough arguments to call the function. It turns out JavaScript has this built-in. Every function in JavaScript has the attribute length, which returns the number of arguments a function is expecting. So in our case:

```
// yields 3
add.length
```

With this information, we know that the base case of our recursive call will be called if we have enough arguments to call the function. With this in mind, let’s look at the first solution:

functionpartialApply(fn) {letslice = Array.prototype.sliceletcurr_args = slice.call(arguments, 1)if(fn.length > curr_args.length) {returnfunction() {letnew_args = slice.call(arguments)letargs = curr_args.concat(new_args)returnpartialApply.apply(null, [fn].concat(args)) } }returnfn.apply(null, curr_args) }

So what does this function do exactly? We recursively call partialApply as long as we don’t have enough arguments. This ensures that we will wait until enough arguments are passed before computing any result. The base case is reached when enough arguments have been passed through one or many calls. Since we return in the recursive if statement, we can simply return the result at the end (that satisfies the base case).

This function allows us to do something like:

```
partialApply(add, 1)(1)(2) // yields 4
partialApply(add)(1)(1)(2) // yields 4
partialApply(add)(1, 1)(2) // yields 4
partialApply(add, 1, 1, 2) // yields 4
```

## Using the function prototype

This is great, but it feels awkward to have to pass the function as a parameter. It would be great if we could curry directly on the function:

```
add.partialApply(1)(1)(2) // yields 4
add.partialApply(1, 1)(2) // yields 4
add.partialApply(1, 1, 2) // yields 4
```

This solution is very similar to the one above but it will be bound to the function prototype. Here is the final implementation of currying properly in JavaScript:

Function.prototype.partialApply =function() {letfn = thisletslice = Array.prototype.sliceletcurr_args = slice.call(arguments)if(fn.length > curr_args.length) {returnfunction() {letnew_args = slice.call(arguments)letargs = curr_args.concat(new_args)returnfn.partialApply.apply(fn, args) } }returnfn.apply(null, curr_args) }

In this function, we utilize the fact the JavaScript functions are first-class objects. This means that any function is an instance of the Function prototype and therefore will have access to our function partialApply. The `this`

keyword in JavaScript refers to the instance calling that function.

The base case remains unchanged (it simply computes according to the original function definition). It gets interesting when we look at the recursive call because we need to ensure that context is properly passed.

**return** fn.partialApply.apply(fn, args)

We can’t simply call partialApply. We need to apply it because we only have access to the arguments in the form of an array. The other interesting part of this line is that we have to pass in fn as the first argument, since it defines what `this`

(the context) will be in the function call. If we simply did:

**return** fn.partialApply.apply(**null**, args)

It would yield the error (“Cannot read property ‘apply’ of undefined”), since we would not bind `this`

to the right function in the recursive call.

## Usage

So now, how can I make it so all my functions can be partially applied? You can simply call `.partialApply()`

on any function declaration you make. It looks like this.

letadd =function(x, y, z) {returnx + y + z }.partialApply()leta = add(1, 2, 3) // yields 6letb = add(1)(2)(3) // yields 6letc = add(1)(2, 3) // yields 6

So this is how you curry your functions in JavaScript!

*This article was originally published on Medium.*