f#

How to define type member constant in F#?

天大地大妈咪最大 提交于 2019-12-19 13:57:12
问题 In C# one can define a type member constant like this: class Foo { public const int Bar = 600; } The IL looks like this. .field public static literal int32 Bar = int32(600) How can I do the same within Visual F# / FSharp? I tried this to no avail: [<Sealed>] type Foo() = [<Literal>] let Bar = 600 回答1: I did a couple of experiments with the F# compiler and here are some my observations. If you want to create IL literal, then you need to place the value marked as a Literal inside a module. For

scripts don't recognize FSharp.Data

天大地大妈咪最大 提交于 2019-12-19 13:38:13
问题 Somewhat of a F# beginner. I'm trying to test out some of my XmlTypeProvider code in the interactive window by first entering it in a script (fsx) file. The script file won't recognize the following open FSharp.Data // gives "The namespace or module 'FSharp' is not defined" Everything has been added to reference, and .fs files seem to not have any problems finding the XmlTypeProvider reference but for some reason, a script in the same project does not. I even got the code to work in a .fs

How to enumerate an enum/type in F#

那年仲夏 提交于 2019-12-19 12:50:12
问题 I've got an enumeration type defined like so: type tags = | ART = 0 | N = 1 | V = 2 | P = 3 | NULL = 4 is there a way to do a for ... in tags do ? This is the error that I'm getting: The value, constructor, namespace or type tags is not defined 回答1: Use Enum.GetValues: let allTags = Enum.GetValues(typeof<tags>) 回答2: Here is a complete example that prints information about any discriminated union. It shows how to get cases of the discriminated union and also how to get the fields (in case you

C# object initialization syntax in F#

你离开我真会死。 提交于 2019-12-19 12:38:11
问题 Please note: this question is not the same as this question. I recently came across some C# syntax I hadn't previously encountered: Is there any way to do this in F#? class Two { public string Test { get; set; } } class One { public One() { TwoProperty = new Two(); } public Two TwoProperty { get; private set; } } var test = new One(){ TwoProperty = { Test = "Test String" }}; (note the initialization of TwoProperty in the initializer when the setter is private - it is setting a property on the

FSI WPF Event Loop

自古美人都是妖i 提交于 2019-12-19 11:21:17
问题 Is the WPF event loop in this answer still a good one for FSI (besides rethrow which is now reraise )? The answer is from 2008 and I'm not sure if there are any "better" implementations around. If not what would one be? My understanding is that the default implementation is for WinForms. 回答1: Yes the default is for Winforms, I do use the WpfEventLoop quite a lot, Code is below, #I "c:/Program Files/Reference Assemblies/Microsoft/Framework/v3.0" #I "C:/WINDOWS/Microsoft.NET/Framework/v3.0/WPF/

Fparsec recursive grammatics throw StackOverflowException

假装没事ソ 提交于 2019-12-19 11:04:15
问题 I've got this code type Exprs = | Val of float | Mult of Exprs * Exprs | Plus of Exprs * Exprs let pexpr, exprRef = createParserForwardedToRef<Exprs, unit>() let pval = pfloat |>> Val let binaryOp s = (ws >>. pexpr.>> ws) .>>. (ws >>. str s >>. ws >>. pexpr) let pplus = binaryOp "+" |>> Plus let pmuil = binaryOp "*" |>> Mult do exprRef := choice [ attempt pmuil pplus pval ] let expression = ws >>. pexpr .>> ws When it evaluated it throws StackoverflowExcpetion. So the question is how can i

How to refactor code to make it functional style?

▼魔方 西西 提交于 2019-12-19 09:46:54
问题 Playing with F#, I'm trying to think of code in a more functional way. A large part of my work happens to be numerical in nature so I'm thinking whether this re-education makes sense. Is writing numerical code in a functional way like trying to fit a square peg in a round hole or is it simply a matter of a steep learning curve irrespective of the application? For example, lets take a snippet which demonstrates the weak law of large numbers: open System open System.IO open System.Windows.Forms

F# compiler error FS0030, problems with the Value Restriction

雨燕双飞 提交于 2019-12-19 09:37:58
问题 I've read the blurb at StrangeLights, I've read the passage from Expert F# (page 119), but I can't see how they apply to my code: For my tests, I want to check equality between floats, with a bit of tolerance. I'm converting everything to units of measure, but I want to be able to be 'generic': let toleq (e:float<_>) a b = (abs ( a - b ) ) < e I can then use this to check equality on different 'types' of float, or curry it to make a custom one: toleqm = toleq 1.0e-10<m> But I get the

Running a batch file with parameters in Python OR F#

白昼怎懂夜的黑 提交于 2019-12-19 09:21:12
问题 I searched the site, but I didn't see anything quite matching what I was looking for. I created a stand-alone application that uses a web service I created. To run the client I use: C:/scriptsdirecotry> "run-client.bat" param1 param2 param3 param4 How would I go about coding this in Python or F#. It seems like it should be pretty simple, but I haven't seen anything online that quite matches what I'm looking for. 回答1: Python is similar. import os os.system("run-client.bat param1 param2") If

Generic F# function: How to get the Type of an F# Discriminated Union?

烂漫一生 提交于 2019-12-19 08:30:41
问题 Code example: http://www.tryfsharp.org/create/dutts/Generics.fsx I have some mapping code in my F# which takes a C# object and wraps it in a discriminated union. module MyModule = type MappedThings = | DoThings of External.Things.DoThings type MappedStuff = | DoStuff of External.Stuff.DoStuff As I always use the same name in my discriminated union as the external object I would like to try to make my mapping code generic for scalability. This is what I've tried so far: let toDomain<'T>