f#

Data sample for JSON type provider with optional property

蹲街弑〆低调 提交于 2019-12-23 09:19:48
问题 I am trying to use the JSON type provider to access StackOverflow / StackExchange data via the API. It works great, with one caveat. The API has a throttle, which is signaled by a field "backoff" that contains the number of seconds you are supposed to back off until your next request. As a result, I can't just point the JSON TP to the url, because by default the backoff field is not present. This is how the response typically looks: { "items": [ { "has_synonyms": true, "user_id": 1144035, "is

Pattern matching based on the function signature

对着背影说爱祢 提交于 2019-12-23 09:17:31
问题 In F# can you pattern match on a function signature. I want to decorate a number of functions with a function that measures the execution of the function and calls out to statsd. The current function I have is: let WrapFunctionWithPrefix(metrics:Metric.Client.IRecorder, functionToWrap, prefix) = let metricsIdentifier = (sprintf "%s.%s" prefix Environment.MachineName) using (metrics.StartTimer(metricsIdentifier)) ( fun metrics -> functionToWrap) As you can see above, the prefix will vary, and

F# instance syntax

岁酱吖の 提交于 2019-12-23 08:56:30
问题 What indicator do you use for member declaration in F#? I prefer member a.MethodName this is to many letters and x is used otherwise. 回答1: I do almost always use x as the name of this instance. There is no logic behind that, aside from the fact that it is shorter than other options. The options that I've seen are: member x.Foo // Simply use (short) 'x' everywhere member ls.Foo // Based on type name as Benjol explains member this.Foo // Probably comfortable for C# developers member self.Foo //

How do I write an exit handler for an F# application?

核能气质少年 提交于 2019-12-23 08:39:11
问题 Subject says it all. I want some code to run if my application is terminated by, say, ^C. 回答1: Use AppDomain.ProcessExit (http://msdn.microsoft.com/en-us/library/system.appdomain.processexit.aspx): System.AppDomain.CurrentDomain.ProcessExit(fun _ -> ...) 回答2: See code below. To handle Ctrl-C in a console app, use the Console.CancelKeyPress event. // does not work - no exception on Ctrl-C //System.AppDomain.CurrentDomain.UnhandledException.Add( // fun _ -> printfn "app is about to die") System

Using F# Option Type in C#

╄→尐↘猪︶ㄣ 提交于 2019-12-23 08:37:31
问题 I have the following type: and ListInfo() = let mutable count = 0 // This is a mutable option because we can't have an infinite data structure. let mutable lInfo : Option<ListInfo> = None let dInfo = new DictInfo() let bInfo = new BaseInfo() member this.BaseInfo = bInfo member this.DictInfo = dInfo member this.LInfo with get() = lInfo and set(value) = lInfo <- Some(value) member this.Count with get() = count and set(value) = count <- value where the recursive "list info" is an Option. Either

F# type constraints on enums

故事扮演 提交于 2019-12-23 08:12:26
问题 I'm trying to define a generic conversion operator from a string to an Enum, and I'd like to use it like this: let day = asEnum<DayOfWeek>("Monday") But with this implementation: let asEnum<'a, 'b when 'a: (new : unit -> 'a) and 'a : struct and 'a :> ValueType and 'a : enum<'b>> text = match Enum.TryParse<'a>(text) with | true, value -> Some value | false, _ -> None I can only use it like this: let day = asEnum<DayOfWeek,_>("Monday") or this: let day:DayOfWeek option = asEnum("Monday") If I

Is F# 3.0 runtime redistributable? [closed]

梦想与她 提交于 2019-12-23 07:57:55
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . Is legally OK to redistribute F# 3.0 runtime (FSharp.Core.dll versions 2.3.0.0, 4.3.0.0), and where do I find proof of that? I prepared a NuGet package with those but having second thoughts about licensing before publishing the package. Microsoft released F# 2.0 runtime as a redist package - so no questions

How to measure CPU and memory usage of F# code?

故事扮演 提交于 2019-12-23 07:51:31
问题 I'm new to the language F# and currently I'm doing a short study on the F# performance. So what I would like to do is benchmark my F# code. I've already found the Stopwatch class, which was pretty obvious, but as for the rest of what you should be able to test, I found no clear leads. What I would wish to do is to find out memory and cpu usage of the F# code when I run it. Is there anyone here that might be able to give me some advices on how to do this? 回答1: For quick prototyping, fsi's

Warning produced by f#: value has been copied to ensure the original is not mutated

梦想的初衷 提交于 2019-12-23 07:48:28
问题 The first definition below produces the warning in the title when compiled with f# 3.0 and the warning level set to 5. The second definition compiles cleanly. I wondered if someone could please explain just what the compiler worries I might accidentally mutate, or how would splitting the expression with a let clause help avoid that. Many thanks. let ticks_with_warning () : int64 = System.DateTime.Now.Ticks let ticks_clean () : int64 = let t = System.DateTime.Now t.Ticks 回答1: I cannot really

Ranges A to B where A > B in F#

房东的猫 提交于 2019-12-23 07:44:51
问题 I've just found something I'd call a quirk in F# and would like to know whether it's by design or by mistake and if it's by design, why is it so... If you write any range expression where the first term is greater than the second term the returned sequence is empty. A look at reflector suggests this is by design, but I can't really find a reason why it would have to be so. An example to reproduce it is: [1..10] |> List.length [10..1] |> List.length The first will print out 10 while the second