Created
October 7, 2013 18:23
-
-
Save misterspeedy/6872596 to your computer and use it in GitHub Desktop.
Representing a card rank as a Discriminated Union
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// A card rank. | |
type Rank = | |
| Ace | King | Queen | Jack | Value of int | |
static member FromString s = | |
match s with | |
| "A" -> Ace | "K" -> King | "Q" -> Queen | "J" -> Jack | |
| "10" -> Value(10) | |
| _ when s.Length = 1 && s.[0] >= '2' && s.[0] <= '9' -> Value(int(s.[0]) - int('0')) | |
| _ -> raise (ArgumentException(sprintf "Invalid rank string: %s" s)) | |
/// The value of the rank for comparison purposes. | |
member r. SortValue = | |
match r with | |
| Ace -> 14 // Aces high - low-ace cases are handled elsewhere | |
| King -> 13 | |
| Queen -> 12 | |
| Jack -> 11 | |
| Value v -> v |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment