Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the advantage of Currying in C#? (achieving partial function)

Tags:

c#

currying

What is the advantage of Currying in C#?

What is the advantage of achieving partial function application on a curried function?

like image 669
masoud ramezani Avatar asked Mar 08 '10 14:03

masoud ramezani


People also ask

What are the advantages of currying?

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.

What is the purpose of currying functional programming?

Currying is useful in both practical and theoretical settings. In functional programming languages, and many others, it provides a way of automatically managing how arguments are passed to functions and exceptions.

What is the advantage of currying in Scala?

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.

What is meant by currying?

to praise someone, especially someone in authority, in a way that is not sincere, in order to get some advantage for yourself: He's always trying to curry favour with the boss. SMART Vocabulary: related words and phrases. Praising insincerely or too eagerly. backhanded compliment.


1 Answers

From Wikipedia

Currying is actually not very different from what we do when we calculate a function for some given values on a piece of paper.

Take the function f(x,y) = y / x

To evaluate f(2,3), first, replace x with 2.

Since the result is a new function in y, this function g(y) can be defined as

g(y) = f(2,y) = y / 2

Next, replacing the y argument with 3,

provides the result, g(3) = f(2,3) = 3 / 2.

On paper, using classical notation, it's just that we seem to do it all at the same time. But, in fact, when replacing arguments on a piece of paper, it is done sequentially (i.e.partially). Each replacement results in a function within a function. As we sequentially replace each argument, we are currying the function into simpler and simpler versions of the original. Eventually, we end up with a chain of functions as in lambda calculus, where each function takes only one argument, and multi-argument functions are usually represented in curried form.

The practical motivation for currying is that very often the functions obtained by supplying some but not all of the arguments to a curried function (often called partial application) are useful; for example, many languages have a function or operator similar to plus_one. Currying makes it easy to define these functions.

like image 76
David Avatar answered Sep 26 '22 02:09

David