f#

How to perform multiple styles of pattern matching?

╄→гoц情女王★ 提交于 2020-01-16 01:09:07
问题 Just started to play with F#. As terrible as I'm with it now, I do not to know to search for a similar thread too. This is what I'm trying to do: let test animal = if animal :? Cat //testing for type then "cat" elif animal :? Dog //testing for type then "dog" elif animal = unicorn //testing value equality then "impossible" else "who cares" Basically it involves type test pattern matching along with other conditional checks. I can get the first part (type checking) done like this: let test

Idiomatic way in F# to establish adherence to an interface on type rather than instance level

别等时光非礼了梦想. 提交于 2020-01-15 12:47:07
问题 What's the most idiomatic way in F# to deal with the following. Suppose I have a property I want a type to satisfy that doesn't make sense on an instance level, but ideally I would like to have some pattern matching available against it? To make this more concrete, I have defined an interface representing the concept of a ring (in the abstract algebra sense). Would I go for: 1. // Misses a few required operations for now type IRing1<'a when 'a: equality> = abstract member Zero: 'a with get

F# Sorting Sequence of Records where Value isn't yet assigned to Type in Sequence

心不动则不痛 提交于 2020-01-15 10:11:18
问题 I am trying to sort a Sequence of records by the second element in each record. The problem is that these are not a value and instead are just a Type. I have a function where it returns the value based on which Type it is. This is what I have: type Suit = Spades | Clubs | Hearts | Diamonds type Rank = Ace | Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten | Jack | Queen | King type Card = { suit: Suit; rank: Rank} type Hand = Card seq let cardValue(card:Card) = if (card.rank = Ace

Memoizing tail call optimized recursive functions in F# [duplicate]

左心房为你撑大大i 提交于 2020-01-15 09:34:29
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Combine memoization and tail-recursion So the following is the code that I wrote, tail call optimized using an accumulation variable let rec counter init count = if init = 1 then count + 1 else match init with | Even value -> (counter (value/2) (1 + count)) | Odd value -> (counter ((3 * value) + 1) (count+1)) let SeqBuilder (initval:int) : int = counter initval 0 How do I memoize this? the problem I ran into

Memoizing tail call optimized recursive functions in F# [duplicate]

≡放荡痞女 提交于 2020-01-15 09:34:05
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Combine memoization and tail-recursion So the following is the code that I wrote, tail call optimized using an accumulation variable let rec counter init count = if init = 1 then count + 1 else match init with | Even value -> (counter (value/2) (1 + count)) | Odd value -> (counter ((3 * value) + 1) (count+1)) let SeqBuilder (initval:int) : int = counter initval 0 How do I memoize this? the problem I ran into

Asynchronous EF query in F#

谁说我不能喝 提交于 2020-01-15 08:44:10
问题 In C# using EF6 I can easily make asynchronous operations like this: using (var context = new MyDbContext()) { var item = await context.SomeEntities.Where(e => e.Id == 1).FirstAsync(); DoSomething(item); } How could I do the same with F# async workflow? I am aware of query workflow and how to use it instead of LINQ, but I have no idea how to make it properly async (i.e. without permanent consumption of thread pool, like in a C# example). Here's what I got so far (it is synchronous): use

System.Version doesn't implement System.IComparable in F#

醉酒当歌 提交于 2020-01-15 08:29:10
问题 I want to sort a sequence of Version objects in F#: let maxVersion = versions |> Seq.max (fun version -> version) The compiler produces the following error message: The type '(seq -> 'a)' does not support the 'comparison' constraint. For example, it does not support the 'System.IComparable' interface When I hit F12 in Visual studio to take a look at the metadata of Version it says that Version only implements ICloneable , but not IComparable . But when I go to sourceof.net it says it

How to convert int64 to Nullable<int64>

人盡茶涼 提交于 2020-01-15 04:35:11
问题 This code: let mutable x : Nullable<int64> = new Nullable<int64> 99L let y : int64 = 88L x <- y produces this compile time error: This expression was expected to have type Nullable but here has type int64 I understand the error, what I would like to know is what is the correct way (cast?) to assign the value in y (88) to x ? 回答1: Use the System.Nullable constructor; for example: > let mutable x = System.Nullable (99L) let y = 88L x <- System.Nullable y;; val mutable x : Nullable<int64> = 88L

F# and MEF: Exporting Functions

不问归期 提交于 2020-01-15 04:15:32
问题 So, I was trying to get this simple test working in an F# console app: open System.Reflection open System.ComponentModel.Composition open System.ComponentModel.Composition.Hosting [<Export(typeof<int -> string>)>] let toString(i: int) = i.ToString() [<EntryPoint>] let main argv = use catalog = new AssemblyCatalog(Assembly.GetEntryAssembly()) use container = new CompositionContainer(catalog) let myFunction = container.GetExportedValue<int -> string>() let result = myFunction(5) 0 I expected

F# and MEF: Exporting Functions

做~自己de王妃 提交于 2020-01-15 04:15:27
问题 So, I was trying to get this simple test working in an F# console app: open System.Reflection open System.ComponentModel.Composition open System.ComponentModel.Composition.Hosting [<Export(typeof<int -> string>)>] let toString(i: int) = i.ToString() [<EntryPoint>] let main argv = use catalog = new AssemblyCatalog(Assembly.GetEntryAssembly()) use container = new CompositionContainer(catalog) let myFunction = container.GetExportedValue<int -> string>() let result = myFunction(5) 0 I expected