Learn C# Programming by Marius Bancila Raffaele Rialdi and Ankit Sharma

Learn C# Programming by Marius Bancila Raffaele Rialdi and Ankit Sharma

Author:Marius Bancila, Raffaele Rialdi, and Ankit Sharma
Language: eng
Format: epub
Publisher: Packt Publishing Ptv Ltd
Published: 2020-04-28T00:00:00+00:00


These, when put in code, would look as follows:

var f1 = Curry<int, double, string, string>(AsString);

var f2 = f1(42);

var f3 = f2(43.5);

string result = f3("44");

The generic Curry() function seen here is similar to the Apply() function from the previous section. However, instead of returning a function with N-1 arguments, it returns a function with a single argument:

Func<T1, Func<T2, Func<T3, TResult>>>

Curry<T1, T2, T3, TResult>(Func<T1, T2, T3, TResult> f)

{

return a => b => c => f(a, b, c);

}

This function can be used to curry functions with exactly three parameters. Should you need to do that with functions that have another number of parameters, then you need appropriate overloads for it (just as in the case of Apply()).

You should note that you do not necessarily need to decompose the AsString() function three different times, as seen earlier with f1, f2, and f3. You can skip intermediate functions and achieve the same result by invoking the function appropriately, as shown in the following code:

var f = Curry<int, double, string, string>(AsString);

string result = f(42)(43.5)("44");

Another important concept in function programming is closures. We'll learn about closures in the next section.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.