什么是柯里化函数

Currying function is a basic tool in functional programming.We can transform a function with multiple arguments into a sequence of nesting functions. It returns a new function that expects the next argument inline.

咖喱函数是函数编程中的基本工具,我们可以将具有多个参数的函数转换为嵌套函数序列。 它返回一个新函数,该函数需要内联下一个参数。

Let’s see a simple example. We’ll create a function ‘add’, which has three arguments and will return the sum of all arguments.

让我们看一个简单的例子。 我们将创建一个函数“ add”,该函数具有三个参数,并将返回所有参数的总和。

function add(a, b, c) {
return a + b + c;
}

add(1, 2, 3);

In above, we see a function with three arguments if we would like to change the numeric arguments we need to write a new function because:

在上面,我们看到一个带有三个参数的函数,如果我们想更改数字参数,我们需要编写一个新函数,因为:

  • This function expects only three arguments

    该函数只需要三个参数
  • It is considered good practice to write functions with a minimal amount of arguments

    编写带有最少参数的函数被认为是一种好习惯
  • We can’t call function add with two parameters or more than three parameters.

    我们不能使用两个或三个以上的参数来调用函数add。

If we want the function to be more flexible. We could use the currying function.

如果我们希望功能更加灵活。 我们可以使用currying函数。

function add(a) {
return function (b) {
return function (c) {
return a + b + c;
}
}
}

add(1)(2)(3); // 6

We used Currying functions:) It’s simple, right?

我们使用了Currying函数:)很简单,对吧?

Now, We want to write a function, which is more flexible. It will be a function which will not have specific arguments but return the sum of all arguments. We’ll use currying functions, of course.

现在,我们要编写一个更灵活的函数。 这将是一个没有特定参数的函数,但返回所有参数的总和。 当然,我们将使用currying函数。

function add(a) {
let sum = a;

const addSum = (b) => {
sum = sum + b;

return addSum ;
}

addSum .result = () => sum ;

return addSum ;
};

console.log('result ', add(2)(4).result()); // 6
console.log('result ', add(2)(4)(6).result()); // 12
console.log('result ', add(2)(4)(6)(9).result()); // 21

In the above snippet, We see a function ‘add’ with one parameter. This parameter assigns to variable ‘sum’. Variable sum will be the total sum of all parameters. According to the logic of the currying function we created another function ‘addSum’ with arguments ‘b’. This function adds the current sum to value b and returns itself.

在上面的代码片段中,我们看到了一个带有一个参数的函数“ add”。 此参数分配给变量“ sum”。 可变总和将是所有参数的总和。 根据currying函数的逻辑,我们创建了另一个参数为'b'的函数'addSum'。 此函数将当前总和加到值b上并返回自身。

Image for post
Call function add with parameters
调用函数添加参数

The completed ‘add property’ result. This property displays the result of the sum.

完成的“添加属性”结果。 此属性显示总和的结果。

何时以及为何使用Currying? (When and Why use Currying?)

Currying using when we want to have pre-configured behave our program. Also helps us avoid often call function with same parameter. Example:

当我们想要进行预配置时使用currying来表现我们的程序。 还可以帮助我们避免经常调用具有相同参数的函数。 例:

function add(2) {
return function (b) {
return 2 + b;
}
}

More advance examples find here. Currying can seems difficult but I hope that explained what is currying and you will not be have problem with Currying functions in JavaScript.

在这里可以找到更多高级示例。 咖喱化似乎很困难,但我希望能解释出什么是咖喱味,并且您不会对JavaScript中的咖喱化功能有任何疑问。

翻译自: https://medium.com/tomaszwlodarczyk/what-is-a-currying-function-a99c5ada57cf

### 函数柯里化的定义 函数柯里化Currying)是一种将原本接受多个参数的函数转换为一系列接受单个参数的函数的技术。这种技术源于数学和计算机科学中的λ演算,在函数式编程中尤为常见。通过柯里化,可以将一个需要多个参数的函数转换为多个嵌套的函数,每个函数只接受一个参数并返回一个新的函数,直到所有参数都被处理完毕[^3]。 ### 函数柯里化的工作原理 假设有一个函数 `f` 接受两个参数 `a` 和 `b`,柯里化后的函数 `g` 会先接受 `a`,然后返回一个新的函数,这个新函数再接受 `b` 并最终调用原始函数 `f`。例如,原始函数 `f(a, b)` 可以被柯里化为 `g(a)(b)`。 ```javascript function add(a, b) { return a + b; } // 柯里化后的版本 function curryAdd(a) { return function(b) { return a + b; }; } const addFive = curryAdd(5); console.log(addFive(3)); // 输出 8 ``` 在这个例子中,`curryAdd` 是 `add` 函数柯里化版本。它首先接受一个参数 `a`,然后返回一个新的函数,该函数再接受参数 `b` 并计算结果。这种方式使得函数可以逐步接收参数,而不是一次性接收所有参数[^1]。 ### 函数柯里化的优点 - **简化函数调用**:通过逐步传递参数,可以使函数调用更加灵活和简洁。 - **提高代码复用性**:柯里化可以创建部分应用函数,这些函数可以在不同的上下文中重复使用。 - **增强函数组合能力**:在函数式编程中,柯里化有助于函数的组合和管道操作,使得代码更具表达力和可维护性[^2]。 ### 函数柯里化的注意事项 尽管柯里化有许多优点,但在实际使用时也需要注意以下几点: - **性能影响**:柯里化会生成多个中间函数,这可能会对性能敏感的场景产生一定的影响。 - **可读性问题**:过度使用柯里化可能会降低代码的可读性,特别是对于不熟悉函数式编程的开发者来说,理解柯里化函数可能需要更多的时间[^4]。 ### 相关问题 1. 函数柯里化在实际开发中的应用场景有哪些? 2. 如何在 JavaScript 中手动实现函数柯里化? 3. 柯里化与部分应用函数(Partial Application)有什么区别? 4. 使用柯里化时如何处理 `this` 和上下文? 5. 在性能敏感的场景中,是否应该避免使用柯里化
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值