f#

How to properly test Exceptions with FsUnit

房东的猫 提交于 2019-12-08 15:48:00
问题 I'm trying to figure out how to properly test exceptions with FsUnit. Official documentation states, that to test for exceptions I have to right something like this: (fun () -> failwith "BOOM!" |> ignore) |> should throw typeof<System.Exception> But, if I don't mark my test method with [<ExpectedException>] attribute it will always fail. Sounds reasonable because if we want to test for exceptions we have to add such attribute in C# + NUnit. But, as long as I've added this attribute it doesn't

Portable class library and .NET ConcurrentDictionary

こ雲淡風輕ζ 提交于 2019-12-08 15:45:36
问题 Looking at http://msdn.microsoft.com/en-us/library/dd287191(v=vs.110).aspx, it seems that ConcurrentDictionary and all of its friends in the System.Collections.Concurrent namespace are available for use in a Portable Class Library. However, when I create either an F# or C# Portable Class Library, even if I explicitly add a reference to mscorlib.dll , the compilation fails when using ConcurrentDictionary . Why? 回答1: The term "Portable" should be loosely applied right now. When you create the

Does using single-case discriminated union types have implications on performance?

蓝咒 提交于 2019-12-08 15:41:03
问题 It is nice to have a wrapper for every primitive value, so that there is no way to misuse it. I suspect this convenience comes at a price. Is there a performance drop? Should I rather use bare primitive values instead if the performance is a concern? 回答1: Yes, there's going to be a performance drop when using single-case union types to wrap primitive values. Union cases are compiled into classes, so you'll pay the price of allocating (and later, collecting) the class and you'll also have an

F# Power issues which accepts both arguments to be bigints

别来无恙 提交于 2019-12-08 15:29:12
问题 I am currently experimenting with F#. The articles found on the internet are helpful, but as a C# programmer, I sometimes run into situations where I thought my solution would help, but it did not or just partially helped. So my lack of knowledge of F# (and most likely, how the compiler works) is probably the reason why I am totally flabbergasted sometimes. For example, I wrote a C# program to determine perfect numbers. It uses the known form of Euclids proof, that a perfect number can be

F# Create 2D Array

匆匆过客 提交于 2019-12-08 15:29:08
问题 Hi I am wanting to create in F# a 2D array of size 1000x1000, with the value in the array at any position to be initialized as the same vaue of its index using the 2DArray class. i.e. position [1,1] would have value (1,1). I have looked at the syntaxt of Array2D.create, but am not sure how to use it properly... Any help would be appreciated... 回答1: Use Array2D.init to pass a function to specify the initial value of each. let a = Array2D.init 3 3 (fun x y -> (x,y)) printfn "%A" a 来源: https:/

code folding in Visual Studio for F#

亡梦爱人 提交于 2019-12-08 15:27:26
问题 I find that I tend to write long source files in F#. Some open source projects in F# also have long source files, e.g. FPersec and F# for excel. So it would be very helpful if code folding (even very limited support) is available in VS for F#. E.g. in a module, we can fold out functions that are stable, only leave functions that are subject to change unfold. Is this feature easy to be supported, e.g. by a third party vendor? 回答1: Unfortunately, this feature is not available in F# (neither

F#: Some, None, or Exception?

半世苍凉 提交于 2019-12-08 14:59:45
问题 I have been teaching myself F# lately, and I come from an imperative (C++/C#) background. As an exercise I have been working on functions that can do stuff with matrices, like add, multiply, get determinants, etc. Everything is going well in this regard, but I find that maybe I am not making the best decisions when it concerns handling invalid inputs, for example: // I want to multiply two matrices let mult m1 m2 = let sizeOK = validateDims m1 m2 // Here is where I am running to conceptual

How to use SQL IN statement in fsharp.data.sqlclient?

蓝咒 提交于 2019-12-08 14:54:25
问题 I have the following sample code. The objective is to run SQL statement with multiple input parameters. [<Literal>] let connectionString = @"Data Source=Localhost;Initial Catalog=Instrument;Integrated Security=True" [<Literal>] let query = "SELECT MacroName, MacroCode FROM Instrument WHERE MacroCode IN (@codeName)" type MacroQuery = SqlCommandProvider<query, connectionString> let cmd = new MacroQuery() let res = cmd.AsyncExecute(codeName= [|"CPI";"GDP"|]) |> Async.RunSynchronously However,

When are F# function calls evaluated; lazily or immediately?

佐手、 提交于 2019-12-08 14:44:00
问题 Curried functions in F#. I get the bit where passing in a subset of parameters yields a function with presets. I just wondered if passing all of the parameters is any different. For example: let addTwo x y = x + y let incr a = addTwo 1 let added = addTwo 2 2 incr is a function taking one argument. Is added an int or a function? I can imagine an implementation where "added" is evaluated lazily only on use (like Schroedinger's Cat on opening the box). Is there any guarantee of when the addition

How do I use breakpoints in F# interactive?

≯℡__Kan透↙ 提交于 2019-12-08 14:39:57
问题 I've started researching some ideas in algorithms using VS2010 and F# interactive. So, I've created a DebugScript.fsx , I write some code there and eventually send it to F#Int to test it. At some some moment I need to catch a bug. But I can't place a breakpoint even in a simple for loop: for i in stringarray do printfn "%s" i When I press F9 to set a breakpoint, the VS show a red circle with a warning sign. The hint for it is "The breakpoint will not currently be hit". Surely, I did open