在F#中检索具有不同数量元素的元组数组

时间:2011-11-06 14:18:31

标签: f#

我有以下要求:根据第二个数组中元素的外观从第一个数组中获取元组数组:

let totals = [| ("old1", "new1"); ("old2", "new2");  ("old3", "new3"); ("old4", "new4")  |]
let changes = [| "new1"; "new4" |]

我想要这个:

let updates = [| ("old1", "new1"); ("old4", "new4") |]

如果两个数组的总数和变化长度相同,那么我认为这很容易:

let updates = Array.zip  changes totals
            |> Array.choose(fun (a, B) -> if a = fst(B) then Some (B) else None)

不幸的是,总数和变化的元素数量不同;因此,我找不到一种简单的方法来获得我需要的元素。

2 个答案:

答案 0 :(得分:6)

pad 发布的解决方案是正确的,适用于changes中的少量元素。但是,它会对changes中的每个元素遍历数组total,因此对于大型数组来说效率可能不高。

作为替代方案,您可以将changes转换为F#set类型,以便更有效地进行会员资格测试:

// Create set containing 'changes'
let changesSet = Set.ofArray changes

// Filter totals where the second element is in 'changesSet'
totals |> Array.filter (fun (_, v) -> changesSet.Contains(v))

// Same thing using function composition
totals |> Array.filter (snd >> changesSet.Contains)

答案 1 :(得分:3)

您应该在totals中选择在changes中出现第二个元素的对:

let updates = 
      totals 
      |> Array.filter (fun (_, x) -> changes |> Array.exists (fun y -> y = x))
相关问题