Functional-Lite JavaScript

# Exercise 4 Solution Part 4

Check out a free preview of the full Functional-Lite JavaScript course:
The "Exercise 4 Solution Part 4" 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 spends a few minutes answering audience questions. He then continues the solution to exercise 4 by using the map() method so an array of individual values can be passed to addn() as an array of functions.

Get Unlimited Access Now

Transcript from the "Exercise 4 Solution Part 4" Lesson

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

[00:00:04]
>> Kyle Simpson: I apologize if it got a little muddy cuz I started my brain down a different way of solving this and I got you confused so hopefully we backed up the stack enough to show this. But the point that I want to get across is that the expression with a reduce, and we could've taken care of this part with a map but we chose to back that part out.

[00:00:24] I was going too far down to the complication tree. But a single reduce here replaces the complexities of recursion or iteration forms just simply knowing how to use that tool. It's like if you walk into a workshop and I've got a hammer in one hand and a mallet in the other.

[00:00:41] They both fundamentally can be used to swing things and drive some nail-looking object in, but you're gonna use a mallet differently than you're gonna use a hammer. You're gonna use them for different kinds of nails. So that's the distinction we're making here. We're not talking about the difference between a hammer and a screwdriver.

[00:00:58] We're talking about the difference between a hammer and a mallet. Recursion was an equally reasonable solution, but the way we ended up expressing it, might've twisted some brains. I hope that what I'm showing you here is being able to use functional programming oftentimes can give us also a graceful solution, even when recursion doesn't give us a graceful solution.

[00:01:22]
>> Kyle Simpson: Yes.
>> Speaker 2: Someone still wants you to kind of explain, he doesn't understand why the first value had to be removed?
>> Kyle Simpson: Okay, look, let's not do it, okay? If that helps. We're gonna do a reduction across the entire array, but we need a good suitable initial value.

[00:01:44] So, tell me what my good suitable initial value is going to be for arithmetic sum. It's gotta be zero, but I can't just pass in zero because everything else here is functions. So I have to pass in a function that will produce zero which is how I call foo(0).

[00:01:57] Okay, so if that works a little bit better with your brain. I feel weirder about that solution which is why I didn't lead with it. It feels weird to me to create a function only for the purposes of returning a zero.
>> Speaker 3: What I think is that some things get misaligned when you started applying map, that my brain sort of, okay, this is what we're doing now.

[00:02:20]
>> Kyle Simpson: Yeah. That's why I took the map back out cuz I realized I was going down the wrong route.
>> Speaker 3: Yeah.
>> Kyle Simpson: So that was my fault for confusing you.
>> Speaker 3: Didn't quite reset enough right now.
>> Kyle Simpson: Apologize for the confusion. Trying to pop the stat back to look at I could delete the file and start over.

[00:02:35] We're trying to pop the stat back to thinking about passing this in as a list of functions, rather than as a list of values.
>> Kyle Simpson: I just got myself one step ahead cuz our next step is going to be to generalize this where I can start out with a list of values.

[00:02:51] But I wasn't supposed to do the map so early. So, my fault. Apologies. Does this one, perhaps, make more sense? This expression of it where we're not doing a slice off the array but we do need an initial value. So here's how I produce my initial value. This'll work exactly the same way if I pop this out, drop this in, still gonna end up with 181.

[00:03:17] Fundamentally, we ended up with one extra function call there cuz we made a function call for a zero. When in reality, we don't even actually need that. We know the properties of our problem are that the initial value really could just start out as the beginning of the list.

[00:03:37]
>> Speaker 4: And more than one function call because it also does another iteration. You call foo and then there's an. Anyway, that alphabet function call-
>> Kyle Simpson: All right, I'll take your word for it rather than going down the rabbit trail. I've probably already confused everybody enough, so I apologize.

[00:03:52] [LAUGH]
>> Kyle Simpson: Anymore questions about the reduce? Did on the livestream, on the chat did we answer those questions about the difference in solution?
>> Speaker 5: They're saying good there.
>> Kyle Simpson: Okay.
>> Kyle Simpson: Some people are saying that their browser may not have the ES6 features turned on. So if your browser doesn't have the ability to do the dot, dot, dot, take that part out and just put a bracket around that list and it'll work the same way.

[00:04:35]
>> Kyle Simpson: Okay.
>> Kyle Simpson: All right, so now, we get to where I was gonna talk about map. I just did it too early. So now, if I start out with a list of functions, that's great. But if I start out with a list of values, and I want to pump those things into an add in, add in is expecting a list of functions, not a list of values.

[00:05:01] So if I started out with a list of 10, 42, 56, and 73, how do I make that into a list of functions?
>> Speaker 4: Map them to the foo?
>> Kyle Simpson: I map it with foo. [LAUGH] You see why foo is a suitable predicate for us? Look at the foo function.

[00:05:20] What does it do?
>> Kyle Simpson: It receives a value and it transforms that value by what? Wrapping it in a closed over function. That's a valid transformation for a value. It's to wrap it in a function that produces that volume. So that's why foo is a suitable transformation predicate for us.

[00:05:40] So, we would now have, at the end of that, an array of functions. So, that value, could be passed into add m. And that's where I was actually trying to go earlier.
>> Kyle Simpson: Okay? That's a suitable array. And because that's an array, we can either take that dot-dot-dot off or leave that one there if we want to and simply spread that array out.

[00:06:12] Either way.
>> Kyle Simpson: You're not always gonna be control, in control of all the signatures, of all the functions you work with. So part of the point that I'm trying to make here is that there are facilities available to us to make the adaptations between different signatures.