1+ module equals2
2+
3+ // <Snippet2>
4+ open System
5+ open System.Collections
6+
7+ type Item2Comparer < 'T1 , 'T2 , 'T3 when 'T1: equality and 'T2: equality and 'T3: equality >() =
8+ interface IEqualityComparer with
9+ member _.Equals ( x : obj , y : obj ) =
10+ // Return true for all values of Item1.
11+ match x with
12+ | :? 'T1 ->
13+ true
14+ | :? 'T2 ->
15+ x.Equals y
16+ | _ -> true
17+
18+ member _.GetHashCode ( obj : obj ) =
19+ match obj with
20+ | :? 'T1 as obj->
21+ obj.GetHashCode()
22+ | :? 'T2 as obj ->
23+ obj.GetHashCode()
24+ | _ ->
25+ ( obj :?> 'T3) .GetHashCode()
26+
27+ let scores =
28+ [| Tuple.Create( " Ed" , 78.8 , 8 )
29+ Tuple.Create( " Abbey" , 92.1 , 9 )
30+ Tuple.Create( " Jim" , 71.2 , 9 )
31+ Tuple.Create( " Sam" , 91.7 , 8 )
32+ Tuple.Create( " Sandy" , 71.2 , 5 )
33+ Tuple.Create( " Penelope" , 82.9 , 8 )
34+ Tuple.Create( " Serena" , 71.2 , 9 )
35+ Tuple.Create( " Judith" , 84.3 , 9 ) |]
36+
37+ for ctr = 0 to scores.Length - 1 do
38+ let score : IStructuralEquatable = scores[ ctr]
39+ for ctr2 = ctr + 1 to scores.Length - 1 do
40+ printfn $" {score} = {scores[ctr2]}: {score.Equals(scores[ctr2], Item2Comparer<string, double, int>())}"
41+ printfn " "
42+ // The example displays the following output:
43+ // (Ed, 78.8, 8) = (Abbey, 92.1, 9): False
44+ // (Ed, 78.8, 8) = (Jim, 71.2, 9): False
45+ // (Ed, 78.8, 8) = (Sam, 91.7, 8): False
46+ // (Ed, 78.8, 8) = (Sandy, 71.2, 5): False
47+ // (Ed, 78.8, 8) = (Penelope, 82.9, 8): False
48+ // (Ed, 78.8, 8) = (Serena, 71.2, 9): False
49+ // (Ed, 78.8, 8) = (Judith, 84.3, 9): False
50+ //
51+ // (Abbey, 92.1, 9) = (Jim, 71.2, 9): False
52+ // (Abbey, 92.1, 9) = (Sam, 91.7, 8): False
53+ // (Abbey, 92.1, 9) = (Sandy, 71.2, 5): False
54+ // (Abbey, 92.1, 9) = (Penelope, 82.9, 8): False
55+ // (Abbey, 92.1, 9) = (Serena, 71.2, 9): False
56+ // (Abbey, 92.1, 9) = (Judith, 84.3, 9): False
57+ //
58+ // (Jim, 71.2, 9) = (Sam, 91.7, 8): False
59+ // (Jim, 71.2, 9) = (Sandy, 71.2, 5): True
60+ // (Jim, 71.2, 9) = (Penelope, 82.9, 8): False
61+ // (Jim, 71.2, 9) = (Serena, 71.2, 9): True
62+ // (Jim, 71.2, 9) = (Judith, 84.3, 9): False
63+ //
64+ // (Sam, 91.7, 8) = (Sandy, 71.2, 5): False
65+ // (Sam, 91.7, 8) = (Penelope, 82.9, 8): False
66+ // (Sam, 91.7, 8) = (Serena, 71.2, 9): False
67+ // (Sam, 91.7, 8) = (Judith, 84.3, 9): False
68+ //
69+ // (Sandy, 71.2, 5) = (Penelope, 82.9, 8): False
70+ // (Sandy, 71.2, 5) = (Serena, 71.2, 9): True
71+ // (Sandy, 71.2, 5) = (Judith, 84.3, 9): False
72+ //
73+ // (Penelope, 82.9, 8) = (Serena, 71.2, 9): False
74+ // (Penelope, 82.9, 8) = (Judith, 84.3, 9): False
75+ //
76+ // (Serena, 71.2, 9) = (Judith, 84.3, 9): False
77+ // </Snippet2>
0 commit comments