How to understand the concept of function Corialization in a popular way, and give an example to illustrate? No, what's the difference between Corey and Corey?

the great gods give examples of how to understand the concept of function Corialization.


https://hackernoon.com/curryi.
is good in English. If you can read this article, you will basically understand it

. < hr >

updated

I don't know if you have read the article I shared with you.

now that you have updated the question and want to know the difference, I will briefly say some things, and if there are any mistakes, please correct them.

to understand the difference between the two, you first need to understand a concept called partial function (Partial Function), which is a more important knowledge point in FP . To put aside some academic stuff, as far as I understand it, it is a means of reusing code in functional programming, for example:

const add = x => y => x + y

const add2 = add(2)
const add3 = add(3)

add2(1) // 3
add3(1) // 4

if you directly use the add method with binary parameters, it goes like this:

const add = (x, y) => x + y

add(2, 1) // 3
add(3, 1) // 4

although the second method has less code than the first method, when calling add, the description of the business is coupled with the caller's parameter passing logic, but this problem does not exist in the former. The description of the business can obviously be known by the name of the intermediate variable, that is, add2 is added to 2, and add3 is added to 3. For atomic methods such as add2 and add3, you can separate them into a separate file, greatly improving readability, maintainability, and testability.

if I look at it this way, it seems superfluous, because I directly use the binary add method, and I can also encapsulate the versions of add2 and add3, but in FP , I generally use to combine (Composition) to realize the concepts of inheritance and polymorphism involved in OOP . Therefore, in the above example, add2 and add3 are generally abstracted as atomic business logic in the actual business work scenario. When these logics are combined with each other, they can form more complex logic, just like Lego bricks.

but there is a problem, that is, it is impossible for all the functions we usually use to have only one parameter, so, in order to enjoy this convenience, we do not have to force us to use a single parameter every time we write a function. Use Corialization to convert a multivariate parameter into a partial function.

another difference is that the Corialized functions are lazy in evaluation through combination, and the performance is better when the business logic is very complicated.


fill in several parameters in a function first, and then return a new function.


function add(a){
    var sum = 0;
    sum += a;
    return function(b){
        sum += b;
        return function(c){
            sum += c;
            return sum;
        }
    }
}
 
add(1)(2)(3);//6

take a look at this, which should help you understand: https://juejin.im/post/5af136.

Menu