Inferring generic types with functional composition

前端 未结 4 1592
猫巷女王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:39

    I like Ran´s answer (+1), but I think this make it a little more concise and nice. (Works under the assumption that you have the possibility to redefine the functions as follows.)

    Func toUpper = s => s.ToUpper();
    Func replicate = s => s + s;
    
    var h = Compose(toUpper, replicate);
    

提交回复
热议问题