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

后端 未结 5 2065
情深已故
情深已故 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 09:46

    I've found partial function application, as opposed to currying, to be useful when I want to reuse code.

    To be clear, since the definitions of currying and partial function application seem to get blurred, by partial function application I mean taking a function with N parameters, and converting it into a function with N-1 parameters.

    In particular, it's been handy when writing unit tests. Since I'll be writing hundreds of unit tests I try to reuse test code wherever possible. So I may have a common test method that takes a delegate to a method I wish to test, plus some parameters to that method and an expected result. The common test method will execute the method under test with the supplied parameters, and will have several assertions comparing the result to the expected result.

    The problem comes when I want to test a method that has more parameters than the delegate being passed into the common test method. I could write another common test method which is identical to the first one, apart from taking a delegate with a different signature. That seems like repeating myself, however. To avoid having to write such duplicate code, I can use partial function application to convert a delegate taking, say, two parameters, into a delegate taking a single parameter. Now I can use my common test method to test methods that take either one or two parameters.

    Here's one of the helper methods I use to fix one of the arguments of the delegate that was passed in:

    /// <summary>
    /// Fixes an argument of an action delegate, creating a closure that combines the 
    /// delegate and the argument value. 
    /// </summary>
    /// <returns>An action delegate which takes only one argument.</returns>
    public static Action<TIn1> FixActionArgument<TIn1, TIn2>(Action<TIn1, TIn2> action, 
        TIn2 argumentValue)
    {
        return in1 => action(in1, argumentValue);
    }
    
    0 讨论(0)
  • 2021-02-07 09:52

    A simple Currying will be

    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication4
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                Func<double, double, double, double, double> newTonLaw = (m1, m2, r, g) => ((m1 * m2) / Math.Pow(r,2)) * g;
    
                // Mass of Earth= 5.98 * 10e24 , Gravitational Constant = 6.6726 * 10e-11
                Func<double, double, double> onEarth = (m2, r) => newTonLaw.Invoke(5.98 * 10e24, m2, r, 6.6726*10e-11);
    
                // Mass of Moon= 7.348x10e22 , Gravitational Constant = 6.6726 * 10e-11
                Func<double, double, double> onMoon = (m2, r) => newTonLaw.Invoke(7.348 * 10e22, m2, r, 6.6726 * 10e-11);
    
                Trace.WriteLine(onEarth(70, 6.38 * 10e6)); // result 686.203545562642
                Trace.WriteLine(onMoon(70, 6.38 * 10e6)); // result 8.43181212841855
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-07 09:57

    If your question was how to implement currying in C# , here is an example

    public Func<T1, Func<T2, TResult>> Curry<T1, T2, TResult>(Func<T1, T2, TResult> func)
        {
            return p1 => p2 => func(p1, p2);
        }
    

    Currying can be implemented in any language that supports closures(lambdas), and is useful for partial function application like in UI programming where all the input necessary for the execution of function isnt received, so a curried function is passed around with already received inputs captured in it.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-07 10:05

    The advantage of Currying in C# is that it allows C# developers to develop in a Functional Programming style.

    Think about LINQ. A LINQ query allows you to pass in a method as a parameter:

    someCollection.Where(x => x.someVal == 1);
    

    x.someVal == 1 gets evaluated as a function and then Where uses the return value in its own execution.

    It's an example that most .NET 3 developers are familiar with, but few realize that they're dabbling in Function Programming. Without the ability to Curry, LINQ wouldn't be possible.

    ...hopefull that makes up for my smart-ass comment.

    0 讨论(0)
提交回复
热议问题