Suppose I want to implement a functional composition, like this:
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 like this:
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 a simpler syntax to invoke Compose? I tried like this:
var h = Compose(ToUpper, Replicate);
...but I get a compile error:
error CS0411: The type arguments for method 'FunctionalTest.Compose(System.Func, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
Quite understandable. I am wondering if it is possible to declare it differently and get the inference to actually work.
EDIT
The origin of the problem: I was watching an online lecture of an undergrad Functional Programming course, UC Berkley's CS61A. (find it on youtube). I don't have any formal training on FP, and I thought I might learn something. The prof uses scheme and he talks about how scheme + lisp are purely functional languages, and other languages are less so. He specifically identified Pascal, C, C++, and Java (but not C#) as lacking functional capabilities, and said it would be difficult to do functional composition with these languages ("Without standing on your head"). He asserted that a pointer-to-function (as available in C, C++) is not the same as a function "entity", a lambda. I get that.
Funny - he didn't mention Javascript or C#, which I consider to be mainstream languages that both have pretty good functional capabilities. (I don't know F#.)
I find it curious that this is a lecture from last year - 14 months ago - and yet he seems to be unaware of the functional aspects of mainstream, modern languages.
So I'm following along and doing exercises, but instead of using scheme or lisp, I'm using C#. And also doing some of them in Javascript.
Anyway thanks to everyone for the quality responses.