This course has been updated! We now recommend you take the Complete Intro to Computer Science course.
Transcript from the "Reduce Function" Lesson
[00:00:00]
>> [MUSIC]
[00:00:04]
>> Brian Holt: Reduce is probably like the second most useful thing, and it's basically saying I have a list of numbers and I want to do something to reduce these down to one number. So really easy way to think about that is if you wanted to find the average, you would just reduce it down and give you back the average.
[00:00:20] So you would take a list of numbers, you would add them all together, and then you would divide by how many numbers you have, and you would get the average. That's what a reduce could do, so let's show you that. So let say I have a list a, b, c and let's look at this one first.
[00:00:40] This one's a little more interesting.
>> Brian Holt: So I have list right here and I'm calling list.reduce, I'm giving it a function which has an accumulator and a letter. An accumulator is just a fancy way of saying like my interim value. So I'm going to be constantly modifying this interim value.
[00:00:57] So in this particular case, I'm going to return an accumulator which is everything that I've gotten before plus letter to uppercase right. So in this particular case I have ABC, and I'm eventually going to get ABC after I'm done, because I'm reducing it down. One piece at a time like one string ABC.
[00:01:15] not an array but actually one string ABC. And just so you know that the second pass and to reduce the seed value so in this particular case would be an empty string but if I gave it like, I don't know. Hello here, it would be hello ABC, because that's what it would start with whatever accumulator would start with, I should say.
[00:01:38] If you don't give it a c value, it's gonna pop off the first thing, not pop is a bad word. It's going to shift off the first thing if you want to be technical. It's going to take the first value and use that as a seed. So, this particular would be a.
[00:01:52] If you're doing numbers, that's not a big deal but ,if you're doing like Letters in this particular case, the transformation the reduction doesn't get applied to the first one. So you would get a aBC, right? So with the A being lower case because that reducer would not apply that toUpperCase to that first letter, so that's why the c value can be important.
[00:02:15]
>> Brian Holt: Questions about that before I just show you a couple more examples?
>> Brian Holt: Okay, other way around. So I have a function called addTogether, it's gonna take a list and all it's gonna do is just add a bunch of numbers together. And that's all that does. So if you look down here.
[00:02:36] Add together. I take all these numbers and add them together. I promise if you add all those together you get 47. [LAUGH] So that's, reduce put that way. So concatenateStringsWithSpaces.
>> Brian Holt: What you're gonna do here is you're gonna have the accumulator and then you're gonna take a string and add a space on the end.
[00:03:03] And you're gonna end up here with this is so fun. So you're gonna get, this is so fun, right? One string.
>> Brian Holt: SquareAndSubtract, so this is where it starts getting cool that we can now, we have multiple pieces that we can chain together, so we're gonna square something.
[00:03:27] So first of all we're gonna map over it and square everything, and then we're going to subtract everything from each other. So let's take a look at how we're calling that down here.
>> Brian Holt: So if I have 10, 5421 It's going to square each 1 of these. So 100, 25, 16, 4, 1, right?
[00:03:47] And it's going to do. Because we don't give it a seed value, it's going to take the first value which is going to be 100. It's going to start subtracting everything else from it. So 100- 25- 16- 4- 1 and you're gonna end up with 54.
>> Brian Holt: So again, that's why the function program is cool, it's just applying these transformations one after each other and if you wanna see what reduce looks like internally, this is essentially what it is.
[00:04:17] So you're gonna start with the C value, right?
>> Brian Holt: And then you're gonna loop over them and just apply that function to each other and you're just gonna keep feeding that answer back in, until eventually you get the answer and you return that. That's it. So as you can see, reduce is not terribly complicated.
[00:04:38] Reduce is what you would say is a higher order function. That's what I was referring to earlier, that it's taking in some function and applying that to other things.