生成在C#中生成函数的函数的最佳方法

时间:2012-01-24 07:43:34

标签: c# f# functional-programming

F#提供了一个功能,其中一个功能可以返回另一个功能。

在F#中生成函数的函数示例是:

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;

我能想出在C#中实现同样目标的最好方法是:

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);
    }
}

还有其他方式(/更好的方法)吗?

4 个答案:

答案 0 :(得分:10)

当然,这在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);
  }
}

容易腻。如果您不喜欢清单类型,那么大部分内容都可以替换为var

答案 1 :(得分:6)

你可以编写一个函数来讨论另一个函数。不便之处在于您必须创建所需的所有重载。

一个例子:

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);
    }
}

答案 2 :(得分:3)

将原始F#几乎直译为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 :(得分:1)

如果你问的是如何将powerFunctionGenarator重写为C#,你可以用一种非常简单的方式来做:

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

你不能把这样的方法声明放在C#中的另一个方法中。但是如果你想这样做,你可以在lambda中使用lambda,正如sblom建议的那样:

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

这相当于F#中的以下代码:

let powerFunctionGenarator = fun baseNumber -> (fun exponent -> baseNumber ** exponent)