F#: Accessing values of decision Union

Viewed 48

Is there a better way or an alternative for the getHandRankValue function? Maybe without the match

type HandRank =
    | StraightFlush of int list
    | FourOfAKind of int list
    | FullHouse of int list
    | Flush of int list
    | Straight of int list
    | ThreeOfAKind of int list
    | TwoPair of int list
    | OnePair of int list
    | HighCard of int list
    
let getHandRankValue handRank =
    match handRank with
    | StraightFlush x -> x
    | FourOfAKind x -> x
    | FullHouse x -> x
    | Flush x -> x
    | Straight x -> x
    | ThreeOfAKind x -> x
    | TwoPair x -> x
    | OnePair x -> x
    | HighCard x -> x

let ranks = [Flush([8;6;4;3;2]); TwoPair([8;4;7]); Straight([8])]

let rankValues = ranks |> List.map getHandRankValue

result is [[8;6;4;3;2]; [8;4;7]; [8]]

1 Answers

I agree with the suggestion to factor out the int list from your type. Barring that, you can also simplify your getHandRankValue function somewhat, as follows:

let getHandRankValue = function
    | StraightFlush x
    | FourOfAKind x
    | FullHouse x
    | Flush x
    | Straight x
    | ThreeOfAKind x
    | TwoPair x
    | OnePair x
    | HighCard x -> x

I've made two changes here:

Related