What is the Currization of a function
Wikipedia:
In computer science, Currying, also translated as carrey or callization, is a technique of converting 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 a result.
Simple understanding is: through the function call to continue to return the function, to achieve multiple acceptance of parameters and finally unified processing of the function encoding form.
First we implement an instance where we implement a function that returns the sum of the arguments passed in
Common writing
function sum(a,b,b){
return a + b + c
}
const result = sum(1.2.3)
console.log(result) / / 6
Copy the code
So this is a very common and intuitive way to write it
But what about something like currie?
We can implement that
Currie notation
function sum(a){
return (b) = > {
return (c) = > {
return a + b + c
}
}
}
const result = sum(1) (2) (3)
console.log(result) / / 6
Copy the code
Here we meet the definition of the Kerritization through the function call to continue to return the function, the realization of multiple acceptance of parameters and finally unified processing of the function encoding form. Seeing this, I think some friends will have a question why do I write this? Why are simple things so complicated that there is no need!
Why is it corrified
Like this one up here, just to show you how it works. Of course, there must be a point to currization.
- Function as much as possible
- Reducing code redundancy also increases readability
- Pack to force 😄