Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

F# provides a feature, where a function can return another function.

An example of function generating 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 could come up with to achieve the same in C# was this:

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 some other way (/better way) of doing this?

like image 810
coder_bro Avatar asked Jan 24 '12 07:01

coder_bro


2 Answers

Sure, that's straightforward 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 typing, most of those can be replaced with var.

like image 169
Eric Lippert Avatar answered Sep 22 '22 18:09

Eric Lippert


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

An 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);
    }
}
like image 37
Paolo Tedesco Avatar answered Sep 19 '22 18:09

Paolo Tedesco