gpt4 book ai didi

f# - 如何在不使用引用的情况下删除 F# 序列中的重复项

转载 作者:行者123 更新时间:2023-11-30 23:49:22 24 4
gpt4 key购买 nike

我有一个排序的序列,想要遍历它并返回序列中的唯一条目。我可以使用以下函数来做到这一点,但它使用引用变量,我认为这不是解决问题的正确方法。

    let takeFirstCell sectors = 
let currentRNCId = ref -1
let currentCellId = ref -1
seq {
for sector in sectors do
if sector.RNCId <> !currentRNCId || sector.CellId <> !currentCellId then
currentRNCId := sector.RNCId
currentCellId := sector.CellId
yield sector
}

我怎样才能以功能方式做到这一点?

最佳答案

[1;1;1;2;2;2;3;3;3]
|> Seq.distinctBy id
|> printfn "%A"

关于f# - 如何在不使用引用的情况下删除 F# 序列中的重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6842466/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com