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

后端 未结 5 2066
情深已故
情深已故 2021-02-07 09:13

What is the advantage of Currying in C#?

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

5条回答
  •  遥遥无期
    2021-02-07 10:01

    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.

提交回复
热议问题