简体   繁体   English

在 javascript 中使用柯里化函数优于普通 function 的优势

[英]Advantages of using curried functions over a normal function in javascript

Below is a specific use case of using a normal and a curried function.下面是使用普通和咖喱 function 的具体用例。 Are there any advantages for using either if you only using two arguments?如果只使用两个 arguments,使用其中任何一个有什么好处?

//Normal Function
function add(x, y) {
    return x + y;
}

//Curried Function
function add1(x) {
    return function add2(y) {
        return x + y;
    }
}

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.通常,柯里化函数允许以“无点”风格表达逻辑,即作为函数的组合,不使用变量、arguments 和类似的构造。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM