柯里化有哪些实际优势?
我看到很多关于柯里化技术是什么的文档和问题,但我发现很少有关于为什么人们会在实践中使用它的信息。我的问题是,柯里化有什么优点?也许您可以提供一个简单的示例,其中柯里化比传统方法调用更可取。
我在太阳升起的时候就在 C++ 中工作,所以到目前为止,除了闲暇时修改语言之外,我几乎没有接触过柯里化。
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,将部分函数应用误认为柯里化是很常见的。例如,请参阅 this (我确信有更好的资源描述它,但这是我发现的第一个)。我几乎从未见过有人在实践中使用柯里化(除了像 Haskell 这样的语言,可以说,每个函数都是由语言本身柯里化的,但即使如此也是为了实现简单的部分函数应用)。另一方面,偏函数应用在许多语言中都非常有用。
无论如何,假设您正在谈论部分函数应用程序(因为这是大多数人在询问柯里化时所谈论的内容),这个概念在 C++ 中并不像在(纯)函数式语言(例如 Haskell)中那么自然例如。
例如,这里我们定义了一个函数
sum
,它接受一个数字数组list
并将所有数字相加。如果您不熟悉折叠(或有时称为减少或注入)的概念,请阅读 这个。不管怎样,它看起来像这样:但是等一下。我们可以通过使用部分函数应用来缩短它!我们不提供参数,而只是说
sum
是一个等于 Foldl 的函数,其中部分应用了 + 和 0。哪一篇更容易阅读?可能是偏好问题,但在我看来,后者更清楚地强调了 sum 和 Foldl 之间的关系。请考虑到这是一个非常简单的示例。老实说,我不知道如何用 C++ 编写一个好的示例,所以请原谅我。无论如何,实际优势是什么?可读性。意图更明确。更短的代码。
免责声明:如果您确实想了解柯里化(相对于部分函数应用)的优点,我很抱歉让您阅读了所有这些内容。但另一方面,如果您了解两者之间的区别,也会理解柯里化是实现部分函数应用的好方法。
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.
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.
For example, here we define a function
sum
that takes an array of numberslist
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: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.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.