Hello, little xia
Last time I shared a technique for reducing the energy cost of traversing sets.
With this trick, we put the original code:
It became something like this:
And at the end, I said I was going to share a new weapon with you, which is this:
It was a difficult thing to get hold of, and it took a lot of work, but at last we got it,
So,
Some young warriors may ask:
“In that case, Tianchen, what’s the use of this weapon you’ve gone to all this trouble to get?”
Little xia!
Now that you’re asking in good faith,
I will be merciful to tell you!
To keep the world from being destroyed,
In order to maintain world peace,
Through love and true evil,
Handsome and charming villain
Tianchen… Dreamer…
“Hey, Tianchen, are you on the wrong set? !!!!!!!!!”
“…”
So to go back to the question,
Here the correct answer is probably:
1. Although the last code above reduces the number of iterations, the logic is relatively coupled, which is not conducive to expansion. Transduce can solve this problem and has good expansibility under the premise of maintaining performance.
Knowledge depends on many key knowledge points, and learning that hand can help you understand them better.
Learning self-control can exercise abstract thinking very well and help shaoxia open their imagination.
, can be used to install force ~
All right,
Now let’s get down to business
To understand Transduce, we first need to understand reduce,
Let’s take a look at the code from last time:
In the end, we implemented filtering, mapping, and the final overlay operation with a single Reduce function.
As YOU may have noticed, Shaman,
The Reduce function is more powerful than we thought,
So,
Since the Reduce function is so powerful, can we use it instead of the Map or filter function?
And for this question,
Some young warriors might say,
“Cut ~, since day day you all so ask, that answer affirmation is can, otherwise you ask it why?”
For this kind of shaxia, Tianchen would like to say:
“You talk too much! ~”
But ~
We can indeed use Reduce to achieve the same effect as map and filter.
Let’s start with a simple example:
Here is an array:
Double numS with map:
Filter out odd numbers in nums:
Think it’s easy?
So how do we achieve the same effect with Reduce?
It’s easy too!
Please look at shaolin
Use reduce to double nums:
Use reduce to filter odd numbers in NUMS:
What! Easy, right?
Did you double that number?
Did you filter out an odd number?
At this point, you must be thinking:
Ha ha, don’t get excited, Shaoxia!
Although the above two examples are a bit excessive
But!
(Here please automatically switch to serious face ~?)
From the above two examples, we have learned at least one thing,
That reduce can give us the results we want,
It can either operate on some elements like a Map and give us an array of the same length;
Or, like a filter, give us a shorter array with some elements filtered out.
Or something weird
So,
And then the question is,
How do we get Reduce to give us what we want?
There are two things you can do here,
In the first case, you already know the result you want, and then you simply return the result by reducing, which is the two examples above…
In other words, Reduce doesn’t actually do anything, it just goes around in circles…
“Reduce guy, the day guy came to double the NUMS array again, do you remember what happened last time?”
“Yes, it’s [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]”
“Ok, just throw it to him and let’s go out and play.”
“Yeah, good.”
Of course, if xiaoxia you do not have my day so good ~
You probably don’t know exactly what the outcome will be right off the bat,
At this point, it’s up to you to tell Reduce how to generate the elements you want,
Like this:
To make it simpler, put the reduce function outside:
All right, Shaolin
Now that we have preliminarily implemented the map effect, the next step should be filter,
Same idea:
Now we have filters,
We combined the two reducer to use once:
Completely OK ~
Here, some young xia may say:
“Stop it! Tianchen, if you do this, aren’t you going to go through the 2 times group again? !”
Well, shaman, you’re one of the more careful people,
Here we’re using reduce twice, so it’s true that we’re going through the 2-degree group,
But ~
Who says I have to use reduce twice?
Look good,
Little xia ~
Now I’m gonna start acting like a pussy,
The basic internal force is not deep shaoxia please take the initiative to stand back 10 meters farther!
First of all,
We have extracted the double operation from mapReducer,
The double function that we’re familiar with,
Now we try to add values to the array from the mapReducer,
Also extracted separately:
And then, as we said in the last chapter,
MapReducer now relies heavily on double and pushValue functions in the current scope,
So, let’s change it to get it by passing in the form of a parameter:
Attention, Little xia
This might look a little bit more complicated,
But all that we do,
It’s really just a function that was previously accessed directly through the scope,
Instead of passing access through function arguments,
After changing the function to pass by argument,
We’re free to choose what function to pass in each time,
For example, replace double with add1:
According to this line of thinking,
The changed filterReducer would look like this:
At this point, if shaman you are a little confused…
Don’t worry,
You may be more confused later!
Later, we will go back and analyze the knowledge points separately…
The point here is that we can now use Reduce more flexibly:
I know what you’re trying to say
Now I still use reduce 2 times…
You see, I’m passing pushValue at the end as a joinFn function,
So when you filter out an odd number, you add it to the array,
But I could also, instead of adding it to the array right before I add it to the array,
So let’s just double the odd number that we filter out,
When you’re done, add it to the array
Now there is only one Reduce!
And then,
I don’t know if you’ve noticed, Shaman,
The magic function can actually be implemented using the mapReducer function
(Please watch carefully ~)
Replace the magic:
All right,
End of the pack
Ha ha ha ha ha!
“Hello, Tianchen! What are you talking about? Are you forgetting something? Transduce as promised!!”
Oh, I almost forgot…
All right, Shaman,
Transduce is a further abstraction of the following code:
However,
To be honest,
Explaining Transduce is more trouble than I thought…
(I should have known better…)
First of all,
Transduce usually requires a compose function to work with it:
With the compose function, we can simplify the following code:
And then,
Transduce looks like this:
The parameters correspond to the following parts:
It’s basically breaking up the first few pieces,
This is how it works,
Put all the code together:
If we want to add a filter condition:
Or if we don’t want to get an array at the end,
Instead of summing the numbers in an array:
All right,
Congratulations, Shaolin!
You have successfully discovered and read another article
According to the convention, first ~
Thank you, Shaolin, for seeing this,
Then ~
Some young warriors may be confused by this article,
That’s normal,
One is little Xia and I may not have written it very clearly,
Also, the implementation of TransDuce does involve a lot of other knowledge,
A large collection of higher-order functions, function closures, partial function applications, and function composition, etc.,
So why am I sharing Transduce with you?
Because,
Transduce itself may not be important,
You don’t have to use it, Sonny,
But understanding and mastering the knowledge it requires is important to many young warriors,
Especially to the pursuit of excellence, as a dreamer dreamer
And…
If you’re confused about higher-order functions, closures, and the this keyword,
More resistant words ~
So, let’s start with something a little more confusing,
When I go back and look at them,
You’ll find them much friendlier
So ~
Little xia,
Are you out of your depth or what?
Some questions you might be concerned about:
Tianchen, you honestly! Did you write this article to pretend to be a douche?
Hey, Sonny, is being a pussy gonna help me get a girl?
If I could, I’d be pretending,
If not, I’m sharing technical articles…
You are transduce. What are you going to do next time?
No more flags this time,
Transduce almost killed me…
However,
Next time you may encounter one of the following situations,
Encounter a new item, such as the compose function seen above:
Encounter new cheats or leftovers on functions, objects, recursion, etc. :
Meet an adventure challenge, complete a particular challenge:
3, Tianchen, you say transDuce design many key points…
Ha ha ha ha
Other resources:
Article:
Transducers: Efficient Data Processing Pipelines in JavaScript
Understanding Transducers in JavaScript
Tool library:
Transduce in the Ramda library
transducer.js
All right,
Shaoxia, river’s lake road, predestined relationship goodbye ~