Advantages of using curried functions over a normal function in javascript

南笙酒味 提交于 2020-01-11 12:51:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!