Check out a free preview of the full Functional-Lite JavaScript course:
The "Exercise 4 Solution Part 1" Lesson is part of the full, Functional-Lite JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

Kyle begins walking through the solution to exercise 4. First he creates an add() function that will add two numbers. He then creates an add2() function that takes two functions instead of two numbers, calls each function, and sends those values to the add() function.

Get Unlimited Access Now

Transcript from the "Exercise 4 Solution Part 1" Lesson

[00:00:00]
>> [MUSIC]

[00:00:04]
>> Kyle Simpson: So to start out with, as the read me says, what we're going to have you do is write two functions, I don't care, just call them foo and bar for now, and each one of those functions should return a different number. So have one of them return 10 and one of them return 42, I don't care, pick your favorite numbers.

[00:00:21] But literally have it just return a number. Okay? Your functions probably look something roughly like that. Okay? Now, there's something interesting in functional programming, there's something interesting about the nature of having a value wrapped in a function. Kind of like I said earlier that there's an instinct to say well, if given the option, I try to stuff.

[00:00:46] I try to stuff values into a list because I know list can. I can do certain sorts of operations on list that are quite handy. In the same way, you also sometimes will say if I can wrap a value in a function, and I know if calling that function will give me the value, that starts to produce patterns of more composibility in our programmes.

[00:01:08] It's easier to compose two functions together that return numbers, than it is to simply compose the two numbers. That's what we're getting at. Okay. So this was one of those pull out of thin air things. I didn't tell you to just write the two values 42 and 10, I told you to wrap them in functions that produce those values.

[00:01:24] Won't be clear why yet, but as we go along, maybe you'll see a little bit more of a reason why. Okay? So, item number two in the readme. What we're going to do, is take, we're going to write a function, which we'll call add. And add needs to take take two numbers, and return the result.

[00:01:42] We'll just leave our function bar and function foo alone for a little bit. Add needs to take in two numbers and add them two together. So let me give you your fifteen seconds to write that. I'm going to take away points from you if you pulled out underscore like there's some underscore function I can do for this.

[00:02:02] We're writing our own code here, we're learning new stuff, okay? That was just a little dig, I apologize. I'm sure there probably is an add function in underscore. All right, add x and y. Is what I'm going to name them. How does add work? Pretty straightforward, x plus y.

[00:02:22] Okay. Not rocket science here. This step progression that I am going through is intentional. It's intentional to lead you two places where you start to get some of those instincts. So don't jump too quickly beyond what I'm doing and think that it's too remedial. This is a very carefully, intentionally designed exercise.

[00:02:47]
>> Kyle Simpson: All right, the next thing we want to do. I could observe that an add that takes two numbers is cool. But an add that takes two functions would be even cooler. So I'm going to make an add2. And that function needs to take in two functions, and it needs to cull both of those to get their values out and then return the sum.

[00:03:13] Okay? So your function might look something a little like this. I'm going to call it fn1 and fn2. And, it's going to use the add function.
>> Kyle Simpson: I'm going to call fn1. I'm going to call fn2, pass those to the add function.
>> Kyle Simpson: Again, it's not totally clear to you why it decided to make add2 use add rather than just do its own addition.

[00:03:48] But in functional programming, you see this all the time. Everything, every abstraction that you define is a combination, it's a composition of some lower level abstraction. We're always looking for that turtles all the way down sort of path to defining a utilities. So why repeat the plus operator when I already have a function that can compute them?

[00:04:14]
>> Kyle Simpson: Now, I know this stuff seems totally remedial. It's about to get, here in a couple of steps, about to get a little more challenging, so just hang on with us. So add2 passes those values in, just like we did, now let's move on to item number four.

[00:04:29] There's two functions that we defined that had. Well, before we do that, let's just verify that we know how we can use this. How do we use the add2 function? What would the call set look like?
>> Kyle Simpson: add2, what am I going to pass into it?
>> Speaker 2: [INAUDIBLE]

[00:04:46]
>> Kyle Simpson: Foo and bar. Now what am I going to get back out in this case?
>> Kyle Simpson: 52. Okay, so everybody make sure we're understanding that. Now, let's take the foo and bar functions, and replace them with a new function. Now I don't care what you call this function, I'm going to still call mine foo, but if you have a different better name that you like, the name doesn't matter here.

[00:05:11] But here's what this function needs to do. This function needs to take a value, a single value, and it needs to return back a function that when that function is called will give you the value on it. So take a moment to figure out how you would define function 2, all right?

[00:05:35] So your function foo might take a value like x. What does it need to do? It needs to return a function that doesn't need to receive any parameters. Now what does that function need to do? Return x.
>> Kyle Simpson: There's our closure again, right? We're producing a function that will remember that x value, and every time we call it it'll give us that x value back.

[00:06:06] Here's a little hint preview towards the asynch workshop. That's called a thunk, and we'll talk about thunks, but when we get to thunks, I want you to remember back to this idea of a function wrapped around a value, okay? All right, so now we don't have a foo and a bar anymore like that, that we can just pass in.

[00:06:28] What do we need to do to call add to now if want to still produce our 52 result, what do we need to do? Foo 10, and Foo 42. Everybody see that? I'm actually calling those functions, and producing functions that get past them.