f#

seeing C# windows forms project code from F#

99封情书 提交于 2019-12-06 06:22:34
问题 I have a C# Windows Forms project open with some C# code in it. Question: How can I have an F# file that I can write F# code in but still referencing all the C# code I have on Form1.cs (including the GUI). I can successfully do this: - Create a C# Windows Forms project - Create a F# Library project - Reference the F# Library DLL from my C# project - That way I can call F# functions from C# But I still can't see my buttons and textboxes from F# I understand that that is because it's a library

Discriminated union structural/custom equality

和自甴很熟 提交于 2019-12-06 06:20:55
I have the following discriminated union: type ActCard = Cellar of card list | Chapel of (card option * card option* card option* card option) | Smithy | Spy of (card -> bool * card -> bool) It had structural equality until I added the card -> bool to Spy . This question is helpful for how to do custom equality for records. However, I'm not sure how best to implement it in this situation. I would prefer to not have to enumerate each case in ActCard : override x.Equals(yobj) = match x, yobj with | Spy _, Spy _ -> true | Cellar cards, Cellar cards2 -> cards = cards2 (* ... etc *) What is a

Parametric Polymorphism vs Subtype polymorphism F#

笑着哭i 提交于 2019-12-06 06:11:53
What is the difference (if any) between these two F# type signatures? UseTheStream<'a when 'a :> Stream> : 'a -> unit and UseTheStream : (stream : Stream) -> unit Do they mean the same thing in this case? msdn says the following about the (:>) Type Constraint type-parameter :> type -- The provided type must be equal to or derived from the type specified, or, if the type is an interface, the provided type must implement the interface. This would indicate that the two signatures are saying the same thing. So Functionally, how are they different? Daniel They are different. Most importantly, the

Error using bool.Parse on null/empty values

眉间皱痕 提交于 2019-12-06 06:06:56
I have an expression using pipe operator that converts the value to string and then to bool, however sometimes the original value can be null. How can I use the pattern matching or something else to assume false when the value is null? type kv = Dictionary<string, obj> let allDayEvent (d: kv) = d.["fAllDayEvent"] |> string |> bool.Parse Anton Schwaighofer There's quite a few places where you can safeguard via pattern matching: dictionary lookup, casting, parsing. Here's an example with all of those: let allDayEvent (d: kv) = match d.TryGetValue "fAllDayEvent" with | true, v -> match v with |

F# A type parameter is missing a constraint

故事扮演 提交于 2019-12-06 05:57:57
问题 I'm trying to define a generic addition operator for a wrapper class. So far I have this: (simplified from the actual code) type Wrap<'a> = | Wrap of 'a static member inline (+) (Wrap x, Wrap y) = Wrap (x + y) let inline addSelf x = x + x and indeed it works: let i = addSelf (Wrap 1) // returns Wrap 2 let f = addSelf (Wrap 1.) // returns Wrap 2.0 but the following alternative to addSelf does not compile let inline addSelf' (Wrap x) = (Wrap x) + (Wrap x) // compile error giving error FS0193: A

Visual Studio 2010 IntelliSense: hints on F# operators

杀马特。学长 韩版系。学妹 提交于 2019-12-06 05:51:15
问题 Is it possible to make Visual Studio to display tooltips on operators? The following image demonstrates a tooltip hint for a function, but it does not work for operators. Operators usually have simple type specs like 'T -> 'T -> 'T , but such hints can be useful for custom ones. 回答1: Following Daniel's suggestion, I'm posting a workaround that I've been using for myself. The workaround is only partially helpful, and I'm still looking for any better ideas. let (!><) a = () let z1 = op

Constructor on public record type?

核能气质少年 提交于 2019-12-06 05:43:29
问题 Let's say I want a record type such as: type CounterValues = { Values: (int) list; IsCorrupt: bool } The thing is, I want to create a constructor that converts the list passed of integers to a new list which has no negative values (they would be replaced by 0s), and have IsCorrupt=true only if there were negative values found at construction time. Is this possible with F#? For now, this is what I've done, using properties (but, meh, it's not very F#-ish and it calls

making functions inline avoids closure?

早过忘川 提交于 2019-12-06 05:31:33
问题 I am reading a blogpost at: http://flyingfrogblog.blogspot.com/2009/07/ocaml-vs-f-burrows-wheeler.html a simple implementation for Burrow Wheeler compressing algorithm: # compare two strings str[i..end,0..i-1] and str[j..end,0..j-1] let cmp (str: _ array) i j = let rec cmp i j = if i=str.Length then 1 else if j=str.Length then -1 else let c = compare str.[i] str.[j] in if c<>0 then c else cmp (i+1) (j+1) cmp i j # sort n strings let bwt (str: byte array) = let n = str.Length let a = Array

Working with Events in F#

£可爱£侵袭症+ 提交于 2019-12-06 04:59:46
问题 I recently asked this question: Replay Recorded Data Stream in F# and combined that code with a subset of the functionality I found here: http://www.mattssoftwareblog.com/?p=271 which combined looks like this: #r "System.Reactive" #r "System.CoreEx" #r "FSharp.PowerPack" #r "WindowsBase" #r "PresentationCore" #r "PresentationFramework" #r "System.Xaml" #r "System.Interactive.dll" open System open System.Linq open System.Collections.Generic open System.Net open System.IO open System.Threading

F#, why can't I access the “Item” member

匆匆过客 提交于 2019-12-06 04:55:30
问题 In F#, why can't I access the "Item" member on the Array here: let last (arr:System.Array) = let leng = arr.Length arr.[leng-1] // Error: Field, constructor or member "Item" is not defined. 回答1: Can you try this? let last (arr:_[]) = let leng = arr.Length arr.[leng-1] 回答2: This seems to be a general dotnet thing. Looking up the documentation I see The Array class is the base class for language implementations that support arrays. However, only the system and compilers can derive explicitly