柯里化的实际优势是什么? [英] What are the practical advantages of currying?

查看:24
本文介绍了柯里化的实际优势是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了很多关于柯里化技术是什么的文档和问题,但我发现很少有关于为什么在实践中使用它的信息.我的问题是,柯里化的优点是什么?也许你可以提供一个简单的例子,说明柯里化比传统的方法调用更可取.

I see a lot of documentation and questions about what the currying technique is, but I have found very little information on why one would use it in practice. My question is, what are the advantages of currying? Perhaps you can provide a trivial example where currying would be preferable to conventional method invocation.

我在太阳升起的时候用 C++ 工作,所以到目前为止,除了下班后修补语言之外,我几乎没有接触过柯里化.

I work in C++ while the sun is up, so to date I have had little exposure to currying other than out-of work tinkering with languages.

推荐答案

首先,将偏函数应用误认为柯里化是很常见的.例如,请参见 this(我确定有更好的资源描述它,但这是我找到的第一个资源).我几乎从未见过有人在实践中使用柯里化(除了像 Haskell 这样的语言,其中每个函数都由语言本身柯里化,可以这么说,但即使如此,也是为了启用简单的部分函数应用程序).另一方面,部分函数应用程序在许多语言中都非常有用.

First, it's very common to mistake partial function application for currying. See this for example (I'm sure there are better resources describing it, but this was the first one i found). I've almost never seen anyone use currying in practice (except for languages like Haskell, where every function is curried by the language itself, so to speak, but even that is in order to enable simple partial function application). Partial function application on the other hand is quite useful in many languages.

无论如何,假设您谈论的是部分函数应用程序(因为这是大多数人在询问柯里化时所谈论的内容),这个概念在 C++ 中不像在(纯)函数式语言中那么自然,例如 Haskell.

Anyway, assuming you're talking about partial function application (since that's what most people are talking about when they're asking about currying), the concept is not quite as natural in C++ as in a (purely) functional language, such as Haskell for example.

例如,这里我们定义了一个函数 sum,它接受一个数字数组 list 并将所有数字相加.如果您不熟悉折叠的概念(或有时称为减少或注入),请阅读 这个.无论如何,它看起来像这样:

For example, here we define a function sum that takes an array of numbers list and sums all the numbers together. If you're unfamilir with the concept of fold (or reduce or inject, as it is sometimes called), read this. Anyway, it would look like this:

sum list = foldl (+) 0 list

但是等一下.我们可以通过使用偏函数应用来缩短它!我们不提供参数,而是说 sum 是一个等于 foldl 的函数,部分应用了 + 和 0.

But wait a minute. We could shorten it by using partial function application! Instead of supplying an argument, we just say that sum is a function that is equal to foldl, with + and 0 partially applied.

sum = foldl (+) 0

哪个更容易阅读?可能是偏好问题,但在我看来,后者更清楚地强调了 sum 和 foldl 之间的关系.请注意这是一个非常简单的示例.老实说,我不知道如何用 C++ 编写一个好的例子,所以你必须在那里原谅我.无论如何,实际优势是什么?可读性.意图更明确.更短的代码.

Which one is easier to read? A matter of preference probably, but the latter emphazises the relation between sum and foldl more clearly in my opinion. And please take into account that this is a very simple example. I honestly don't know how to write a good example in C++, so you'll have to excuse me there. In any case, what is the practical advantage? Readability. Clearer intent. Shorter code.

免责声明:如果您真的想知道柯里化(相对于部分函数应用)的优点,我很抱歉让您阅读了所有这些.但另一方面,如果你了解两者的区别,也会明白柯里化是实现偏函数应用的好方法.

Disclaimer: If you actually wanted to know the advantages of currying (as opposed to partial function application) I'm sorry to have made you read all this. But on the other hand, if you understand the difference between the two will also understand that currying is a great way to implement partial function application.

这篇关于柯里化的实际优势是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆