This course has been updated! We now recommend you take the Functional-Light JavaScript, v3 course.
Transcript from the "Exercise 4 Solution Part 5" Lesson
[00:00:00]
>> [MUSIC]
[00:00:04]
>> Kyle: Next step in our readme. let's define a slightly, let's put a few more values in here. So let me come up with 15 and two and 98, and seven, okay. I just added some extra values. I've now lost my ability to know what that running total will be but we'll cheat and love JavaScript tell us what the total ought to be.
[00:00:32] Okay, so if that was our list, we might want to exclude some items from the list. So the readme suggests. That we want to filter the array and exclude all of the odd numbers. So how are we gonna exclude the odd numbers from our list if we start out without a list?
[00:00:52] Let's make this a little easier. Let's start this out with this array here.
>> Kyle: And then let's perform the operations, like we're doing. So right now, we have arr = arr.map with foo.
>> Kyle: So all I need to do is split those out so we're dealing with separate lines now.
[00:01:19] All right, so let's add an additional step in here. Which after before we are mapping the functions. We wanna look at the values themselves and filter out any of those values that are odd. So how do we do that?
>> Speaker 2: Write a function that.
>> Kyle: What utility am I gonna reach for if I need to do an exclusion?
[00:01:42]
>> Speaker 3: Reach for filter.
>> Kyle: I'm gonna reach for filter, right? So I need to do an exclusion I'm gonna call arr.filter. And I'm gonna need a predicate, a function to pass in that returns true or false. Remember before in our slides we passed in and it is odd.
[00:02:00] And it returned true if the value is on. Is that the one that we wanna use?
>> Speaker 2: We might use zero instead of one.
>> Kyle: We actually want the negation of the is odd don't we? We want an isEven. So we could write our own is even. But another functional programming technique is to observe.
[00:02:19] That is even is just a composition of the negation with the isOdd function call. So let's assume I had an isOdd. That's all I had.
>> Kyle: Remember, the way it works is to mod by two and say, that it's gotta be equal to one. So let's say, I only have an isOdd, could I make an isEven?
[00:02:43] Yeah, I could also from a functional programming perspective and I had functional utilities that did composition this would end up being really easy. Because you could make a function that just negates a Boolean. But if I wanted to make an isEven.
>> Kyle: One way of making is to actually do the modules calculation.
[00:03:04] The other way of doing it is to call a negation of is off.
>> Kyle: Does everybody see that? That's doing it manually.
>> Kyle: Or I can look at that pattern and say, you know what, I do that an awful lot. I negate my Boolean utilities an awful lot, so I'm gonna make myself a compositional pattern that automatically lets me take in a function, call it with a value, and negate its return response.
[00:03:35] So let's just pretend that we have one like that. Compose neg is what we'd call it. I could say, isEven is equal to compose neg with is odd.
>> Kyle: So that would produce me a new function that is called is odd and negated it's result before returning it back to me.
[00:03:59] You see how that could end up being a useful utility in my functional programming library. But only if that was a pattern I ended up doing a lot. So that's what functional programmers have done. They've done enough functional programming and they've run across enough of these compositional patterns that they've said here's the common ones that we're gonna provide to you and they gave them a bunch of fancy names.
[00:04:20] All I'm suggesting to you is that you can start with nothing and build yourself up, a little set of these things that you are regularly using. That's basically what I've done. That's what I've made use of function programming. That's a way of getting at the same principles of functional programming.
[00:04:38] Instead of coming at it from the other direction and having to learn all the academic stuff. Okay? And there are tons of really great functional programming libraries out there, RAMDA, Bacon JS, several other ones out there. So if you do wanna go the route of just using someone's predefined library and you wanna take on the task of learning some of those more sophisticated terminology and techniques and what patterns, functional program are to use.
[00:05:05] That's a great next step from what I'm teaching. But I hope that I'm teaching you that there is a way to get some of the benefit here without having to go through all of that effort. That's personally how I've woven functional programming techniques in. Alright, so let's just assume we're gonna have an isEven.
[00:05:25] We're just passing an isEven. However you end up defining it, we wanna filter. So I can end up doing a reassignment here, as I'm doing, or this can all just be a set of chains. So arr is the chain of those two commands.
>> Kyle: So do we see how that works?
[00:05:50] Let's just double-check to make sure I didn't mess something up. I need, I won't be able to run that because I haven't find as you can see, so let's make one just for testing purposes.
>> Kyle: Uh-oh.
>> Speaker 3: [INAUDIBLE].
>> Kyle: I did, didn't I? Sorry.
>> Kyle: All right, so that's telling us that it's 208.
[00:06:37] Let's just sanity check that. We wanna add up only the even, so 52, 108, 110.
>> Kyle: Yeah 208. There we go. Okay, so 208's our expected end result. All right, the last step in this exercise and then we'll wrap up.
>> Kyle: We've already done the last step. The item that I wanted to call your attention to is number nine, now we can try to stretch our brains into thinking, how would we write tests for all this composition?
[00:07:18] We're visually looking at it and saying, I can tell you that the end result is supposed to be 208. How would you write tests for this?
>> Kyle: That's not a question I'm expecting a concrete answer to, it's a rhetorical for you to ponder as we go forward. Because functional programming does end up creating some very powerful expressions of our programming, but I think you will find as I have found that when you use functional programming techniques and you try to put those into your traditional testing frameworks, you might find some incompatibilities.
[00:07:54]
>> Kyle: So pondering how you are going to test the end result of these different things. Are you going to test only the end result or are you gonna test each layer of all this composition? Those are questions that you should ponder.