f#

Object initialization syntax

蓝咒 提交于 2019-12-28 08:07:30
问题 I'm just starting out with F# and I can't find the syntax to do object initialization like in C# 3. I.e. given this: public class Person { public DateTime BirthDate { get; set; } public string Name { get; set; } } how do I write the following in F#: var p = new Person { Name = "John", BirthDate = DateTime.Now }; 回答1: You can do it like this: let p = new Person (Name = "John", BirthDate = DateTime.Now) 回答2: the answer from CMS is definitely correct. Here is just one addition that may be also

Object initialization syntax

非 Y 不嫁゛ 提交于 2019-12-28 08:07:08
问题 I'm just starting out with F# and I can't find the syntax to do object initialization like in C# 3. I.e. given this: public class Person { public DateTime BirthDate { get; set; } public string Name { get; set; } } how do I write the following in F#: var p = new Person { Name = "John", BirthDate = DateTime.Now }; 回答1: You can do it like this: let p = new Person (Name = "John", BirthDate = DateTime.Now) 回答2: the answer from CMS is definitely correct. Here is just one addition that may be also

How to generically remove F# Units of measure

China☆狼群 提交于 2019-12-28 07:02:53
问题 I've got some data manipulation code which spits out csv at the end. I started upgrading it to add units of measure everywhere, but I now have a problem with my csv function: val WriteCSV : string -> 'a list array -> 'b list -> string -> unit (the parameters are fileName, column array, column headers, separator) Where I previously sent [|s;x;y|] to WriteCSV, I now have a problem, because I can't send [|skm; xmm; ymm|]. I tried writing a function for generically removing units of measure, but

Implementing the same interface at different generic instantiations

浪尽此生 提交于 2019-12-28 06:29:05
问题 In C#, I can implement a generic interface twice on one class, using two different type-parameters: interface IFoo<T> { void Foo(T x); } class Bar : IFoo<int>, IFoo<float> { public void Foo(int x) { } public void Foo(float y) { } } I would like to do the same thing in F#: type IFoo<'a> = abstract member Foo : 'a -> unit type Bar() = interface IFoo<int> with [<OverloadID("int")>] member this.Foo x = () interface IFoo<float> with [<OverloadID("float")>] member this.Foo x = () But it gives a

F# how to pass equivalent of interface

青春壹個敷衍的年華 提交于 2019-12-28 05:14:13
问题 I know I will laugh when I see this answer, but for some reason I don't see it. For some reason it is eluding me how to pass multiple func in one parameter (for lack of better words.) For instance, lets say I have IDoSomething that has 3 methods: 1.) DoIt() 2.) DoItMore() 3.) DoItMost() in OO, I would do this: type MyController(something:IDoSomething) = let a = something.DoIt() let b = something.DoItMore() let c = something.DoItMost() So, for F# I would have a module with the 3 functions

F# and type inference: “int list” does not support “+” [closed]

血红的双手。 提交于 2019-12-27 06:17:08
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . Why does the following generate an error? let mult a b = a * b let sum a b = a + b The second let generates a compile error: Type constraint mismatch when applying the default type 'int list' for a type inference

F# and type inference: “int list” does not support “+” [closed]

…衆ロ難τιáo~ 提交于 2019-12-27 06:16:44
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . Why does the following generate an error? let mult a b = a * b let sum a b = a + b The second let generates a compile error: Type constraint mismatch when applying the default type 'int list' for a type inference

F# and type inference: “int list” does not support “+” [closed]

帅比萌擦擦* 提交于 2019-12-27 06:15:55
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . Why does the following generate an error? let mult a b = a * b let sum a b = a + b The second let generates a compile error: Type constraint mismatch when applying the default type 'int list' for a type inference

How do I get the max value from a list with a function that takes two arguments?

你离开我真会死。 提交于 2019-12-25 19:41:12
问题 Define the function max2 that takes two integers as arguments and returns the largest of them. I did this: let max2 x y = if x < y then y else x this I belive is correct Then define the function max_list that returns the largest of the elements in a nonempty list of integers by calling max2. For the empty list, it should abort with an error message ( raising an exception) I did this: let list = [3;4] let max_list = if list.IsEmpty then 0 else max2 list.Item(0) list.Item(1) but this wont work

How do I get the max value from a list with a function that takes two arguments?

梦想的初衷 提交于 2019-12-25 19:41:06
问题 Define the function max2 that takes two integers as arguments and returns the largest of them. I did this: let max2 x y = if x < y then y else x this I belive is correct Then define the function max_list that returns the largest of the elements in a nonempty list of integers by calling max2. For the empty list, it should abort with an error message ( raising an exception) I did this: let list = [3;4] let max_list = if list.IsEmpty then 0 else max2 list.Item(0) list.Item(1) but this wont work