Inference of typical types with functional composition

Suppose I want to implement functional composition, for example:

public Func<T,T> Compose<T>(Func<T,T> f, Func<T,T> g) { return new Func<T,T>( x => f(g(x))); } 

Now in practice, I can use this Compose () fn as follows:

  public String ToUpper(String s) { return s.ToUpper(); } public String Replicate(String s) { return s+s; } public void Run() { var h = Compose<String>(ToUpper, Replicate); System.Console.WriteLine("{0}", h("fred")); } 

And the result is FREDFRED .

Is there a way to use simpler syntax to call Compose? I tried like this:

  var h = Compose(ToUpper, Replicate); 

... but I get a compilation error:

error CS0411: type arguments for the method "FunctionalTest.Compose (System.Func, System.Func)" cannot be taken out of use. Try explicitly specifying type arguments.

It’s completely clear. I am wondering if it is possible to declare it differently and get the conclusion that it really works.


EDIT
Origin of the problem: I watched an online lecture on a functional programming course led by UC Berkley CS61A. (find it on youtube). I have no formal FP training, and I thought I could learn something. Prof uses a scheme, and he talks about how the + lisp scheme is purely functional languages ​​and other languages ​​less. He specifically identified Pascal, C, C ++, and Java (but not C #) as missing functionality, and said that it would be difficult to perform functional composition with these languages ​​("Do not stand on your head"). He argued that a function pointer (as available in C, C ++) does not match the entity function, a lambda. I understand.

Funny - he did not mention Javascript or C #, which I consider to be the main languages ​​that have pretty good functionality. (I do not know F #.)

I am curious that this is a lecture from last year - 14 months ago - and yet he does not seem to know about the functional aspects of the main modern languages.

So, I follow and do the exercises, but instead of using a scheme or lisp, I use C #. And also some of them in Javascript.

In any case, thanks to everyone for the quality answers.

+6
c # functional-programming
source share
4 answers

The following code will work:

 Func<string, string> toUpper = ToUpper; Func<string, string> replicate = Replicate; // now the compiler knows that the parameters are Func<string, string> var h = Compose(toUpper, replicate); 

So perhaps you can still improve readability by defining these variables only once and reusing them during tests (I assume this is a test utility ...)

+4
source share

I like the answer of Ran (+1), but I think that makes it a little more concise and enjoyable. (It works under the assumption that you have the ability to override functions as follows.)

 Func<string, string> toUpper = s => s.ToUpper(); Func<string, string> replicate = s => s + s; var h = Compose(toUpper, replicate); 
+4
source share

Adding an answer to lasseespeholt if you define Compose as an extension method (renamed β€œThen” so that the result makes more sense):

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

You can do it freely:

 var h = toUpper.Then(replicate); // .Then(trim) etc... 
+4
source share

You can also pass a Compose parameter, and actually evaluate the function; it should be able to infer the type of the parameter in this case. (You may need to specify a return type.)

Other than that, no, there is no way to output such things in C #.

+2
source share

All Articles