咖喱的实用优势是什么? [英] What are the practical advantages of currying?

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

问题描述

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



在太阳升起的时候,我用C ++工作,所以迄今为止我的曝光很少以解决以外的其他问题。

解决方案

首先,错误地将部分函数应用程序误认为是currying。例如,参见这个(我确信有更好的资源描述它,但这是我发现的第一个资源)。我几乎从来没有见过任何人在实践中使用currying(除了像Haskell这样的语言,每种功能都被语言本身所熟悉,可以这么说,但即使这样做也是为了实现简单的部分功能应用)。另一方面,部分功能应用程序在许多语言中非常有用。



无论如何,假设您正在讨论部分函数应用程序(因为这是大多数人在谈论什么时他们问的是currying),这个概念在C ++中并不像纯粹的函数式语言那样自然,比如Haskell。例如,在这里我们定义一个函数 sum ,它接受一个数组 list 并将所有数字相加在一起。如果您对折叠的概念(或缩小或注入,有时称为)不熟悉,请阅读。无论如何,它看起来像这样:

  sum list = foldl(+)0 list 

但请稍等。我们可以通过使用部分函数应用程序来缩短它!而不是提供参数,我们只是说 sum 是一个等于foldl的函数,其中+和0部分应用。

  sum = foldl(+)0 

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



免责声明:如果您真的想知道currying的优点(与部分功能应用程序相对)让你读到这一切。但另一方面,如果你了解两者的区别,也会明白,currying是实现部分函数应用的好方法。


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.

解决方案

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 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

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

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天全站免登陆