Best way to generate a function that generates a function in C #

F # provides a function in which a function can return another function.

An example of a function that generates a function in F # is:

let powerFunctionGenarator baseNumber = (fun exponent -> baseNumber ** exponent); let powerOfTwo = powerFunctionGenarator 2.0; let powerOfThree = powerFunctionGenarator 3.0; let power2 = powerOfTwo 10.0; let power3 = powerOfThree 10.0; printfn "%f" power2; printfn "%f" power3; 

The best way I was able to achieve this in C # was as follows:

 class Program { delegate double PowerOf2(double exponent); delegate double PowerOf3(double exponent); delegate double PowerOfN(double n, double exponent); static void Main(string[] args) { PowerOfN powerOfN = (a, b) => { return Math.Pow(a,b) ; }; PowerOf2 powerOf2 = (a) => { return powerOfN(2, a); }; PowerOf3 powerOf3 = (a) => { return powerOfN(3, a); }; double result = powerOf2(10); Console.WriteLine(result); result = powerOf3(10); Console.WriteLine(result); } } 

Is there any other way (/ better way) to do this?

+7
source share
4 answers

Of course, this is right in C #:

 using System; class P { static void Main() { Func<double, Func<double, double>> powerFunctionGenerator = baseNumber => exponent => Math.Pow(baseNumber, exponent); Func<double, double> powerOfTwo = powerFunctionGenerator(2.0); Func<double, double> powerOfThree = powerFunctionGenerator(3.0); double power2 = powerOfTwo(10.0); double power3 = powerOfThree(10.0); Console.WriteLine(power2); Console.WriteLine(power3); } } 

Easy peasy. And if you don't like the manifest, most of them can be replaced with var .

+10
source

You can write a function to create another function. The inconvenience is that you have to create all the overloads you need.

Example:

 using System; class Program { static Func<T2, TRes> Curry<T1, T2, TRes>(Func<T1, T2, TRes> f, T1 t1) { return (t2) => f(t1, t2); } static double PowerFunction(double d1, double d2) { return Math.Pow(d1, d2); } static void Main(string[] args) { var powerOf2 = Curry<double, double, double>(PowerFunction, 2); double r = powerOf2(3); } } 
+6
source

Almost literal translation of the original F # into C #:

 Func<double,Func<double,double>> powerFunctionGenerator = (baseNumber) => ((exponent) => Math.Pow(baseNumber, exponent)); var powerOfTwo = powerFunctionGenarator(2.0); var powerOfThree = powerFunctionGenarator(3.0); var power2 = powerOfTwo(10.0); var power3 = powerOfThree(10.0); Console.WriteLine(power2); Console.WriteLine(power3); 
+3
source

If you ask how to rewrite powerFunctionGenarator in C #, you can do it quite simply:

 Func<double, double> powerFunctionGenarator(double baseNumber) { return exponent => Math.Pow(baseNumber, exponent); } 

You cannot put this method declaration, like this, inside another method in C #. But if you want to do this, you can use lambda in lambda, as sblom suggested:

 Func<double, Func<double, double>> powerFunctionGenerator = baseNumber => exponent => Math.Pow(baseNumber, exponent); 

Which is equivalent to the following code in F #:

 let powerFunctionGenarator = fun baseNumber -> (fun exponent -> baseNumber ** exponent) 
+1
source

All Articles