f#

How to get the type of each union case for a given union type in F#

僤鯓⒐⒋嵵緔 提交于 2019-12-11 07:46:54
问题 I am wondering in the F# code below how to fetch the type associated with each union case via reflection type AccountCreatedArgs = { Owner: string AccountId: Guid CreatedAt: DateTimeOffset StartingBalance: decimal } type Transaction = { To: Guid From: Guid Description: string Time: DateTimeOffset Amount: decimal } type AccountEvents = | AccountCreated of AccountCreatedArgs | AccountCredited of Transaction | AccountDebited of Transaction I tried using FSharpType.GetUnionCases(typeof

F# - fileReplace, overwriting words in txt, but when running it overwrites exisiting content in txt

筅森魡賤 提交于 2019-12-11 07:33:32
问题 open System //helpfunction let fileReplace filename needle replace = let replaceIn (reader:System.IO.StreamReader) needle (replace: String) = while not(reader.EndOfStream) do let mutable allText = reader.ReadToEnd() allText <- allText.Replace(needle, replace) reader.Close() //start let reader = System.IO.File.OpenText filename let newText = replaceIn reader needle replace let writer = System.IO.File.CreateText filename writer.Write newText ; writer.Close() // testing let filename = @".\abc

FSharpLint, how to use the rule “InterfaceNamesMustBeginWithI” in SuppressMessageAttribute?

寵の児 提交于 2019-12-11 07:26:53
问题 [<SuppressMessage("NameConventions","InterfaceNamesMustBeginWithI")>] //No effect [<SuppressMessage("NameConventions","InterfaceNames")>] //It's working module Test= type [<AllowNullLiteral>] MutationEvent = abstract attrChange: float with get, set ... Also, failed to search source code about "InterfaceNamesMustBeginWithI". 回答1: The name of the rule is InterfaceNames , so you can suppress it thus: [<SuppressMessage("","InterfaceNames")>] module Test = ... Also note that the first argument to

Unexpected keyword 'member' in definition, in a F# module

左心房为你撑大大i 提交于 2019-12-11 07:21:38
问题 Trying to get optional parameters working in a module I tried changing the let to member and static member but that yields errors. Error FS0010 Unexpected keyword 'member' in definition. Expected incomplete structured construct at or before this point or other token. OK syntax: module Kingdom = let Rule years = () Bad syntax module Kingdom = member this.Rule years = () Can you not define a member in a module? 回答1: As documentation states Members are features that are part of a type definition

Concatenating strings in F# number of times

十年热恋 提交于 2019-12-11 06:56:31
问题 I'm trying to concatenate a string with a certain amount of times, but I feel like I've cheated a bit (or at least not actually understood how it's supposed to be done) by using a higher-order function: let repeat s n = String.replicate n s |> printfn "%s" repeat "a" 10 Obviously gives me "aaaaaaaaaa", but how could I do this without a higher-order function? I feel like it's a very simple problem but I can't seem to wrap my head around it, the F# syntax, or way of thinking, is still

How to create a dataframe of multiple types with the RTypeProvider

大憨熊 提交于 2019-12-11 06:55:37
问题 It seems like the RTypeProvider can only handle namedParams of the same type. Is this the case? For example, open RDotNet open RProvider type foo = { Which: string Qty: float option } let someFoos = [{Which = "that"; Qty = Some 4.0}; {Which = "other"; Qty = Some 2.0}] let thingForR = namedParams [ "which", someFoos |> List.map (fun x -> x.Which); "qty", someFoos |> List.map (fun x -> x.Qty); ] |> R.data_frame doesn't work as I get an error on the x.Qty saying This expression was expected to

F#: type matching on DU cases, make this slightly more generic

蓝咒 提交于 2019-12-11 06:55:01
问题 In this previous question, there is a lovely solution to asking if an object is a particular union case: let isUnionCase (c : Expr<_ -> 'T>) = match c with | Lambdas (_, NewUnionCase(uci, _)) -> let tagReader = Microsoft.FSharp.Reflection.FSharpValue.PreComputeUnionTagReader(uci.DeclaringType) fun (v : 'T) -> (tagReader v) = uci.Tag | _ -> failwith "Invalid expression" which is great. If I have: type Dog = | Spaniel | Shepherd type Cat = | Tabby | Manx type Animal | Dog of Dog | Cat of Cat I

Get a cartesian-like product of a list of lists, with unequal lengths and a function ('a -> 'b list) is applied to each item

人走茶凉 提交于 2019-12-11 06:37:11
问题 I've been struggling with this for a while now. While I had some long imperative method that worked, I decided to redesign this part: Take a list of lists Make a list of the first item of each sublist, then again the first item, but from the last sublist the second item, then the third, until that last list is exhausted, do the same for the N-1 sublist, which basically gives a kind of product of all these lists In other words: [abc][FG][98] should be evaluated like (apply function f to each

Escaping Asterisk Grabs wrong character

大兔子大兔子 提交于 2019-12-11 06:33:35
问题 When I run the code below I get the unexpected result where \* also captures É . Is there a way to make it only capture * like I wanted? let s = "* A ÉR * B" let result = System.Text.RegularExpressions.Regex.Replace(s, "\n(?!\*)", "", Text.RegularExpressions.RegexOptions.Multiline) printfn "%s" result Result After Running Replace * AÉR * B Expected Result "* A ÉR * B" UPDATE This seems to be working, when I use a pattern like so \n(?=\*) . I guess I needed a positive lookahead . 回答1: You're

Deedle: errors using Frame.mapColValues

妖精的绣舞 提交于 2019-12-11 06:29:19
问题 I'm following a tutorial: http://dkowalski.com/blog/archive/2014/01/11/f-deedle-and-computational-investing.aspx and when I try to apply normalization to all the columns of "stocks" Frame, using Frame.mapColValues , I obtain the following error message: System.InvalidOperationException: OptionalValue.Value: Value is not available in Deedle.OptionalValue``1.get_Value() in c:\Tomas\Public\Deedle\src\Deedle\Common\Common.fs:riga 35 in FSI_0046.normalized@52-8.Invoke(ObjectSeries``1 os) in C: