Inferring generic types with functional composition

前端 未结 4 1611
猫巷女王i
猫巷女王i 2021-01-05 18:04

Suppose I want to implement a functional composition, like this:

    public Func Compose(Func f, Func g)
    {
            


        
4条回答
  •  没有蜡笔的小新
    2021-01-05 18:18

    Adding to lasseespeholt's answer, if you define Compose as an extension method (renamed "Then" so that the result makes more sense):

    public static Func Then(this Func f, Func g)
    {
        return x => g(f(x));
    }
    

    you can make this fluent:

    var h = toUpper.Then(replicate); // .Then(trim) etc...
    

提交回复
热议问题