In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly talks about "what are the handwritten Compose methods". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the handwritten Compose methods?"
Why should we learn this method?
Introduction to compose
The realization of compose
The easiest way to understand
Reduce method in handwritten javascript
Implementation of compose in redux
Reference article
Why should we learn this method?
Encountered this method mainly recently in reading the principle of redux,koa and so on many times encountered this method, in order to better understand the principle of the framework, so in-depth study of the implementation of compose.
Then I also found that this belongs to functional programming, and found that functional programming is the only way to advance to the front end, because the concept of pure function like it is also shown incisively and vividly in redux's reducer, while the idea of retaining function calculation results can also be seen in many other frameworks, such as vue and react.
So it is recommended that you have time to look at the function trial programming.
Next, let's learn about the compose function.
Introduction to compose
Compose is to perform a series of tasks (functions), such as the following task queue
Let tasks = [step1, step2, step3, step4]
Every step is a step, which is executed step by step to the end. This is a compose.
Compose is a very important tool function in functional programming. The compose implemented here has three points.
The first function is multivariate (accepts multiple parameters), and the subsequent functions are all unit (accept a parameter).
Performing sequentially from right to left.
All functions are executed synchronously
Let's take an example, for example, there are several functions
Let init = (... args) = > args.reduce ((ele1, ele2) = > ele1 + ele2, 0) let step2 = (val) = > val + 2 let step3 = (val) = > val + 3 let step4 = (val) = > val + 4
These functions form a task queue.
Steps = [step4, step3, step2, init]
Use compose to combine this queue and execute
Let composeFunc = compose (... steps) console.log (composeFunc (1,2,3))
Execution process
6-> 6 + 2 = 8-> 8 + 3 = 11-> 11 + 4 = 15
So the process is executed from right to left from init, the parameters of the next task are the returned results of the previous task, and the tasks are synchronized, so that the tasks can be executed in an orderly direction and in an orderly time.
The realization of compose
Well, now that we know what compose is, let's implement it now!
The easiest way to understand
The idea is to use the idea of recursive process to constantly check whether there are any tasks in the queue, execute if there is a task, and pass the execution results back. Here is a local thinking, and it is impossible to predict when the task will end. Intuitively, it's the easiest to understand.
Const compose = function (... funcs) {let length = funcs.length let count = length-1 let result return function F1 (... arg1) {result = funcs[ count] .apply (this, arg1) if (count)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.