“This is the sixth day of my participation in the First Challenge 2022. For details: First Challenge 2022”
Get to know The Cremation
Kori-ization (English: Currying), also known as Karel, Gary. Currization is the technique of transforming a function that takes multiple arguments into a function that takes a single argument (the first argument of the original function), and returning a new function that takes the remaining arguments and returns the result.
It reduces code redundancy and increases code readability
This might not make sense, but let’s do an example
function sum(a,b,c,d){
return a+b+c+d
}
foo(10.20.30.40) / / 100
Copy the code
The sum function is the sum of the parameters passed in. If we change the sum function to a currization function:
function sum(a){
return function(b){
return function(c){
return function(d){
return a+b+c+d
}
}
}
}
Copy the code
As written above, the sum call will be
sum(10) (20) (30) (40) / / 100
const add=sum(10)
add(20) (30) (40) / / 100
Copy the code
As you can see from this example, corrification is to convert a function with many arguments into a function that can be passed in segments, thus reducing the passing in of backup arguments to the function.
Currization application scenarios
As an example in a real world scenario, the MyURL function is used to generate a concatenated URL link that takes three parameters: protocol, domain, and path.
function MyURL(protocol, domain, path) {
return protocol + ": / /" + domain + "/" + path;
}
Copy the code
MyURL('https'.'juejin.cn'.'user/2858385965322935');
// https://juejin.cn/user/2858385965322935
MyURL('https'.'juejin.cn'.'post/7055941374687838216');
// https://juejin.cn/post/7055941374687838216
MyURL('https'.'juejin.cn'.'post/7054594359206871053');
// https://juejin.cn/post/7054594359206871053
Copy the code
From the above three calls to the MyURL function, you will find that the first two arguments remain the same. We might want to encapsulate this function as follows.
function getUrl(path){
return MyURL('http'.'juejin.cn',path);
}
Copy the code
Changing the above call this way only requires changing the path parameter, but what if one day you need to change the domain parameter?
We can currify the MyURL function above, again passing in path. This is the purpose of function currification, to reduce code redundancy and increase code readability.
const justinPost=MyURL('http'.'juejin.cn')
justinPost('post/7055941374687838216')
// https://juejin.cn/post/7055941374687838216
justinPost('post/7054594359206871053')
// https://juejin.cn/post/7054594359206871053
Copy the code
Currization function implementation
In the above example, where we need to convert a function to a Cremation function manually, we can implement the function to a Cremation function by hand
The following is the currie function to achieve the function, a look to understand!
// Implement the Currization function
function MyCurrying(fn) {
function curried(. args) {
if (args.length >= fn.length) {
return fn.apply(this, args);
} else {
function curried2(. args2) {
return curried.apply(this, [...args, ...args2]);
}
returncurried2; }}return curried;
}
Copy the code
Analytical writing
- At the beginning, the function takes a function, and currizes that function.
- Check whether the number of parameters passed by the current function is greater than the number of parameters of the original function. If yes, pass
apply
Method to call a function - If the number of arguments of the original function is not reached: a function is returned to continue receiving the remaining arguments
- Call the returned function, when the number of arguments reached the original function arguments, through
apply
Method to call a function
Train of thought
The principle of closure is used to store the parameters passed in each time. When the parameters do not meet the expectation, a new function is returned to receive the remaining parameters and continue to call. If the parameters do not meet the expectation, recurse again.
knowledge
Function.prototype.length
Is to get the number of function arguments- If not used
apply
Method to call the original functionthis
Pointing incorrectly
The properties of corrification
The use of coriolization means additional memory overhead
- use
arguments
Object is slower than directly manipulating named parameters - Scope, memory overhead of closures, performance degradation
- use
call
,apply
Calling a function is slower than calling a function directly, and creates nested relationships
conclusion
Currie transformation is mainly based on closures, which store the parameters of the function and execute the function when the parameters reach a certain number. Using Corrification makes your code more flexible, but it also has some downsides, as its use of arguments, recursion, closures, and more can have a performance impact. In daily development, please use the Coriolization function according to the actual situation.