1

I was reading a blog post about combining higher order functions and it provided a C# example of "currying".

The examples look like:

public static Func<T1, Func<T2, T3>> Curry<T1, T2, T3>
    (Func<T1, T2, T3> function) 
{ return a => b => function(a, b); }

public static Func<T1, Func<T2, Func<T3, T4>>> Curry<T1, T2, T3, T4>
    (Func<T1, T2, T3, T4> function)
{ return a => b => c => function(a, b, c); }

public static Func<T1, Func<T2, Func<T3, Func<T4, T5>>>> Curry<T1, T2, T3, T4, T5>
    (Func<T1, T2, T3, T4, T5> function) 
{ return a => b => c => d => function(a, b, c, d); }

My question is, can methods taking other forms that effectively produce the same "curried form" result be considered currying.

For example, are the following methods considered currying? If not, what is the more appropriate naming convention?

public static Func<T1, Func<T2, Func<T3, Func<T4, T5>>>> SomethingLikeCurry<T1, T2, T3, T4, T5>
    (Func<T1, T2, Func<T3, T4, T5>> function) 
{ return a => b => c => d => function(a, b)(c, d); }

public static Func<T1, Func<T2, Func<T3, T4>>> SomethingLikeCurry<T1, T2, T3, T4>
    (Func<T1, T2, Func<T3, T4>> function) 
{ return a => b => c => function(a, b)(c); }
4

1 回答 1

0

来自维基百科

.. currying 是一种转换具有多个参数(或参数元组)的函数的技术,使其可以作为一个函数链调用,每个函数都有一个参数......

那么你问过关于柯里化函数的两种方法吗?让我们简化他们的声明以便于检查:

Func<T1, T2, Func<T3, T4, T5>>  => Func<T1, Func<T2, Func<T3, Func<T4, T5>>>>
^ multiple arguments               ^ all single arguments 
Func<T1, T2, Func<T3, T4>>      => Func<T1, Func<T2, Func<T3, T4>>>
^ multiple arguments               ^ all single arguments

所以,答案是肯定的;这些是柯里化函数。

请注意:不是正确的柯里化函数签名:

Func<T1, T2, Func<T3, T4, T5>>  => Func<T1, Func<T2, Func<T3, T4, T5>>>>

由于函数的输出不是一个函数链,每个函数都有一个参数。因此只有一个更简单的弱类型柯里化签名,像这样

Func<T1, T2, TResult>           => Func<T1, Func<T2, TResult>>

TResult当结果是具有多个参数的函数时,不会产生柯里化。引发您兴趣的两种方法只是为了防止这种情况发生在具有一两个参数的函数中。当然,在 curried 版本中,类型T5也可能是一个有多个参数的函数,因此我们可以无限循环

于 2014-02-17T20:01:33.177 回答