f#

Type inheritance in F#

故事扮演 提交于 2019-12-07 08:25:23
问题 I can't find the proper syntax for coding a type D that inherits a base class B (written in C#) and his constructors other than the base class implicit constructor: C# code: public class B { private int _i; private float _f; public B() { _i = 0; _f = 0.0f; } public B(int i) { _i = 0; _f = 0.0f; } public B(int i, float f) { _i = i; _f = f; } } F# code: type D() = inherit B() //how to inherit from other constructors ? Thanks 回答1: I found a way to do it thanks this blog ! type D = class inherit

Why is my Trie lookup slower than that of the standard F# Map's?

帅比萌擦擦* 提交于 2019-12-07 08:19:09
问题 So, I just ported the Trie from OCaml. Unfortunately, it runs slower than the standard Map in terms of tryFind. I don't understand this - the trie seems like it should be faster. Is F#'s code libraries built in some special way as to make them faster than the code that the user typically deploys? Here's the code - [<RequireQualifiedAccess>] module Trie type Node<'k, 'v when 'k : comparison> = { TrieMap : Map<'k, Node<'k, 'v>> TrieKvp : ('k list * 'v) option } member inline x.IsEmpty = x

Lazily grouping a flat sequence in F#

拥有回忆 提交于 2019-12-07 08:12:55
问题 Given a sequence of items as follows: [ ("a", 1); ("a", 2); ("a", 3); ("b", 1); ("c", 2); ("c", 3) ] How can I convert this lazily into: { ("a", { 1; 2; 3}); ("b", { 1 }); ("c", { 2; 3}) } You can assume that the input data source is already sorted on the grouping key element e.g. "a" "b" and "c". I'm using the { } there to indicate that it's a lazily-evaluated sequence of items. I've gotten it working imperatively with two while loops operating over the IEnumerator of the source sequence,

What's a succinct, useful and efficient way to store large time-series in F#?

ε祈祈猫儿з 提交于 2019-12-07 08:01:15
问题 I'm currently learning F# and I'm exploring using it to analyse financial time-series. Can anyone recommend a good data structure to store time-series data in? F# offers a rich selection of native types and I'm looking for a some simple combination that would provide an elegant, succinct and efficient solution. I'm looking store tick data, which consists of millions of records each with a time stamp, and several (~5-20) fields of numerical and textual data, with possible missing values. My

F#, Visual Studio 2017 and dotnet new

无人久伴 提交于 2019-12-07 07:49:36
问题 To create a .NET class library from the command line, you can run the script dotnet new classlib Do that in a clean folder, and it will create a csproj file that can then be opened in Visual Studio 2017. However, run the script dotnet new classlib -lang f# in a clean folder, and the fsproj file that is subsequently create cannot be opened in Visual Studio 2017. The error message reads The imported project "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Sdks\FSharp

F# - “Not a valid property expression”

白昼怎懂夜的黑 提交于 2019-12-07 07:37:38
I'm having this method which takes a Expr as parameter: member x.HasSeq (expr:Expr<'a -> 'b seq>) = let casted = <@ fun z -> (%expr) z :?> ICollection<'b> @> ManyNavPropertyInfo(cfg.HasMany <| toLinq casted) What I want is to cast the 'b seq to an ICollection<'b> , which seems to work as it should, however when it reaches the line where it's going to convert the Expr to LINQ (need to do this since cfg.HasMany excepts a System.Expression<Func<'a,ICollection<'b>>> ) it simply throws an exception saying: InvalidOperationException: The expression 'z => UnboxGeneric(ToFSharpFunc(z => z.Books)

looping through F# record like Javascript object

荒凉一梦 提交于 2019-12-07 06:53:44
问题 In javascript, I can access every property of an object with a simple for loop as follows var myObj = {x:1, y:2}; var i, sum=0; for(i in myObj) sum = sum + myObj[i]; I am wondering if I can do similar thing with F#. type MyObj = {x:int; y:int} let myObj = {x=1; y=2} let allValues:seq<int> = allPropertyValuesIn myObj //How do I implement allPropertyValuesIn let sum = allValues |> Seq.fold (+) 0 Thank you for your input Edit to clarify why I want to do such thing I am working on an XML file

Code gen error or my misunderstanding?

坚强是说给别人听的谎言 提交于 2019-12-07 06:28:55
问题 I'm seeing behavior I can't explain from the F# compiler (Visual F# 3.1.1.0)--what appears on the surface to be only the difference between having a named local and passing a temporary actually produces a behavior difference. Am I not understanding something about F# behavior, or is this a code gen error? (I know, the latter is more likely.) Repro - I found it difficult to repro without using Reactive Extensions, so this is about as simple as I got it. Note that try1 and try2 are nearly

short-cutting equality checking in F#?

匆匆过客 提交于 2019-12-07 06:22:46
问题 In F#, the equality operator (=) is generally extensional, rather than intensional. That's great! Unfortunately, it appears to me that F# does not use pointer equality to short-cut these extensional comparisons. For instance, this code: type Z = MT | NMT of Z ref // create a Z: let a = ref MT // make it point to itself: a := NMT a // check to see whether it's equal to itself: printf "a = a: %A\n" (a = a) ... gives me a big fat segmentation fault[*], despite the fact that 'a' and 'a' both

Comparing Discriminated Unions

◇◆丶佛笑我妖孽 提交于 2019-12-07 06:13:37
问题 I'm a newbie to F# and I'm playing around with FParsec. I would use FParsec to generate an AST. I would like to use FsUnit to write some tests around the various parts of the parser to ensure correct operation. I'm having a bit of trouble with the syntax (sorry, the exact code is at work, I can post a specific example later) so how exactly could one compare two discriminated unions (one the expected, the other the actual result)? Could someone provide a tiny code example using FsUnit (or