问题
Below is a specific use case of using a normal and a curried function. Are there any advantages for using either if you only using two arguments?
//Normal Function
function add(x, y) {
return x + y;
}
//Curried Function
function add1(x) {
return function add2(y) {
return x + y;
}
}
回答1:
Here's a small example:
let add = (x, y) => x + y;
let addc = x => y => x + y;
// add 5 to every element
result = [1,2,3,4,5].map(x => add(x, 5)) // dirty and tedious
result = [1,2,3,4,5].map(addc(5)) // nice and tidy
In general, curried functions allow to express the logic in a "point-free" style, that is, as a combination of functions, without using variables, arguments and similar constructs.
来源:https://stackoverflow.com/questions/58080109/advantages-of-using-curried-functions-over-a-normal-function-in-javascript