josh.code

12 Days of Posts: Day 2 – Partial application of a function

 : 253 words

This day’s post is a continuation of the last post. In the last post we covered that a function can return a function, but we did not cover any use cases. This post will discuss a very useful one.

We have an add function:

function add(x, y){
  return x + y;
}

We can partially apply one of the parameters and return a new function. To reinforce what this means we will look at an example.

function partiallyApply(param, func){
  return function(nextParam){
    return func(nextParam, param);
  }
}

var addOne = partiallyApply(1, add);

The variable addOne takes the add function and partially applies one of the parameters (y is now always 1). When we execute partiallyApply we bind the first parameter and return a new function which allows us to bind the other parameter later. This means we can do this:

addOne(5);
//returns 6
addOne(10);
//returns 11
var addTen = partiallyApply(10, add);
addTen(5);
//returns 15

What good is this? Well we essentially are passing in parameters over two separate points in time. I had a problem where I needed an index for an item in a list and the event object. The issue was that I did not have these two things at the same time. The event object is only created in response to an event. I partially applied the index first which returned a function that would accept the event object. I have the code in the react_components.js file in my Where to eat repository.

The next post will continue the JavaScript theme.

comments powered by Disqus