Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

    Write Concise Data Transformations in JavaScript with Partial Application

    Alan ShawAlan Shaw

    In this lesson you will learn how to use partial application to eliminate boilerplate and customize data processing for concise and readable code. Partial application makes it easy to apply functional programming techniques like map, filter, and reduce to transform data while leaving the originals intact.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson


    Instructor: 00:00 First, let's define a couple of functions we're going to be working on. + is a function that takes two arguments and returns their sum. < is a function that takes two arguments and returns a Boolean, indicating whether the first is smaller than the second.

    00:15 Now, imagine that at a certain point working on our important project, we come upon many, many Ys that we wish to transform in the same way, for example, by adding one to each of them. Rather than saying +1Y, +1Y over and over again, we write a function to do that for us.

    00:35 When we pass one to the function, we obtain two. When we pass three to the function, we obtain four. The function is called +1, because it takes a single argument, passes it to the plus function as the second argument, and passes a fixed number one to the plus function as its first argument.

    00:57 Quite often, the large data set that we wish to work on is contained in an array. Here is our first higher order function. It's called a higher order function, because it takes at least one function as an argument. Here's how we use it.

    01:11 We've mapped the +1 function over the incoming array to produce a new array whose values are the result of applying the function to each of the values in the original array. Now, let's do the same thing with our < function.

    01:27 The function takes a single argument, and tells us whether two is less than that argument. When we pass one to it, two is not less than one, and we obtain false. When we pass three to it, two is less than that, so we obtain true. Let's map this function over the input array.

    01:49 We obtain a new array, whose values are the same as those we obtained by applying the functions to the elements individually, but when we have a predicate and an array, the thing we're really interested in is producing a new array that contains only the elements for which the predicate return true.

    02:08 For that, we use our second higher order function filter. We construct a function that given an array will filter that array based upon the predicate. We see that the original one, since the predicate returned false is not present in the output array, and the original three for which the predicate returned true is.