What is 'Currying'?

前端 未结 18 1290
遥遥无期
遥遥无期 2020-11-21 05:26

I\'ve seen references to curried functions in several articles and blogs but I can\'t find a good explanation (or at least one that makes sense!)

18条回答
  •  情话喂你
    2020-11-21 06:28

    Here you can find a simple explanation of currying implementation in C#. In the comments, I have tried to show how currying can be useful:

    public static class FuncExtensions {
        public static Func> Curry(this Func func)
        {
            return x1 => x2 => func(x1, x2);
        }
    }
    
    //Usage
    var add = new Func((x, y) => x + y).Curry();
    var func = add(1);
    
    //Obtaining the next parameter here, calling later the func with next parameter.
    //Or you can prepare some base calculations at the previous step and then
    //use the result of those calculations when calling the func multiple times 
    //with different input parameters.
    
    int result = func(1);
    

提交回复
热议问题