Call a higher order F# function from C#

Given the F# higher order function (taking a function in parameter):

let ApplyOn2 (f:int->int) = f(2)  

and the C# function

public static int Increment(int a) { return a++; } 

How do I call “ApplyOn2” with “Increment” in parameter (from C#)? Note that ApplyOn2 is exported as Microsoft.FSharp.Core.FSharpFunc<int,int> which do not match with Increment’s signature.

Lift higher order function into monad

Let’s say I have a higher order function which performs some computation using values which it retrieves from it’s functional parameter. f :: a -> (b -> c) -> d where a,b,c,d are some concre

Using higher-order Haskell types in C#

How can I use and call Haskell functions with higher-order type signatures from C# (DLLImport), like… double :: (Int -> Int) -> Int -> Int — higher order function typeClassFunc :: … -&gt

Higher order function « filter » in C++

I wanted to write a higher order function filter with C++. The code I have come up with so far is as follows: #include <iostream> #include <string> #include <functional> #include &lt

Scala – Make signature of function parameter f of higher order function g dependent on varars of g

I am trying to define a higher order function f which accepts a variable number of parameters args of type Wrapper[T]* and a function parameter g in Scala. The function f should decapsulate each obje

Why can’t the F# compiler fully inline function arguments of a higher order function?

One of the things I love about F# is a real inline keyword. However, while it allows to write first order functions that perform the same as pasted code blocks, things aren’t so rosy for higher order

Higher order functions in C

Is there a proper way to implement higher order functions in C. I’m mostly curious about things like portability and syntax correctness here and if there are more than one ways what the merits and f

Higher order functions in C

Is there a proper way to implement higher order functions in C. I’m mostly curious about things like portability and syntax correctness here and if there are more than one ways what the merits and f

What do we call this (new?) higher-order function?

I am trying to name what I think is a new idea for a higher-order function. To the important part, here is the code in Python and Haskell to demonstrate the concept, which will be explained afterward.

Implementing a higher order function that performs currying in scala

A coworker of mine sent me a question as follows: Implement a HOF(higher order function) that performs currying, the signature of your function is as follows: def curry[A,B,C](f:(A,B) => C) : A =

Higher-order function of recursive functions?

Is there some way to wrap a recursive function via a higher-order function, so that the recursive call is also wrapped? (e.g. to log the arguments to the function on each call.) For example, suppose

Answers

Just create reference to your assembly:

#r @"Path/To/Your/Library.dll"
let ApplyOn2 (f:int->int) = f(2)
ApplyOn2 Library.Class.Increment

To get an FSharpFunc from the equivalent C# function use:

Func<int,int> cs_func = (i) => ++i;
var fsharp_func = Microsoft.FSharp.Core.FSharpFunc<int,int>.FromConverter(
    new Converter<int,int>(cs_func));

To get a C# function from the equivalent FSharpFunc, use

var cs_func = Microsoft.FSharp.Core.FSharpFunc<int,int>.ToConverter(fsharp_func);
int i = cs_func(2);

So, this particular case, your code might look like:

Func<int, int> cs_func = (int i) => ++i;
int result = ApplyOn22(Microsoft.FSharp.Core.FSharpFunc<int, int>.FromConverter(
            new Converter<int, int>(cs_func)));

If you would like to provide a more friendly interop experience, consider using the System.Func delegate type directly in F#:

let ApplyOn2 (f : System.Func<int, int>) = f.Invoke(2)

You would be able to call your F# function very easily in C# like this:

MyFSharpModule.ApplyOn2(Increment); // 3

There is an issue with the Increment function as you have written it, however. You need the prefix form of the increment operator in order for your function to return the correct result:

public static int Increment(int a) { return ++a; }