c#-to-f#

C# async / await method to F#?

馋奶兔 提交于 2019-11-30 19:00:58
I am trying to learn F# and am in the process of converting some C# code to F#. I have the following C# method: public async Task<Foo> GetFooAsync(byte[] content) { using (var stream = new MemoryStream(content)) { return await bar.GetFooAsync(stream); } } Where bar is some private field and GetFooAsync returns a Task<Foo> . How does this translate to F#? Here is what I currently have: member public this.GetFooAsync (content : byte[]) = use stream = new MemoryStream(content) this.bar.GetFooAsync(stream) Which returns a Task . In F#, asynchrony is represented by the async computation builder,

Translating async-await C# code to F# with respect to the scheduler

陌路散爱 提交于 2019-11-30 06:44:22
I wonder if this is too a broad question, but recently I made myself to come across a piece of code I'd like to be certain on how to translate from C# into proper F#. The journey starts from here (1) (the original problem with TPL-F# interaction), and continues here (2) (some example code I'm contemplating to translate into F#). The example code is too long to reproduce here, but the interesting functions are ActivateAsync , RefreshHubs and AddHub . Particularly the interesting points are AddHub has a signature of private async Task AddHub(string address) . RefreshHubs calls AddHub in a loop

C# and F# casting - specifically the 'as' keyword

℡╲_俬逩灬. 提交于 2019-11-29 02:57:41
In C# I can do: var castValue = inputValue as Type1 In F#, I can do: let staticValue = inputValue :> Type1 let dynamicValue = inputValue :?> Type1 But neither of them is the equivalent of the C#'s keyword as . I guess I need to do a match expression for the equivalent in F# match inputValue with | :? Type1 as type1Value -> type1Value | _ -> null Is this correct? As far as I know, F# doesn't have any built-in operator equivalent to C# as so you need to write some more complicated expression. Alternatively to your code using match , you could also use if , because the operator :? can be use in

F# Discriminated Union usage from C# [duplicate]

放肆的年华 提交于 2019-11-29 01:26:22
This question already has an answer here: What is the simplest way to access data of an F# discriminated union type in C#? 7 answers What are the best ways to use F# Discriminated Unions from C#? I have been digging into this problem for a while, I have probably found the simplest way, but as it is rather complex, there may be some other thing I don't see... Having a discriminated union, e.g.: type Shape = | Rectangle of float * float | Circle of float the usage from C# I found would be (avoiding using vars, to make the type obvious): Shape circle = Shape.NewCircle(5.0); if (circle.IsCircle) {

Using c# delegates with f# functions

有些话、适合烂在心里 提交于 2019-11-28 12:23:46
I am trying to call a c# function from f# where the c# function takes a function (delegate?) as a parameter and I need this argument to be a f# function. Eg: Sample c# public static void function_1(double x, ref double y) { y = Math.Exp(x); } main() { state s; call_func(s, function_1) } So, call_func has a parameter of type void fn(double, ref double) In f# I tried: let function_1 (x:double) (y:double byref) = let y = 6.0 () let test = let s = new state let ret = call_func(s, function_1) But I get the error that the f# function_1 has type double -> double byref -> unit when it should be the

Method Chaining vs |> Pipe Operator

好久不见. 提交于 2019-11-28 09:41:26
So I have the following code: // Learn more about F# at http://fsharp.net open System open System.Linq open Microsoft.FSharp.Collections let a = [1; 2; 3; 4; 54; 9] let c = a |> List.map(fun(x) -> x*3) |> List.filter(fun(x) -> x > 10) let d = a.Select(fun(x) -> x*3).Where(fun(x) -> x > 10) for i in c do Console.WriteLine(i) for i in d do Console.WriteLine(i) Both seem to do the same thing, but most F# examples I see use the |> pipe operator, while I'm more used to method chaining (a.l.a. C# Linq). The latter is also somewhat shorter, albeit somewhat more crunched together. For now i'm using

How to organize F# source of large project (>300 classes) in Visual Studio?

陌路散爱 提交于 2019-11-27 19:24:49
In C# you can put files in folders corresponding to their namespaces and view them in the Solution explorer. In F# it seems I have to put everything in plain specifically ordered list for compilation. When I get to scale of ~300 of classes it gets a bit confusing and disorganized and I start to envy C# and think that probably it is the price for type inference. Are there better options than splitting to several assemblies? Judging by F# compiler source its the route they have taken but I have pretty large system of interconnected components (Controller - ViewModel - View, > 300 classes) I want

C# and F# casting - specifically the 'as' keyword

走远了吗. 提交于 2019-11-27 17:12:12
问题 In C# I can do: var castValue = inputValue as Type1 In F#, I can do: let staticValue = inputValue :> Type1 let dynamicValue = inputValue :?> Type1 But neither of them is the equivalent of the C#'s keyword as . I guess I need to do a match expression for the equivalent in F# match inputValue with | :? Type1 as type1Value -> type1Value | _ -> null Is this correct? 回答1: As far as I know, F# doesn't have any built-in operator equivalent to C# as so you need to write some more complicated

F# Discriminated Union usage from C# [duplicate]

不打扰是莪最后的温柔 提交于 2019-11-27 14:53:43
问题 This question already has answers here : What is the simplest way to access data of an F# discriminated union type in C#? (7 answers) Closed 5 years ago . What are the best ways to use F# Discriminated Unions from C#? I have been digging into this problem for a while, I have probably found the simplest way, but as it is rather complex, there may be some other thing I don't see... Having a discriminated union, e.g.: type Shape = | Rectangle of float * float | Circle of float the usage from C#

How to organize F# source of large project (>300 classes) in Visual Studio?

谁说胖子不能爱 提交于 2019-11-26 19:52:16
问题 In C# you can put files in folders corresponding to their namespaces and view them in the Solution explorer. In F# it seems I have to put everything in plain specifically ordered list for compilation. When I get to scale of ~300 of classes it gets a bit confusing and disorganized and I start to envy C# and think that probably it is the price for type inference. Are there better options than splitting to several assemblies? Judging by F# compiler source its the route they have taken but I have