In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how ES6 uses a sentence of code to achieve the Curitization of the function, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
What does Collier do? First, take a look at the following function
Let store = (a _ dint _ b _ c) = > "this is your seven noodles" / / the function is like a snack bar, a bowl of seven-son noodles requires three pieces of soft girl coins: a _ c (five parameters)
What is the function Corialization? That is, the process of buying noodles may be like this:
Let curryStore = curry (store) / / the commissary just got Corey / / Best condition curryStore / / Boss, just right! Keep the change. It's exactly seven yuan. Boss: "this is your seven noodles" / / the occasional situation let boss = curryStore (5) / / Boss, take the five yuan first and I'll see if you have a dollar. Boss:. Boss = boss (1) / / with the boss, take this dollar first and I'll see if there is another dollar. Boss:. Boss (1) / / , finally found it, here! Boss: "this is your seven noodles" curryStore (5) (1) (1) / / equivalent to the appeal situation
So here we can see that the Corialization of the function can be used to slowly gather up the parameters and delay the execution of the function. Pay in installments first and then deliver the goods! )
Do a question.
Now, our goal is to implement a curry function to achieve the following effect: execute the function when enough parameters are given. When there are not enough arguments, a new curry function is returned.
Let curryPlus = curry (curry bpenc) = > a+b+c) / / here gives a function curryPlus (1) (2) (3) / / returns 6curryPlus (1) (2) / / returns 6curryPlus (1) (2) (3) / / returns 6let x = curryPlus (1) (2) / / Hey, why are there only two parameters? Returns a curry function (with two arguments) x (1) / / returns 4x (2) / / returns 5
As a person who is very good at js, it is not a problem to solve this kind of problem with one line of code. The problem is that I am not that good.
So, let's start with the local method, orz.
According to the use of curry, the principle is that a function is returned with an uncertain number of parameters (you may take out two pieces of one dollar at the same time), so we can use the method of indefinite parameters:
Const curry = (fn) = > {return (... args) = > {/ / uncertain parameters, give as much as you want / / pay for delivery}
The Corialization function needs to remember the parameters you have given him, and if not, it defaults to an empty array:
Const curry = (fn,arr= []) = > {/ / arr array is used to record the existing parameters return (... args) = > {/ / payment delivery link}}
Next, when you call each time, you need to check whether there are enough parameters. If so, execute fn. If not, return a new curry function and insert the existing parameters to him:
Const curry = (fn, arr = []) = > {return (... args) = > {/ / determine whether the total number of parameters equals the number of fn parameters if ([... arr,... args]. Length = fn.length) {return fn (... arr,... args) / / expand the parameter, call fn} else {return curry (fn, [... arr,... args]) / / iteration, pass in all existing parameters}
At this point, we have actually implemented the curry function.
The next thing to do is to see how to write it more succinctly. First of all, the intermediate code can be written as an immediate execution function, saving some. Arr,. Args:
Const curry = (fn, arr = []) = > {return (... args) = > {return (a = > {/ / an is an array if (a.length = fn.length) {return fn (... a)} else {return curry (fn, a)}}) ([... arr,... args]) / / here arr and args are spread out into an array to assign values to a}}
If statements can be reduced to ternary expressions, or you can save a lot of words:
Const curry = (fn, arr = []) = > {return (... args) = > {return (a = > {return a.length = fn.length? Fn (... a): curry (fn, a)}) ([... arr,... args])}}
Finally, since nothing is done in the function, just return, you might as well use input = > output, the most economical way of the arrow function, to save both return and curly braces:
Const curry = (fn, arr = []) = > {return (... args) = > {return (a = > a.length = fn.length? Fn (... a): curry (fn, a)) ([... arr,... args]) / / fold a layer}} first
Refold:
Const curry = (fn, arr = []) = > {return (... args) = > (a = > a.length = fn.length? Fn (... a): curry (fn, a) ([... arr,... args]) / / refold}
Let's call it a day:
Const curry = (fn, arr = []) = > (... args) = > (a = > a.length = fn.length? Fn (... a): curry (fn, a) ([... arr,... args]) / / the clothes are folded for you.
Try it:
Const curry = (fn, arr = []) = > (... args) = > (a = > a.length = fn.length? Fn (... a): curry (fn, a)) ([... arr,... args]) let curryPlus = curry ((ALECHEROBLING c args d) = > a+b+c+d) curryPlus (1 Magi 2) (4) / return 10curryPlus (1 Mague 2) (4) (3) / / return 10curryPlus (1 Mague 2) (3) / return 10
Of course, the main function of the Corialization function is to delay execution. The trigger condition for execution may not necessarily be equal to the number of parameters, but it can also be other conditions, for example, if the parameter is 0, then we need to modify the above curry function slightly:
Const curry = (fn, arr = []) = > (... args) = > ((args b) = > b.length = = 0? Fn (... a): curry (fn, a) ([... arr,... args], [... args]) let curryPlus = curry ((... x) = > x.reduce ((a) B) = > aqb) curryPlus (1) / / return a function curryPlus (1) (2) / / return a function / / execute curryPlus (1) (2) (4) () / return 7curryPlus (1) (2) (4) / / return 7 when the number of arguments is 0.
The above is all the content of the article "how to use one sentence of code to realize the Corialization of a function in ES6". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.