f#

How do I get a discriminated union case from a string?

南楼画角 提交于 2020-02-22 06:11:46
问题 I have a discriminated union and I want to select a case based on a string (which is read from a JSON file). This is easy to do: type MyDU = A | B let str = "B" let myDU : MyDU = match str with | "A" -> MyDU.A | "B" -> MyDU.B | _ -> failwith "whatever" // val myDU : MyDU = B However, sometimes there are many cases, which would require a lot of typing. The Microsoft.FSharp.Reflection library allows me to get a UnionCaseInfo object: open Microsoft.FSharp.Reflection let myDUInfo : UnionCaseInfo

How do I get a discriminated union case from a string?

对着背影说爱祢 提交于 2020-02-22 06:08:12
问题 I have a discriminated union and I want to select a case based on a string (which is read from a JSON file). This is easy to do: type MyDU = A | B let str = "B" let myDU : MyDU = match str with | "A" -> MyDU.A | "B" -> MyDU.B | _ -> failwith "whatever" // val myDU : MyDU = B However, sometimes there are many cases, which would require a lot of typing. The Microsoft.FSharp.Reflection library allows me to get a UnionCaseInfo object: open Microsoft.FSharp.Reflection let myDUInfo : UnionCaseInfo

Custom equality of types with statically resolved type parameters

笑着哭i 提交于 2020-02-21 23:57:33
问题 How to implement custom equality methods in F# types with statically resolved type parameters? I've tried doing it this way: [<CustomEqualityAttribute>] type Fraction< ^a when ^a: equality and ^a : (static member (*): ^a * ^a -> ^a) > (nominator: ^a, denominator: ^a) = member inline this.Nominator = nominator member inline this.Denominator = denominator member inline this.IsEqualTo(other: Fraction< ^a >) = this.Nominator * other.Denominator = other.Nominator * this.Denominator override inline

Why no warning for unused let bindings?

烂漫一生 提交于 2020-02-21 10:56:31
问题 C# warns for unused variables that are compile-time constants: static void Main(string[] args) { var unused = "hey"; //CS0219 The variable 'unused' is assigned but its value is never used Console.WriteLine("Hello World!"); } But the F# compiler does not, even though the editor now does pick it up: If it covered not just compile-time constants but all let bindings, this would have caught a real bug in production caused by a trivial mistake, something like let callApiXyz connectionInfo = async

Can I install/reference packages from within an fsx file?

倾然丶 夕夏残阳落幕 提交于 2020-02-19 16:00:51
问题 I'm trying to find a simple solution that isn't so much manual work to reference packages. inside a .fsx file. LinqPad 4 lets me simply add nuget packages no intellisense or autocompletion deletes package after download for certain types of packages (templatus for example) LinqPad 5 beta lets me add nuget packages deletes package after download for certain types of packages (templatus for example) with frequent failures (intellisense and compilation) VS2015 doesn't let you download/install

Is it possible to use or extend the FSharpChart library to create a line chart of a sliding window?

怎甘沉沦 提交于 2020-02-06 02:29:27
问题 I have been trying to figure out how to create a chart in F#, using the FSharpCharting library, which shows a sliding window of data. For example, I would like to be able to show all values for the past 3 minutes. Values older than that would fall off the chart, and be replaced as new values were observed. Based on my experiments, I don't think that the underlying Microsoft Chart Controls support this scenario. Does anyone know if it is possible to create this type of chart? Are there other

Is it possible to use or extend the FSharpChart library to create a line chart of a sliding window?

有些话、适合烂在心里 提交于 2020-02-06 02:28:16
问题 I have been trying to figure out how to create a chart in F#, using the FSharpCharting library, which shows a sliding window of data. For example, I would like to be able to show all values for the past 3 minutes. Values older than that would fall off the chart, and be replaced as new values were observed. Based on my experiments, I don't think that the underlying Microsoft Chart Controls support this scenario. Does anyone know if it is possible to create this type of chart? Are there other

What is a better way to model a treeNode?

限于喜欢 提交于 2020-02-05 02:11:17
问题 What are the pros and cons of each of the following two ways to create a tree node? type TreeNode = | TreeNode of int * (TreeNode option) * (TreeNode option) * (TreeNode option) type Node = | Node of int * Node * Node | None 回答1: They are very close, but not entirely equivalent. You can actually reason about functional types quite nicely using mathematics (see for example my recent article), but you can see that even informally. Given any TreeNode(num, optLeft, optRight) , you can always

What if the best way to return Option types by WCF service

那年仲夏 提交于 2020-02-04 08:22:26
问题 I have a WCF method which search the record in database and return Some(object) if the record exists and None if it doesn't. As I see I can't call the method which returns Type option through WCF (I get exception). What is the best way to design WCF services in F# in this way? For example, I can return empty Type Person { Name = "" Age = 0 // .... } if the record doesn't exist in DB, but I am looking for the best ideas... 回答1: A WCF service, just like a RESTful service, exposes an API that

how would I constrain to non-function?

梦想与她 提交于 2020-02-04 01:24:09
问题 I'm working on code that abstracts away System.Data.IDbConnection and its siblings. target code let inline runWithConnection connector f = match connector with | ICon con -> f con | CString cs -> use conn = new SqlConnection(cs) openConnection conn f conn I can see that type constraints compile and run, but constraining to object doesn't have the intended effect. I want to constrain to an object or value that is not a function that needs more inputs to return something. Alternatively write