F#:访问 decision Union 的值
F#: Accessing values of decision Union
getHandRankValue
函数是否有更好的方法或替代方法?
也许没有比赛
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
结果是 [[8;6;4;3;2]; [8;4;7]; [8]]
我同意从您的类型中提取 int list
的建议。除此之外,您还可以稍微简化 getHandRankValue
函数,如下所示:
let getHandRankValue = function
| StraightFlush x
| FourOfAKind x
| FullHouse x
| Flush x
| Straight x
| ThreeOfAKind x
| TwoPair x
| OnePair x
| HighCard x -> x
我在这里做了两处修改:
- 将
match handRank
替换为 pattern matching function。
- 删除了多余的
-> x
表达式。
getHandRankValue
函数是否有更好的方法或替代方法?
也许没有比赛
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
结果是 [[8;6;4;3;2]; [8;4;7]; [8]]
我同意从您的类型中提取 int list
的建议。除此之外,您还可以稍微简化 getHandRankValue
函数,如下所示:
let getHandRankValue = function
| StraightFlush x
| FourOfAKind x
| FullHouse x
| Flush x
| Straight x
| ThreeOfAKind x
| TwoPair x
| OnePair x
| HighCard x -> x
我在这里做了两处修改:
- 将
match handRank
替换为 pattern matching function。 - 删除了多余的
-> x
表达式。