我想做一个hashset
of (int*int)[]
,如下
let mySet = new HashSet<_>()
因为我认为 and 的默认比较器array
是tuple
,HashIdentity.Structural
它会自动满足我的需求。
但是它不起作用。这是我的实验:
let mySet = new HashSet<_>()
let a = [|1;2|]
let b = [|1;2|]
let c = compare a b
mySet.Add(a)
mySet.Add(b)
val a : int [] = [|1; 2|]
val b : int [] = [|1; 2|]
val c : int = 0
val it : HashSet<int []> = seq [[|1; 2|]; [|1; 2|]]
let mySet = new HashSet<_>()
let a = [1;2]
let b = [1;2]
let c = compare a b
mySet.Add(a)
mySet.Add(b)
val a : int list = [1; 2]
val b : int list = [1; 2]
val c : int = 0
val it : HashSet<int list> = seq [[1; 2]]
正如我们所见,arrayIEqualityComparer
的默认值不是,但list是。但是,默认值对它们都是结构性的。HashIdentity.Structural
IComparer
这有点奇怪,为什么?还如何通过使用和IEqualityComparer
的默认结构比较为我的哈希集创建一个。tuple
array
我知道如何在 C# 中手动完成,但由于我刚开始学习 F#,有人可以帮忙吗?
以下代码是我的努力:
let a = [|(1,2);(2,3)|]
let b = [|(1,2);(2,3)|]
type MyEqualityComparer() =
interface IEqualityComparer<(int*int)[]> with
member this.Equals (a,b) = (Array.forall2 (=) a b)
member this.GetHashCode (a) = hash (a |> Array.map hash)