Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# define function with partial application as delegate

Consider the following method:

int Foo(string st, float x, int j)
{
    ...
}

Now I want to wrap it in a delegate of type Func<float, int> by providing values for the parameters st and j. But I don't know the syntax. Can someone help?

This is the idea (might look a bit Haskell-ish):

Func<float, int> myDelegate = new Func<float, int>(Foo("myString", _ , 42));
// by providing values for st and j, only x is left as a parameter and return value is int
like image 226
Kjara Avatar asked Oct 21 '25 13:10

Kjara


1 Answers

This should do the trick:

Func<float, int> f = (x) => { return Foo("myString", x, 42); };

Partially applying functions the way you want to do it is currently only possible in F#, not in C#.

like image 92
Schmid Avatar answered Oct 23 '25 02:10

Schmid