Created
October 7, 2013 18:25
-
-
Save misterspeedy/6872627 to your computer and use it in GitHub Desktop.
Representing a hand of cards as an F# type.
This file contains hidden or 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 hand of exactly five cards. | |
| type Hand(cards : Card[]) = | |
| do | |
| if cards.Length <> 5 then | |
| raise (ArgumentException("Invalid number of cards")) | |
| /// The cards in the hand, sorted in descending order of rank. | |
| member h.Cards = cards |> Array.sortBy (fun card -> 0 - card.Rank.SortValue) | |
| static member FromString (s : string) = | |
| let cards = | |
| s.Split([|' '|]) | |
| |> Array.map (fun s' -> Card.FromString s') | |
| Hand cards |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment