これは機能しませんか?Extensionメソッドはパブリック静的クラスに含まれている必要があり、メソッド自体は静的である必要があることに注意してください。
public static class FunctionalExtensions
{
public static Func<T, V> Compose<T, U, V>(this Func<U, V> f, Func<T, U> g)
{
return x => f(g(x));
}
}
class Program
{
static void Main(string[] args)
{
Func<double, double>
f1 = Square, // Func<double, double> is a delegate type so we have to create
g1 = AddOne, // an instance for both f and g
h1 = f1.Compose(g1); // This is a Func<double, double> now, (i.e. h(X) = f(g(x)) )
// To call new function do this
double result1 = h1(5.0);
Func<double, double>
f2 = x => x*x,
g2 = x => x + 1,
h2 = f2.Compose(g2);
// To call new function do this
double result2 = h2(5.0);
}
public static double Square(double x)
{
return x * x;
}
public static double AddOne(double x)
{
return x + 1;
}
}
また、f1:double-> doubleおよびg1:double->doubleであることに注意してください。
作成機能で
f:V-> Uおよびg:U-> T
だからfg:V-> T
言い換えれば、私の例では、すべてのタイプがdoubleである必要はありませんでした。関数f(外部関数)の定義域に関数g(内部関数)の範囲が含まれていることを確認する必要があります。プログラミングでは、これは、gの戻り型がfのパラメーター型と同じである(または暗黙的にキャストされる)必要があることを意味します。