f#

what is use cases of F# explicit type parameters?

放肆的年华 提交于 2020-01-13 16:57:11
问题 As I know, explicit type parameters in value definitions is a one way to overcome "value restriction" problem. Is there another cases when I need to use them? Upd : I mean "explicitly generic constructs", where type parameter is enclosed in angle brackets, i.e. let f<'T> x = x 回答1: This would likely be rare, but when you want to prevent further generalization (§14.6.7): Explicit type parameter definitions on value and member definitions can affect the process of type inference and

Deserialization in F# vs. C#

假如想象 提交于 2020-01-13 14:59:10
问题 I have the following json: { "data": [ { "timestamp": "2019-11-07T00:23:52.095Z", "symbol": "XBTUSD", "side": "Buy", "size": 1, "price": 9356.5, "tickDirection": "PlusTick", "trdMatchID": "01476235-ad89-1777-9067-8ce6d0e29992", "grossValue": 10688, "homeNotional": 0.00010688, "foreignNotional": 1 } ] } The last 3 fields are optional. When I deserialize it in C#, I do the following: public class Trade { public DateTime Timestamp; public string Symbol; public string Side; public long Size;

Deserialization in F# vs. C#

吃可爱长大的小学妹 提交于 2020-01-13 14:58:10
问题 I have the following json: { "data": [ { "timestamp": "2019-11-07T00:23:52.095Z", "symbol": "XBTUSD", "side": "Buy", "size": 1, "price": 9356.5, "tickDirection": "PlusTick", "trdMatchID": "01476235-ad89-1777-9067-8ce6d0e29992", "grossValue": 10688, "homeNotional": 0.00010688, "foreignNotional": 1 } ] } The last 3 fields are optional. When I deserialize it in C#, I do the following: public class Trade { public DateTime Timestamp; public string Symbol; public string Side; public long Size;

Parsing method arguments with FParsec

北战南征 提交于 2020-01-13 13:04:06
问题 I am trying to implement a method arguments parser with FParsec. I was wondering if there is some already implemented feature in FParsec itself that'd aid me on this purpose? I ask this as FParsec provides tooling when dealing with operator precedence, so there might be something for this too. Parsing the opening and closing braces is pretty straight-forward. The headache lies in dealing with the following 3 cases that can happen: Method arguments can consist of: no arguments, one argument,

Recursive function vs recursive variable in F#

左心房为你撑大大i 提交于 2020-01-13 11:22:09
问题 The first method is OK. The second repeats constantly the same pair of numbers. It is quite obscure to me why... Could you point to the good direction ? module Normal = let rnd = new MersenneTwister() let sampleNormal = fun () -> let rec randomNormal() = let u1, u2 = rnd.NextDouble(),rnd.NextDouble() let r, theta= sqrt (-2. * (log u1)), 2. * System.Math.PI * u2 seq { yield r * sin theta; yield r * cos theta ; printfn "next";yield! randomNormal() } randomNormal() let sampleNormalBAD = fun () -

Performance penalty when Generic.List<T>.Add is the the last statement in a function and tailcall optimization is on

你说的曾经没有我的故事 提交于 2020-01-13 08:49:18
问题 I've run into a strange performance penalty that I've boiled down to this code: [<Struct>] type Vector3(x: float32, y: float32, z: float32) = member this.X = x member this.Y = y member this.Z = z type Data(n: int) = let positions = System.Collections.Generic.List<Vector3>() let add j = positions.Add (Vector3(j, j, j)) let add1 j = positions.Add (Vector3(j, j, j)); () member this.UseAdd () = for i = 1 to n do add (float32 i) member this.UseAdd1 () = for i = 1 to n do add1 (float32 i) let

fsc.exe is very slow because it tries to access crl.microsoft.com

时间秒杀一切 提交于 2020-01-13 08:26:06
问题 When I run F# compiler - fsc.exe - on our build server it takes ages (~20sec) to run even when there are no input files. After some investigation I found out that it's because the application tries to access crl.microsoft.com (probably to check if some certificates aren't revoked). However, the account under which it runs doesn't have an access to the Internet. And because our routers/firewalls/whatever just drops the SYN packets, fsc.exe tries several times before giving up. The only

F# Object Initialization with a Constructor

北城余情 提交于 2020-01-13 08:15:11
问题 I know that in F# if you have a C# class of the format: public class Person { public DateTime BirthDate { get; set; } public string Name { get; set; } } You can initialize it like so, which is nice: let p = new Person (Name = "John", BirthDate = DateTime.Now) However how would you initialize it in F# if the C# class also had a constructor like this: public class Person { public Person(int id) { Id = id } public int Id {get; private set;} public DateTime BirthDate { get; set; } public string

Use case for F# Choice type

房东的猫 提交于 2020-01-13 07:55:22
问题 I've been aware of the F# Choice type for a while , but can't think of any place I'd use it rather than defining my own union type with meaningful named cases. The MSDN documentation doesn't offer much advice ("Helper types for active patterns with two choices.") and doesn't have any example usages. I must be missing something - what are the key advantages of this type over a custom union? 回答1: In my opinion the use cases for the Choice type are quite similar to the use cases for tuple types.

Use case for F# Choice type

痴心易碎 提交于 2020-01-13 07:55:07
问题 I've been aware of the F# Choice type for a while , but can't think of any place I'd use it rather than defining my own union type with meaningful named cases. The MSDN documentation doesn't offer much advice ("Helper types for active patterns with two choices.") and doesn't have any example usages. I must be missing something - what are the key advantages of this type over a custom union? 回答1: In my opinion the use cases for the Choice type are quite similar to the use cases for tuple types.