F#排序记录序列,其中值尚未分配给序列中的类型 [英] F# Sorting Sequence of Records where Value isn't yet assigned to Type in Sequence
问题描述
我正在尝试按每个记录中的第二个元素对记录序列进行排序.问题是这些不是值,而只是类型.我有一个函数,它根据它是哪种类型返回值.
I am trying to sort a Sequence of records by the second element in each record. The problem is that these are not a value and instead are just a Type. I have a function where it returns the value based on which Type it is.
这就是我拥有的:
type Suit = Spades | Clubs | Hearts | Diamonds
type Rank = Ace | Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten | Jack | Queen | King
type Card = { suit: Suit; rank: Rank}
type Hand = Card seq
let cardValue(card:Card) =
if (card.rank = Ace) then 1
elif (card.rank = Two) then 2
elif (card.rank = Three) then 3
elif (card.rank = Four) then 4
elif (card.rank = Five) then 5
elif (card.rank = Six) then 6
elif (card.rank = Seven) then 7
elif (card.rank = Eight) then 8
elif (card.rank = Nine) then 9
elif (card.rank = Ten) then 10
elif (card.rank = Jack) then 10
elif (card.rank = Queen) then 10
elif (card.rank = King) then 10
else 0
let sortHandByValue(hand:Hand) =
......missing code here......
我想做的是按等级对Hand进行排序.
and what I am trying to do is sort Hand by the Rank as a value.
例如,Hand当前为:{{Hearts;三};{黑桃;杰克};{钻石;两个}}
So for example Hand is currently : {{Hearts; Three}; {Spades; Jack}; {Diamonds; Two}}
它将对Hand进行排序,因此结果为:{{Diamonds;二};{心;三};{黑桃;杰克}}
It will sort Hand so the result is : {{Diamonds; Two}; {Hearts; Three}; {Spades; Jack}}
我尝试做手|> Seq.sort |> Seq.groupBy id |> Seq.map snd,但这并没有按值和仅按字母顺序对其进行排序.
I tried doing hand |> Seq.sort |>Seq.groupBy id |> Seq.map snd but that doesn't sort it by value and only by alphabetical.
我无法更改任何类型,但可以更改其他所有内容.任何想法将不胜感激,谢谢!
I cannot change any of the types, but I can change everything else. Any ideas would be appreciated, thanks!
推荐答案
您需要对显示记录的代码进行一些清理,我将Card类型重写为元组,并且将match函数也重写了.然后,您所需要做的就是将它插入 Seq.sortBy
或 Seq.sortByDescending
(按升序排序):
You need to clean up the code a bit as it shows records, I rewrote the Card type to be a tuple and the match function as well. Then all you needs is to pipe into Seq.sortBy
or Seq.sortByDescending
(as sortby sorts ascending):
type Suit = Spades | Clubs | Hearts | Diamonds
type Rank = Ace | Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten | Jack | Queen | King
type Card = Suit * Rank
type Hand = Card seq
let cardValue (card:Card) =
match card with
| _, Ace -> 1
| _, Two -> 2
| _, Three -> 3
| _, Four -> 4
| _, Five -> 5
| _, Six -> 6
| _, Seven -> 7
| _, Eight -> 8
| _, Nine -> 9
| _, Ten | _, Jack | _, Queen | _, King -> 10
let hand = seq [(Hearts, Three); (Spades, Jack); (Diamonds, Two)]
hand |> Seq.sortBy cardValue
//val it : seq<Suit * Rank> =
//seq [(Diamonds, Two); (Hearts, Three); (Spades, Jack)]
包含记录的版本:我将其保留为接近您的原始照片.
Version with records: I kept this close to your original.
/// Version with Records
type Suit = Spades | Clubs | Hearts | Diamonds
type Rank = Ace | Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten | Jack | Queen | King
type Card = { suit: Suit; rank: Rank}
type Hand = seq<Card>
let cardValue(card:Card) =
if (card.rank = Ace) then 1
elif (card.rank = Two) then 2
elif (card.rank = Three) then 3
elif (card.rank = Four) then 4
elif (card.rank = Five) then 5
elif (card.rank = Six) then 6
elif (card.rank = Seven) then 7
elif (card.rank = Eight) then 8
elif (card.rank = Nine) then 9
elif (card.rank = Ten) then 10
elif (card.rank = Jack) then 10
elif (card.rank = Queen) then 10
elif (card.rank = King) then 10
else 0
let hand = seq [{suit=Hearts; rank=Three}; {suit=Spades;rank=Jack}; {suit=Diamonds;rank= Two}]
hand |> Seq.sortBy cardValue
验证:seq = seq [{suit = Diamonds;等级= 2;};{suit = Hearts;等级=三;};{suit =黑桃;等级=杰克;}]
val it : seq = seq [{suit = Diamonds; rank = Two;}; {suit = Hearts; rank = Three;}; {suit = Spades; rank = Jack;}]
这篇关于F#排序记录序列,其中值尚未分配给序列中的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!