In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what is function Corialization". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Corey (Currying)
Currying [1] is a high-order technique about functions. It is used not only in JavaScript but also in other programming languages.
Corialization is a function transformation, which refers to the conversion of a function from callable f (a, b, c) to callable f (a) (b) (c).
Corification does not call functions. It just converts the function.
Let's first look at an example to better understand what we are talking about, and then proceed to a practical application.
We will create a helper function curry (f), which will perform a Corialization of the function f with two parameters. In other words, executing curry (f) on the function f (a, b) with two parameters converts it to a function running in the form of f (a) (b):
Function curry (f) {/ / curry (f) performs return function (a) {return function (b) {return f (a, b);} / usage function sum (a, b) {return a + b;} let curriedSum = curry (sum); alert (curriedSum (1) (2)); / / 3
As you can see, the implementation is very simple: only two wrappers (wrapper).
The result of curry (func) is a wrapper function (a).
When it is called like curriedSum (1), its parameters are saved in the lexical environment and a new wrapper function (b) is returned.
The wrapper is then called with a parameter of 2, and it passes the call to the original sum function.
More advanced implementations of Currification, such as _ .curry [2] of the lodash library, return a wrapper that allows functions to be called normally or as partial:
Function sum (a, b) {return a + b;} let curriedSum = _ .curry (sum); / / use _ .curry (curriedSum (1,2)) from the lodash library; / / 3, you can still call alert (curriedSum (1) (2)) normally; / / 3, call it as a partial function
Corey? What is the purpose?
To understand its benefits, we need a practical example.
For example, we have a logging function log (date, importance, message) for formatting and outputting information. In a real project, this kind of function has many useful functions, such as sending logs over the network (log). Here we only use alert:
Function log (date, importance, message) {alert (`[${date.getHours ()}: ${date.getMinutes ()}] [${importance}] ${message}`);}
Let's make it Corey!
Log = _ .curry (log)
After Corey, log is still operating normally:
Log (new Date (), "DEBUG", "some debug"); / / log (a, b, c)
…… But it can also be run in the form of Corey:
Log (new Date ()) ("DEBUG") ("some debug"); / log (a) (b) (c)
Now we can easily create a convenient function for the current log:
/ / logNow will be the partial function let logNow = log (new Date ()) of the log with the first parameter fixed; / / use it to logNow ("INFO", "message"); / / [HH:mm] INFO message
Now, logNow is a log with a fixed first argument, in other words, a shorter "partial application function (partially applied function)" or "partial function (partial)."
We can go a step further and provide convenient functions for the current debug log (debug log):
Let debugNow = logNow ("DEBUG"); debugNow ("message"); / / [HH:mm] DEBUG message
So:
After Corey, we didn't lose anything: log can still be called normally.
We can easily generate partial functions, such as the one used to generate today's logs.
Advanced Corey implementation
If you want to know more details, here is the "advanced" Corialization implementation for multi-parameter functions, which we can also use in the above example.
It's very short:
Function curry (func) {return function curried (... args) {if (args.length > = func.length) {return func.apply (this, args);} else {return function (... args2) {return curried.apply (this, args.concat (args2));};}
Use case:
Function sum (a, b, c) {return a + b + c;} let curriedSum = curry (sum); alert (curriedSum (1,2)); / / 6, can still be called normally alert (curriedSum (1) (2) (3)); / / 6, alert (curriedSum (1) (2) (3)) for the first parameter; / 6, full Coriolization
The new curry may look a little complicated, but it's easy to understand.
The result of the curry (func) call is the wrapper curried shown below:
/ / func is the function function curried (... args) {if (args.length > = func.length) {/ / (1) return func.apply (this, args);} else {return function pass (... args2) {/ / (2) return curried.apply (this, args.concat (args2));}
When we run it, there are two if execution branches:
Call now: if the length of the args passed in is the same as or longer than the func.length defined by the original function, you just need to pass the call to it.
Get a partial function: otherwise, func has not been called. Instead, another wrapper pass is returned, which reapplies the curried, passing in the previously passed parameters with the new parameters. Then, in a new call, again, we will get a new partial function (if the arguments are insufficient), or the final result.
For example, let's look at the example of sum (a, b, c). It has three parameters, so sum.length = 3.
For calling curried (1) (2) (3):
The first call to curried (1) saves 1 in the lexical environment and then returns a wrapper pass.
The wrapper pass is called with an argument of (2): it takes the previous parameter (1), connects it to the resulting (2), and calls curried (1, 2) together. Because the number of arguments is still less than 3, the curry function still returns pass.
The wrapper pass is called again with an argument of (3), and in the next call, pass (3) takes the previous arguments (1, 2) and merges 3 with it, and executes the call curried (1, 2, 3)-which ends up with three arguments that are passed into the original function.
If this is not clear enough, you can go through the order of function calls in your mind or on paper.
Only functions that determine the length of parameters are allowed
Corialization requires a function to have a fixed number of parameters.
Functions that use the rest parameter, such as f (... args), cannot be Corialized in this way.
A little more than Corey.
By definition, Corey should convert sum (a, b, c) to sum (a) (b) (c).
However, as mentioned earlier, most of the Corialization implementations in JavaScript are advanced: they allow functions to be called by multiple parameter variants.
This is the end of the content of "what is function Corialization". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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: 289
*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.