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.
Advantages of Currying The main benefit of currying is when you need to use the same call with some of the same parameters a lot i.e it helps to avoid passing the same variable again and again. In these situations, currying becomes a good technique to use. Currying will make your code easier to refactor.
Advantages of Currying Function in Scala One benefit is that Scala currying makes creating anonymous functions easier. Scala Currying also makes it easier to pass around a function as a first-class object. You can keep applying parameters when you find them.
Currying is a checking method to make sure that you get everything you need before you proceed. It helps you to avoid passing the same variable again and again. It divides your function into multiple smaller functions that can handle one responsibility.
Currying is the technique of converting a function that takes multiple arguments into a sequence of functions that each takes a single argument.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With