22

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.

MM.
  • 4,224
  • 5
  • 37
  • 74
  • 1
    This seems related: http://stackoverflow.com/questions/4888480/practical-use-of-curried-functions/4894045#4894045 -- does it answer your question? – Artyom Shalkhakov Mar 14 '11 at 16:22
  • Thanks. Jakob's post gave me a lot of insight in this case. The linked post builds on that. – MM. Mar 16 '11 at 15:26
  • 1
    There is a simple, non-theoretical, example in this answer: http://stackoverflow.com/questions/2725811/is-currying-just-a-way-to-avoid-inheritance/2725841#2725841 – jtolle Mar 20 '11 at 21:37

1 Answers1

14

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.

Jakob
  • 24,154
  • 8
  • 46
  • 57
  • 4
    I think your point is illustrated more clearly by simply the partial application of + in your first example. Also don't you need currying to be able to do partial application? So currying, as the process that transforms tupled arguments to curried arguments (e.g. the 'curry' function in Haskell) is very useful. – Kurt Schelfthout Mar 14 '11 at 16:51
  • 3
    "Readability. Clearer intent. Shorter code" - frankly this is a terribly weak argument, considering how NOT readable function code generally is. I think you are just saying 'because familiarity bias'. – JoyalToTheWorld Aug 04 '17 at 18:32