Functional-Light JavaScript, v3

List Operations Solution: add & constant

Kyle Simpson

Kyle Simpson

You Don't Know JS
Functional-Light JavaScript, v3

Check out a free preview of the full Functional-Light JavaScript, v3 course

The "List Operations Solution: add & constant" Lesson is part of the full, Functional-Light JavaScript, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Kyle live codes the first part of the exercise solution, two fixed number functions and two add functions.


Transcript from the "List Operations Solution: add & constant" Lesson

>> Kyle Simpson: All right, well, I trust that exercise was a bit challenging, but hopefully you got a sense of pulling in a variety of different things. Let's walk through it and make sure we catch all of the details. So item number one in the read me says that we need to define two different functions each of which return a value.

I'm gonna use numbers just to be very convenient. So I'll define a function five, that returns the number 5. And then I'll define a function nine, that returns the number 9, for no particular reason just because that's what I made up at this point, okay? That's what item number one in the read me says.

Item number two in the read me said to create an add function which takes two numbers. Well, that should be pretty straightforward. We take an add of an x and y, and it just adds those two together, and it gives you the value back. Now, how would we use add with our five and our nine?

Well, we would call that by saying add, and then we'd have to call the function five and call the function nine and pass in their values. What's that called when a function returns its result and that immediately becomes the input to another function? Composition, that's right, okay. Just making sure you're still at least partly awake, okay.

Item number three says we wanna create a function that can take functions and call them. So what did we say, we'll call that add to, that's what the readme says. We'll make an add2 function, and that takes an fn1 and an fn2. And it needs to use the add utility, so we're just gonna literally call it like we called it there, but with the fn1 and the fn2.

>> Kyle Simpson: That's sort of a higher order add utility, isn't it? It doesn't add numbers, it adds functions that hold numbers.
>> Kyle Simpson: Okay, item number four said take that five and that nine function that we defined in step one and let's replace those with another utility. A utility which I'm going to call constant, and I'll explain in just a moment why.

That utility needs to take in a value and return back a function, that when called, that function returns the initial value.
>> Kyle Simpson: So it's like taking a value and wrapping it up in a function closure. And I called this utility constant because that's exactly what that's called in functional programming libraries.

When you wanna make a function wrapped around a value, it's called constant. So how could I define a five? I could say five = constant(5) and nine = constant(9). And then if I wanted to use that add2 utility with both of those, then that would look like add2, and I'd call five and nine.

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