如何在不使用引用的情况下按F#顺序删除重复项 [英] How can I remove duplicates in an F# sequence without using references

查看:70
本文介绍了如何在不使用引用的情况下按F#顺序删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个排序的序列,想遍历该序列并返回序列中的唯一条目.我可以使用以下函数来完成此操作,但是它使用引用变量,并且我认为这不是解决问题的正确方法.

I have a sorted sequence and want to go through it and return the unique entries in the sequence. I can do it using the following function, but it uses reference variables and I don't think it's the correct way of solving the problem.

    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
        }

我该如何以功能性方式做到这一点?

How can I do this in a functional way?

推荐答案

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

这篇关于如何在不使用引用的情况下按F#顺序删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆