Check out a free preview of the full Functional JavaScript First Steps course

The "Partial Application & Currying" Lesson is part of the full, Functional JavaScript First Steps course featured in this preview video. Here's what you'd learn in this lesson:

Anjana explains partially applied functions have some arguments "locked in" by using a closure. Currying is the process of refactoring a single function with multiple arguments to a series of partially applied functions.

Preview
Close

Transcript from the "Partial Application & Currying" Lesson

[00:00:00]
>> So what this lets us do, is it lets us take a function, which takes multiple arguments, and kind of lock in some of the values. Another way of saying this is it lets us partially apply a function. Apply in the functional programming world is kind of a word like call a function.

[00:00:22]
And so partial application is this notion of kind of remembering certain arguments and being able to then reuse functions more easily because I don't have to [COUGH] keep passing in the same value over and over again. Cool, cool, cool, cool. There's a related concept in functional programming called currying.

[00:00:49]
Currying is the process of taking a multi-argument function, a function that takes in multiple values as its input arguments and breaking it up in a sense, into a series of single argument functions, which successively, remember the outer scope, so that I can partially apply each of those multiple arguments to create again more reusable functions that I can use to create more complex programs out of simple functions.

[00:01:30]
Let's take a look at some code because me yapping is not gonna help this setting very well. All right. Here I've got my old greet function, if you remember from earlier, which takes in a greeting and a name, and returns out, hi Alan or Howdy, Alonzo. Now what I'm doing is I'm making a curried version of that greet function, which I'll call curly curry.

[00:01:54]
Greet [COUGH] which is a single argument function, it takes in a greeting, then it returns another single argument function, which takes in a name, and returns the same result as our original greet function did. So now, if I wanna greet lots of people with the same greeting, I don't have to keep passing in that greeting over and over and over again, I can create a new function like greetItalion sort of function, that I am going to create by calling curryGreet on the greeting that I want to use like Ciao.

[00:02:35]
And now I have a function that I can call on anybody's name. And because of closure, this inner function remembers that Ciao that greeting, and is going to be able to output the desired string even though I didn't have to keep passing Ciao in. And so similarly, we can see it again in this example, I can create like a tex and greeting bypassing Howdy to my current greet.

[00:03:02]
And now I can greet all kinds of people with my Howdy greeting without needing to repeat myself and keep passing in that same greeting argument over and over and over again. So these concepts kind of go hand in hand. Closure being, the concept through which, that inner function can remember, the outer function scope.

[00:03:27]
And currying, being the process of taking that multi-arg function, and breaking it down Into a series of single argument functions, which thanks to closure, allow me to partially apply that multi argument function and create a version that remembers the first argument I passed in and gives me a new function I can call on the second argument.

[00:03:51]
So these are a series of concepts that kind of go hand in hand to allow us to make more modular functions. They allow us to break down complex functions in a way into sort of little Lego blocks that we can then build up together into other complex functions.

[00:04:12]
So these are some of these concepts that when you get really into the functional programming world, they're gonna start to become second nature. But for now, they might feel a little bit strange. And it also might look a little bit strange because sometimes we'll have a lot of functions successively calling arguments one after the other, instead of passing in all those arguments to a single call to the function.

[00:04:36]
But if we can wrap our heads around this we'll be in great shape to go on and understand some of the more in-depth ways that we can work with closures and currying and partial application, in more intermediate or advanced functional programming courses, or tutorials, or things like that.

[00:04:53]
Why are we doing it like this instead of just using our original function and passing in the two arguments, the greeting and then the name each time that we wanna greet a new person? Is that the question?
>> Yeah, it was not really, so if you look the last few things here, the pair button, great text and agreed texts.

[00:05:18]
We'll repeat that again.
>> Yeah,
>> Instead of us just this let's do that things and we guess that said that the strings Alonzo prove it up. Howdy, comma, and then Alonzo
>> Sorry, is the question whyare we, why do we have such a simple function when we could just write the string?

[00:05:40]
>> Yeah
>> That's a good question. And in this case, this is a really, really simple example that's just for demonstration purposes, just to try to drive the idea through. But this function is not an immediately very useful function, I agree. [LAUGH] But as we'll see in the next exercise, there are a lot of cases where being able to split up arguments like this, and being able to lock in some of those arguments so that we have a function that we can reuse again and again.

[00:06:10]
Will be, useful because the computation is more complex than just smashing some strings together. So in this case, the functions here on these slides are really simple just to try to convey the point. And this is probably not the type of code that any of us need to write very often.

[00:06:28]
But hopefully in the next exercises, you'll get a sense for something that maybe is a little bit also contrived, but hopefully also [LAUGH] can convey why you'd wanna break down operations into really simple chunks, so that you can use them over and over again without having to repeat yourself a lot.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now