Posts

....
Technical Blog for .NET Developers ©

Thursday, July 29, 2021

FSharp : InvokeFast()

Microsoft defines InvokeFast() method to invoke an F# first class function value with two curried arguments. In some cases this will result in a more efficient application than applying the arguments successively
https://github.com/MicrosoftDocs/visualfsharpdocs/blob/master/docs/conceptual/fsharpfunc.invokefast

In this post we implement a basic use of FSharpFunc's InvokeFast, with a curried functions' structure

We begin top-to-bottom, so we first define a FSharpFunc which will takes a string param and will return a double value


              
              FSharpFunc<string, double> doublefunc = FuncConvert.ToFSharpFunc<string, double>((item) => getDouble(item));
              
              //
              
              static double getDouble(string item) => Convert.ToDouble(item);
  
After, we define a FSharpFunc which will takes a parameter type int, and will curry the function just created, with this implementation


  		FSharpFunc<int, FSharpFunc<string, double>> func = FuncConvert
                     .ToFSharpFunc<int, FSharpFunc<string, double>>(i => doublefunc);
  
Now we initialize a variable type int to test the functionality


  
            int o = 2;

            double test = FSharpFunc<int, string>.InvokeFast<double>(func, o, await getString_async(await getNumber_async(o)));  
  
  
<METHOD SOFTWARE 2021 ©>