f#

Strip Unit of Measure from array

谁都会走 提交于 2020-01-24 11:14:44
问题 I am calling external function requiring float[] , but my array is float<m>[] . How could I strip the unit of measure from array? I need something like the function below, but this does not compile. And I would like to avoid any iterating or duplicating of the array, as float<m> and float values are identical... let demeasure (arr:float<m>[]): float[] = float[] (arr) 回答1: I believe that a cast to obj , followed by a dynamic cast to float[] would work, e.g. (arr :> obj) :?> float[] because

Explicitly specifying parameter types in F#

非 Y 不嫁゛ 提交于 2020-01-24 11:11:06
问题 I'm writing an F# function that factorises a number into prime factors. let factors primes i = let mutable j = i for p in primes do while (j>1) && (j%p=0) do j <- j/p printfn "prime: %i" p It works for int values of i , but not int64 values. The parameter primes is a set of int values. I understand why this is the case - type inference is assuming that the function only takes int parameters - but I want to explicitly specify the parameter type as int64 . Is it possible to write this function

Working with missing values in Deedle Time Series in F# (1)

痴心易碎 提交于 2020-01-24 10:21:06
问题 here is a small example where I want to deal with missing values on custom functions on series. suppose that i have obtained a series series4;; val it : Series<int,int opt> = 1 -> 1 2 -> 2 3 -> 3 4 -> <missing> for example, this way: let series1 = Series.ofObservations [(1,1);(2,2);(3,3)] let series2 = Series.ofObservations [(1,2);(2,2);(3,1);(4,4)] let series3 = series1.Zip(series2,JoinKind.Outer);; let series4 = series3 |> Series.mapValues fst Then if i do, Series.mapAll (fun v -> match v

Function reading from Console.In returns previous values

若如初见. 提交于 2020-01-24 09:56:34
问题 In order to learn F# I'm trying to solve the Codingame puzzle The Descent 8 numbers are read from Console.In.ReadLine() the index of the highest number should be printed repeat 8 times Below code works for the first run, but appears to give the exact same result after the first loop for different inputs. Running the idxWithHighestValue function in Interactive with an array instead of reading from console gives the desired results. Expected behavior: [9,8,7,6,5,4,3,2] => 0 [0,8,7,6,5,4,3,2] =>

How to achieve assembly binding redirect in a plugin scenario?

♀尐吖头ヾ 提交于 2020-01-24 07:49:11
问题 I have a plugin P extending and application A (.NET40) that I have no control over. The P assembly (.NET40) has a shared dependency D (.NET35). Both P and D depend on FSharp.Core, but different versions: P is compiled against FSharp.Core, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a D is compiled against FSharp.Core, Version=2.3.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a Only FSharp.Core, Version=4.4.0.0 is deployed and I subscribe to AppDomain.AssemblyResolve

Recursive objects in F#?

为君一笑 提交于 2020-01-24 03:43:05
问题 This snippet of F# code let rec reformat = new EventHandler(fun _ _ -> b.TextChanged.RemoveHandler reformat b |> ScrollParser.rewrite_contents_of_rtb b.TextChanged.AddHandler reformat ) b.TextChanged.AddHandler reformat results in the following warning: traynote.fs(62,41): warning FS0040: This and other recursive references to the object(s) being defined will be checked for initialization-soundness at runtime through the use of a delayed reference. This is because you are defining one or more

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

自古美人都是妖i 提交于 2020-01-23 15:36:09
问题 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 there a relationship between untyped/typed code quotations in F# and macro hygiene?

最后都变了- 提交于 2020-01-23 07:20:31
问题 I wonder if there is a relationship between untyped/typed code quotations in F# and the hygiene of macro systems. Do they solve the same issues in their respective languages or are they separate concerns? 回答1: Quotations are a form of meta-programming. They allow you to manipulate abstract syntax trees programmatically, which can be in turned spliced into code, and evaluated. Typed quotations embed the reified type of the AST in the host language's type system, so they ensure you cannot

Group by with tuples in F#

我们两清 提交于 2020-01-23 04:31:06
问题 Suppose I have a list of tupples like these : [("A",12); ("A",10); ("B",1); ("C",2); ("C",1)] And I would like to do some kind of groupby how do I handle that? In pseudocode-SQL it should look something like this : SELECT fst(tpl), sum(lst(tpl)) FROM [TupplesInList] GROUP BY fst(tpl) yielding [("A",22); ("B",1); ("C",3)] I could make a Dictionary and add the ints if the key exist, but I can hardly believe that would be the best solution in a language as expressive as F#. 回答1: One solution:

Return an F# record type as JSON in Azure Functions

梦想与她 提交于 2020-01-22 21:28:25
问题 I'm creating a simple azure function in F#. At the end, I'm returning a record type as JSON. I'm doing something like this: let y = {Gender = "Woman"; Frequency = 17; Percentage = 100.0} req.CreateResponse(HttpStatusCode.OK, y); When I call the function from Postman I'm getting this JSON {"Gender@":"Woman","Frequency@":17,"Percentage@":100} It looks like that this is caused by the default serializer (Serializing F# Record type to JSON includes '@' character after each property). Then I tried