Create Reusable Functions with Partial Application in JavaScript

InstructorKyle Shevlin

Share this video with your friends

Send Tweet

This lesson teaches you how arguments passed to a curried function allow us to store data in closure to be reused in our programs and applications. Since each argument, except for the final one, returns a new function, we can easily create reusable functions by supplying some of these arguments beforehand, and sharing these partially applied functions with other parts of our codebase. In this lesson, we'll create a curried function to fetch requests from an API that uses partial application to create reusable functionality.

LEE SUK JAE
~ 5 years ago

i think you missed fat arrow in "Transcript" at every 'cb' .

const getFromAPI = baseURL => endpoint => cb

should be

const getFromAPI = baseURL => endpoint => cb =>

Xu Lishuai
~ 5 years ago

Curring: const add = x => y => z => x + y + z; Partial Application: const add = x => (y, z) => x + y + z; right?

Kyle Shevlininstructor
~ 5 years ago

@Xu, no, not really. Partial application means that you have supplied values for the arguments (but not so many that you've triggered the final argument and the evaluation of the function).

// Your curried add function for three values
const add = x => y => z => x + y + z

// Partially apply first value
const add10 = add(10) // returns y => z => 10 + y + z

// Partially apply the second value
const add25 = add10(15) // returns z => 10 + 15 + z

// By passing in the final argument, the last function is called and we get the result
const fortyFive = add25(20) // returns 10 + 15 + 20 === 45