Sample Video Frame

Created by Zed A. Shaw Updated 2024-11-15 13:50:32
 

29: Partial Application

In the previous exercise when you created the closure you also accidentally used something called "partial application". Sometimes this is also called "currying" but others are subtle distinctions between the two which I won't get into in this exercise. Frankly JavaScript isn't strong enough of a functional programming language to even bother with this distinction.

In partial application you create a function that already has one half of the calculation ready to go, and then use that function with other functions to complete the calculation. In the previous exercise you did this by establishing the left side of an addition, and then when you called the function you gave it the right hand side to complete the calculation. This is useful because there are functions that only receive limited parameters, but you have to change how they work or add another parameter to complete a calculation. With partial application you simply create a new function that "eats" some of the parameters.

In this exercise I'm going to use partial application, combined with data transformations to create a useful function called the tee. This function works like a pipe in your plumbing which takes the water coming in and sends it out to two different directions. You could also call this a "fork" but that is confused with existing operations called fork in other contexts. In this case I'm using plumbing as my model for this function.

You can use this technique when you want to capture the result of a calculation in the middle of a chain of transformation calls. Partial application isn't limited to only this, but I find this is the most direct and useful way to use it.

Previous Lesson Next Lesson

Register for Learn JavaScript the Hard Way

Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.