f#

How to catch any exception (System.Exception) without a warning in F#?

大兔子大兔子 提交于 2019-12-06 17:55:40
问题 I tried to catch an Exception but the compiler gives warning: This type test or downcast will always hold let testFail () = try printfn "Ready for failing..." failwith "Fails" with | :? System.ArgumentException -> () | :? System.Exception -> () The question is: how to I do it without the warning? (I believe there must be a way to do this, otherwise there should be no warning) Like C# try { Console.WriteLine("Ready for failing..."); throw new Exception("Fails"); } catch (Exception) { } 回答1: C#

Record-type recursive member functions and the “rec” keyword

允我心安 提交于 2019-12-06 16:43:17
问题 I've always believed that in F# we needed to use the rec keyword for every recursive function, for example: let rec factorial = function | 0 -> 1 | k when k > 0 -> k * (factorial (k - 1)) | failwith "oops!" Today I was playing around with F# and I came up with a code similar to the following: let MyRecordType = { Something : float; SomethingElse : int } with static member factorial = function | 0 -> 1 | k when k > 0 -> k * (MyRecordType.factorial (k - 1)) | failwith "oops!" As you see, I've

How to use high order functions to filter and match on options fields in a record

空扰寡人 提交于 2019-12-06 14:31:03
问题 So a bit of context is required just to understand my issue. In an RPG, I consider the equipment as a record with optional fields, which means, while they're None that nothing was attributed yet. Equipment is constructed by the game items of the game that represents either weaponry or character protection (helmets and etc) which will be seen in the snippet below. The functionalities are removed and the domain model reduced to make it easier to read. type ConsummableItem = | HealthPotion |

Get random numbers in F#

ぃ、小莉子 提交于 2019-12-06 14:09:57
I have the following function in F#: let randomCh () = let random = Random() alph.Chars (random.Next (alph.Length - 1)); This function returns same value each time. I had same problem in C#, and solution very simple: create just one Random object like this: var rnd = new Random(); for (int i = 0; i < 10; i++) Console.WriteLine(rnd.Next(10)); How can I reach same behaviour in F#: get each time random value? Well your C# code doesn't define a function whereas your F# does; you could have the same problem in C#. You should either refactor your random definition out of the randomCh function, or

Async/Parallel Combined

删除回忆录丶 提交于 2019-12-06 13:40:50
问题 I have seen some examples of combining async with parallel in F#. Here's an MSDN sample: http://msdn.microsoft.com/en-us/library/dd233250(v=vs.120).aspx Isn't that an inefficient use of threads? Why would you want to use new threads for many, potentially long running, IO type operations. Isn't that basically creating threads that will just sit there and wait? 回答1: Quick answer: Asynchronous workflows are lightweight, user-space threads. You can create large numbers of such asynchronous

Map to Deedle Frame

回眸只為那壹抹淺笑 提交于 2019-12-06 12:38:55
I am learning F#. I am trying to convert a Map<string, seq<DateTime * float>> to a Deedle dataframe ( http://bluemountaincapital.github.io/Deedle/tutorial.html#creating ). I have prapared the following code: let folderFnct (aFrame:Frame) colName datesAndValues = let newSerie = Series(Seq.map (fun x -> fst x) datesAndValues, Seq.map (fun y -> snd y) datesAndValues) let newFrame = aFrame.Join([colName], [newSerie], kind=JoinKind.Inner) newFrame let mapToDeedleFrame myMap frame = Map.fold ( fun s ticker datesAndValues -> folderFnct s ticker datesAndValues) frame myMap mapToDeedleFrame folds the

WebSharper - How to expose dynamically mapped strategy-pattern objects on the server to the client?

◇◆丶佛笑我妖孽 提交于 2019-12-06 12:28:39
问题 I am at the process of learning WebSharper, and I am struggling with making some of my logic work on the client-side. I have a few server-side objects with inheritance hierarchy, which I need to expose to the client-side. They deal with generating different parts of the page as doc fragments -- but on the client. [<JavaScriptExport>] type [<AbstractClass>] A() = abstract member Doc: Map<string, A> -> Doc ... [<JavaScriptExport>] type [<AbstractClass>] B() = inherit A() [<JavaScriptExport>]

How to fold over a discriminated union

蓝咒 提交于 2019-12-06 12:27:39
I'm attempting to implement a fold over a discriminated union. The DU is called Expr, and represents a program expression, and is often recursive. I'm attempting to write a fold that accumulates the result of an operation over the Exprs recursively. Below is my attempt to write the fold. let rec foldProceduralExpr (folder : 's -> Expr list -> 's) (state : 's) (expr : Expr) : 's = let children = match expr with | Series s -> s.SerExprs | Lambda l -> [l.LamBody; l.LamPre; l.LamPost] | Attempt a -> a.AttemptBody :: List.map (fun ab -> ab.ABBody) a.AttemptBranches | Let l -> l.LetBody :: List

Why are all my functions being run even though I'm only calling one function in one module?

一个人想着一个人 提交于 2019-12-06 11:29:42
I have the following code in a Test.fs file: namespace Testing module test1 = let Run = printfn "Test1" module test2 = let Run = printfn "Test2" In my Program.fs I am calling: [<EntryPoint>] let main argv = let sw = Stopwatch.StartNew() printfn "%A" Testing.test1.Run sw.Stop() printfn "Problem took %d minutes, %d seconds, and %d milliseconds" sw.Elapsed.Minutes sw.Elapsed.Seconds sw.Elapsed.Milliseconds let s = Console.ReadLine() 0 // return an integer exit code This outputs Test1 Test2 Why is Test2 being outputting even though I am only calling Test1.Run ? test1.Run is not a function, it is a

What's wrong with my tree traversal code?

折月煮酒 提交于 2019-12-06 11:21:26
I have a simple tree, defined thusly: type BspTree = | Node of Rect * BspTree * BspTree | Null I can get a collection of leaf nodes (rooms in my tree "dungeon") like this, and it seems to work: let isRoom = function | Node(_, Null, Null) -> true | _ -> false let rec getRooms dungeon = if isRoom dungeon then seq { yield dungeon } else match dungeon with | Node(_, left, right) -> seq { for r in getRooms left -> r for r in getRooms right -> r } | Null -> Seq.empty But now I'm trying to get sister leaf node rooms so I can connect them with corridors, and I'm failing miserably (as I often do). Here