This course has been updated! We now recommend you take the JavaScript: From First Steps to Professional course.

Check out a free preview of the full JavaScript: From Fundamentals to Functional JS, v2 course:
The "Currying and Composing Exercises" Lesson is part of the full, JavaScript: From Fundamentals to Functional JS, v2 course featured in this preview video. Here's what you'd learn in this lesson:

In these exercises, students work on implementing currying and compose functions.

Get Unlimited Access Now

Transcript from the "Currying and Composing Exercises" Lesson

>> So, what we have left is our currying exercise and our composing exercise. So here's our little reference for how this works. So, Let's create a function called curry. Const curry equals some function, takes a function, that's what it takes, and then, We need to Get it to work with this behaviour, but with just two of them, we don't need to do this.

[00:00:50] Okay? Just because for this exercise I want to simplify it for everyone's Benefit really. Okay, so we have a function and we need to return a function, we know that, right? Okay. So when we call it, we return a function, we call it a second time, we wanna save that value.

[00:01:29] So we are going to need to be able to take an argument there for the first one. And then, we're gonna return another function, and we need to have a second argument. There, and then once we get to that second argument, then we can call our function with both of those arguments.

[00:02:02] Arg2, we probably wanna return it. All right, so shall we run this? So the first thing we do is we run curry with our function name, okay? So our function name is abc, and we return this function. So curried now holds the value of this function body here, when we call it, so we can, so function is our abc, then arg is going to be one.

[00:02:48] And then we return this function We call it again, we can do that because it returns a function, right? Remember when we tried it before it didn't work because it returned undefined? Now it works because we are actually returning a function, and we can do this little bit where we do it twice in a row.

[00:03:09] And then we pass two, And because of scope, we retain access to function arg, and of course arg2. And so, we're two levels deep recalling our function with arg and arg2 and returning that value. And that's the basis of implementing a curry function. Obviously, it gets a little bit more complicated when you have varying numbers of inputs, right?

[00:03:45] Longer arguments, right? For this one, we're just assuming there's two. But as you can imagine, if you don't know how many there are, there's some looping involved, probably some recursion to get that to work. But that's the basic value, so we're just returning nested functions. Cool? Any questions about this?

[00:04:16] No? Okay, and the next one is compose. Let's get our little example Just so we have something to run. Okay. Get our word wrap, perfect. Okay, so we have our composed function, and for this case, we're gonna do two as well. So we're gonna say const compose equals a function, and it's gonna take two arguments.

[00:05:03] Okay? And the first thing that needs to happen, Is that we need to return a function so that we can call it later. So let's set that up, let's return a function. Here, and it needs to be able to take some arguments here. Let's just say it'll take one for now, and then, We wanna first call exclaim, so that's the second one so we're gonna call, let's call this function one and function two.

[00:05:54] So let's call function two with our arg, and we're gonna return that to a result. And then, we're going to pass that result into the original function, the first function that we passed in, and we're just going to return whatever that returns. And there you have it, so and this isn't taking a consideration of variable number of functions passed or a variable number of arguments passed.

[00:06:32] Similarly with curry, you'd have to probably keep track of how many functions there are. And then for args, here, this would be a problem with an error function. Because we probably would wanna retain access to the argument's keyword, so we would need to use a regular function for that.

[00:06:53] So those are some considerations for a more robust solution to compose. But again, so here we're returning a function With an argument passed in the second one, and this one we're returning nested arrays until we get everything that we need to finally call our function. We retain access because parameters are just like local scope variables, and yeah.

[00:07:24] Any questions about those solutions?
>> So if we wanted to basically generalize either of these, we can get into like storing arguments or functions in an array in the arguments object, and then slicing it off one by one and doing that like recursively? That's basically how you would kinda go about it?

>> Yeah, well, I think for This one, if this wasn't an error function and we had access to the arguments array
>> Yeah.
>> You could really just do function apply.
>> Okay.
>> These arguments That would make it general. But for this, for calling each of those functions with the result of that function would require some kind of loop.

>> Yeah.
>> Whether it's recursive loop, or a while loop, or something like that, or just a regular loop, that's what you would have to do.
>> Okay.
>> Yeah.