f#

Awaiting an IAsyncOperation in F#

血红的双手。 提交于 2019-12-11 08:42:10
问题 I have the following code in F#: let CreateSampleDataFromJson<'T>(path) = let uri = new Uri(path) async { let file = StorageFile.GetFileFromApplicationUriAsync(uri) let jsonText = FileIO.ReadTextAsync(file) return JsonObject<'T>.Parse(jsonText) } The problem I'm having is that file is an IAsyncOperation<StorageFile> and not a StorageFile as ReadTextAsync expects. In C# you can do something similar to this: var file = await StorageFile.GetFileFromApplicationUriAsync(uri) i.e. public async Task

CompileAssemblyFromFile

风流意气都作罢 提交于 2019-12-11 08:31:45
问题 Why I get the error when trying to do this function: let compile_file path use_asms output = use pro = new FSharpCodeProvider() let opt = CompilerParameters(use_asms, output) let res = pro.CompileAssemblyFromFile(opt, path) // <-- Error if res.Errors.Count = 0 then Some(FileInfo(res.PathToAssembly)) else None Error-code: -2147467259 Error: cant find the file specified Now I'm trying two broken implementations: type System.CodeDom.Compiler.ICodeCompiler with member this.CompileAssemblyFromFile

F# How to call let function correctly? Function call in [<EntryPoint>] isn't working

你。 提交于 2019-12-11 08:30:23
问题 I am new to F#. What I am trying to do is to call test more than once to print out Hello World . In this example, I want to call test three times. When I run the code it only prints to console once. Even without the test call, it runs. I have the following code: open System let test = let mutable output = "" output <- "Hello World" printfn "%s" output [<EntryPoint>] let main argv = test test test ignore(Console.ReadKey()) 0 回答1: If you want test to be a function, declare it like this: let

Extending types in third-party library for serialization

让人想犯罪 __ 提交于 2019-12-11 08:27:12
问题 I need a function to convert types from a third-party library to IDictionary s so they can be easily serialized (to JSON). There are dependencies between the types so the dictionaries are sometimes nested. Right now I have something hideous like this: //Example type type A(name) = member __.Name = name //Example type type B(name, alist) = member __.Name = name member __.AList : A list = alist let rec ToSerializable x = match box x with | :? A as a -> dict ["Name", box a.Name] | :? B as b ->

Deedle moving window stats calcuation with a dynamic condition and boundary.atending

*爱你&永不变心* 提交于 2019-12-11 08:26:57
问题 I am using a dynamic moving window to calculation simple stats on a series ordered on the date key. I want to be able to set the boundary at the end of the window. for example a timeseries with monthly moving average, the monthly is decided by a (fun d1 d2 -> d1.addMonths(1) <= d2) however the deedle series function windowWhileInto cond f series always uses the begin as the boundary. Therefore, it always creates produce a n datapoints series from the first data instance for the next n data

ServiceStack F# sample fails starting

风格不统一 提交于 2019-12-11 08:25:41
问题 I'm trying to run this Self Hosting example, using latest ServiceStack release (4.0.3) and latest Mono/F# (3.2.5). It fails with an exception on appHost.Init() : { System.IO.FileNotFoundException: Virtual file not found File name: '<>.FSharpSignatureData.' at ServiceStack.VirtualPath.ResourceVirtualDirectory.CreateVirtualFile (System.String resourceName) [0x00033] in <>/ServiceStack/VirtualPath/ResourceVirtualDirectory.cs:99 } System.IO.FileNotFoundException The same does not happen with the

Async.Start with timeout and cancellationToken?

断了今生、忘了曾经 提交于 2019-12-11 08:07:45
问题 I have an Async<'T> computation that I want to run, and obtain the result 'T . I only have two requirements: After certain timeout:TimeSpan has passed, I want the computation/IO to be aborted. I want to run it with a cancellationToken just in case I want to abort it before timeout has passed. According to my requirement (1) above, you might think that Async.StartChild is a good candidate because it accepts a timeout parameter, however, it doesn't accept a cancellationToken parameter! It seems

Specifying type of parameter in F# function declaration

耗尽温柔 提交于 2019-12-11 08:00:07
问题 I am just starting out with F# so this is a bit of basic question on best practise with type inference. I am trying to write a function that works on a TimeSpan. This is a simplified version of what I am trying to do: let intervalsFromTimespan t = t.TotalMinutes / 5.0 Clearly this won't work because I need to somehow state that t is a timespan. Would the correct way be: let intervalsFromTimespan' t = (t : TimeSpan).TotalMinutes / 5.0 回答1: Try this: let intervalsFromTimespan (t : TimeSpan) = t

Recursively iterating over an array in F#

有些话、适合烂在心里 提交于 2019-12-11 07:56:51
问题 This is a rather simple request, but I am having trouble with F#'s syntax. I need to create a function to iterate over a bidimensional array recursively, increasing a counter every time a condition is met. In this case, the function takes an ID as a parameter, then checks to see how many times that ID is present inside the inner arrays. I was thinking of something like this: let runner array count = match count with |0 -> (exit loop) |n -> (perform function) runner array count-1 For the

Is this a proper thread-safe Random wrapper?

岁酱吖の 提交于 2019-12-11 07:54:44
问题 I am fairly inexperienced with threading and concurrency; to remedy that, I am currently working for fun on implementing a random-search algorithm in F#. I wrote a wrapper around the System.Random class, following ideas from existing C# examples - but as I am not sure how I would even begin to unit test this for faulty behavior, I'd like to hear what more experienced minds have to say, and if there are obvious flaws or improvements with my code, either due to F# syntax or threading