This course has been updated! We now recommend you take the Functional-Light JavaScript, v3 course.
Transcript from the "Exercise 3 Solution" Lesson
[00:00:00]
>> [MUSIC]
[00:00:03]
>> Kyle Simpson: First off, the main goal of this is to be able to handle an arbitrary number of arguments. So I'm going to take those off, those named ones, and in their place I'm going to take advantage of that es6 spread.Kyle Simpson In this case it's called the rest operator, I like to call it the gather operator by the way.
[00:00:23] But ...args, that's my gather operator, so I'm gonna gather everything together into the array args. Now, all recursive functions, the main defining characteristic is that they have a base case, some way to know that it's time to stop so they don't run forever. And they have a recursive call.
[00:00:45] So let's deal with the base case. If the length of args is less than or equal to two. That means I have all that I need to make a multiplication between two elements. Using the multiplication operator. So I just am going to take args of zero and args of one, multiply them together, and return that result.
[00:01:08] So I'm going to simply say return args of zero times args of one. I was asked in chat over the work break what about other of those trivial base cases? Like if you pass no arguments or only one. We're not worrying about those trivial base cases here. Obviously those are easy to deal with.
[00:01:29]
>> Kyle Simpson: Okay, so that's the path we're returning if we have the base case. Otherwise, what we want to do is return the args of zero times the recursive call, the recursive for the rest of the list. So I'm gonna simple say return args of zero times. And the way I call mult with the rest of the list is to say mult.
[00:01:54] And I'm gonna use... to spread out an array. And the array that I'm gonna spread out is not just args, but it's actually args with the first value sliced off.
>> Kyle Simpson: Okay. Something interesting to note here by the way. This will be something that we look at a little bit more later.
[00:02:17] But you know how we talked about immutability? We already start to see some principals of that happening in the JavaScript methods that are built into language. Slice, for example is a prototypal method available to all arrays, all real arrays. But what does it do? Does it mutate the existing array?
[00:02:34] Actually it creates a whole new array. So slice is one of those immutable methods. Because it takes the method as it's context but it doesn't change that array, that context, it produces a whole new one with the subset of what you asked for. So slice is one of those immutable methods.
[00:02:56] There are other methods like for example splice that's with a p in there. Splice actually changes the array in place, doesn't create a new one. So we can see even built into the language we see differences between using an immutable principle and returning a new value versus changing something in place.
[00:03:17] So slice is one of those cases. Questions about exercise 3? Yes?
>> Speaker 2: Is this now tail call optimized because you do mult at the end?
>> Kyle Simpson: Perfect question, thank you for asking that. No. This is not a proper tail call, great question, a good segway back to what we were talking about.
[00:03:38] Before the work break. The reason why this is not a proper tail call is because after mult is called, another multiplication has to occur. It's only a proper tail call if the very last thing to happen is the function call, and then its return value is immediately returned back.
[00:03:59] Okay. Can this be rewritten in a proper tail call form? Yes. Am I going to do that now? No. If you wanna know more about that technique how to rewrite in the proper tail call form, check out the AS6 book. I think it's chapter 7, if I remember correctly.
[00:04:15] Okay, so recursion nice graceful way of expressing a problem kinda getting some of the unnecessary details out of the way. ES6 helps us out a bit with the rest and spread operator.