Functional-Light JavaScript, v3

# Deriving Transduction: Single Reduce

## Kyle Simpson

You Don't Know JS

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

The "Deriving Transduction: Single Reduce" 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 completes the derivation by composing the map and filter reductions, and then turning the remaining two reducers into one by passing sum into the transducer.

Preview
Close

### Transcript from the "Deriving Transduction: Single Reduce" Lesson

[00:00:00]
>> Kyle Simpson: The next step of our derivation asks us, what would we do if we had the list come. It's essentially we have to identify and again, this is pulling it out of thin air. We have to identify that to compose, we would have to have something to pass in.

[00:00:18]
So what if that those, two intermediary functions, were not being passed in this combination, what if they were stand alone? So let's get just the intermediary function representation, that transducer representation. What would that look like? Well, if I were to take mapReducer, look at line 19 specifically. If I were to take mapReducer, and call it with add1, it is a function that is expecting a reducer, right?

[00:00:46]
And if I call filter reducer with we have another one of those partial reducers or transducer or higher reducer. Whatever word you wanna use, we have another one of those that's waiting for a reducer. And what do both of those functions produce when provided a reducer, each of those functions produces a reducer.

[00:01:11]
If you pass a reducer into a higher order reducer, AKA a transducer, what do you get out? You get a reducer, well that's what's interesting because,f map reducer add one is expecting a reducer, how do we come up with a reducer to give it? Why don't we call filter reducer, and give it a reducer, and then that reducer becomes the input to the next one.

[00:01:36]
So rather than thinking about numbers flowing the composition, we're actually gonna be thinking about reducers being the value that travels through the composition. So this composed call on line 19 is setting up our two transducers into a composed transducer. And then, look on line 24 this is where the magic starts to happen, line 24 we pass in list combination.

[00:02:01]
Where does list combination go? It goes into the waiting filter reducer. Right, that waiting filter reducer transducer thing, that thing is sitting and waiting and it goes into that. And what's the end result? A reducer that can do filtering. Now that reducer that can do filtering, becomes the input to the waiting map reducer transducer.

[00:02:26]
The one on line 19, and that thing, when provided a filtering reducer, what does it do?
>> Kyle Simpson: It makes a mapping reducer. You follow me? So that mapping, filtering reducer, that's what we call transducer here, and that's why we can call list out reduce line 24 with that reducer.

[00:02:51]
It's doing both the mapping and the filtering at the same time, okay. So if you track this, this is gonna get really complicated, but I want you track that the things that are happening right here, when we call predicate function. What is that thing? It was the isOdd, and it returns this reducer, is this combined function.

[00:03:16]
Remember, filter reducer gives us a reducer, and that thing becomes the input to combiner. So when we call combined function here on line 8, we're actually calling this reducer. And this reducer has the filtering in it, right? So what we're doing here on line 8 is saying, I want you to get this mapped value and pass it into this combiner and let the combiner decide if it should get added to the list or not.

[00:03:43]
How does the combiner decide if it should get added to the list? It calls the predicate function. So this line of code, line 8, is doing both mapping and filtering at the same, time because combiner is the filtering part, it's the filtering reducer. And it becomes the mapping filtering reducer.

[00:04:03]
If I were to give these, we have several more slides, and I would give them these intermediate names, I would call this thing, filter transducer and I call this thing nap filter transducer because it has both mapping in filtering in it. There's one last step to do, okay?

[00:04:24]
The last step is that, we now have only two reduces instead of one. That's a good start, we've definitely gone from 3 down to 2, or if it had been 20 we would have gone from 20 down to 2. But there's one last thing, one last, wow you pulled that out of thin air.

[00:04:44]
What does listCombination do?
>> Kyle Simpson: listCombination says I have a value, and I have a running accumulator, which happens to be an array. And the way I combine my value in my accumulator, is to stick my value into the array. What does the sum function do? The sum function does the same thing, right?

[00:05:08]
The sum function says, I'm going to take a value and an accumulator and the way I'm gonna get those together is to do numeric addition. Both sum and listCombination are doing the same thing, which is that they are combining a value with an accumulator. So instead of first, making our intermediary list because the end result of reduce would have been a mapped and filtered array, that we then shrink down to a number.

[00:05:39]
Why don't we just pass some in instead of listCombination? And in other words, let's just delete listCombination entirely because we actually don't need it.
>> Kyle Simpson: Now that we have parameterized our reducer, our combiner, we can simply pass in the sum function. And that's what we do here on line 19, we pass in the sum function, and notice now, we don't need an empty array as our initial value, [be]cause why even have the intermediary array?

[00:06:10]
As we go along and do our mapping and filtering, and we finally get to that final delegation where we say, here's two values, you figure out what to do with them. Sum says, I know what to do with those, I just numerically add them together. There's no reason to go through the indirection of first creating an array that then has to get reduced.

### Learn Straight from the Experts Who Shape the Modern Web

• In-depth Courses