S
S
Sergey2014-02-13 13:27:53
C++ / C#
Sergey, 2014-02-13 13:27:53

C# double lambda (currying)

Please explain this expression:

class Program
    {
        static Func<string, Func<string, string>> func;
        static void Main(string[] args)
        {
            func = a => b => a = b;
        }
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vadim Martynov, 2014-02-13
@besagon

Here is an example of usage.

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

Alas, I did not succeed in trying to formulate an explanation better than the article. In fact, you can call a function that will return a function, one of whose parameters will be the parameter passed to the first function .... In general, a function of two arguments yields 2 functions of 1 argument, but the second function is a derivative of the first.
In your example, there is not much logic, but the call can be like this:
In this case, the value of the first parameter will be assigned to the second, but are strings, nothing will happen to the first one.

S
Sergey, 2014-02-13
@besagon

Thanks @Vadimyan . Indeed, such constructions are used when currying a function. Initially, I simply could not understand the construction like func (a) (b) and could not even find in Google what this expression means. But now everything has become on the metsa.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question