Skip to content

Commit 124fd40

Browse files
authored
System.HashCode F# snippets (#7730)
* HashCode F# snippets * fix snippets
1 parent 3956181 commit 124fd40

File tree

6 files changed

+178
-0
lines changed

6 files changed

+178
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module example1
2+
3+
// <Snippet1>
4+
open System
5+
open System.Collections.Generic
6+
7+
[<Struct; CustomEquality; NoComparison>]
8+
type OrderOrderLine(orderId: int, orderLineId: int) =
9+
member _.OrderId = orderId
10+
member _.OrderLineId = orderLineId
11+
12+
override _.GetHashCode() =
13+
HashCode.Combine(orderId, orderLineId)
14+
15+
override this.Equals(obj) =
16+
match obj with
17+
| :? OrderOrderLine as o -> (this :> IEquatable<_>).Equals o
18+
| _ -> false
19+
20+
interface IEquatable<OrderOrderLine> with
21+
member _.Equals(other: OrderOrderLine) =
22+
orderId = other.OrderId && orderLineId = other.OrderLineId
23+
24+
let set =
25+
HashSet<OrderOrderLine> [ OrderOrderLine(1, 1); OrderOrderLine(1, 1); OrderOrderLine(1, 2) ]
26+
printfn $"Item count: {set.Count}."
27+
28+
// The example displays the following output:
29+
// Item count: 2.
30+
// </Snippet1>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module example2
2+
3+
// <Snippet1>
4+
open System
5+
open System.Collections.Generic
6+
7+
[<Struct; CustomEquality; NoComparison>]
8+
type Path([<ParamArray>]segments: string[]) =
9+
member _.Segments =
10+
Array.AsReadOnly segments
11+
12+
override this.Equals(obj) =
13+
match obj with
14+
| :? Path as o -> (this :> IEquatable<_>).Equals(o)
15+
| _ -> false
16+
17+
interface IEquatable<Path> with
18+
member this.Equals(other: Path) =
19+
Object.ReferenceEquals(this.Segments, other.Segments) ||
20+
not (isNull this.Segments) &&
21+
not (isNull other.Segments) &&
22+
this.Segments.Count = other.Segments.Count &&
23+
Seq.forall2 (=) this.Segments other.Segments
24+
25+
override this.GetHashCode() =
26+
let hash = HashCode()
27+
28+
for i = 0 to this.Segments.Count - 1 do
29+
hash.Add this.Segments[i]
30+
hash.ToHashCode()
31+
32+
let set =
33+
HashSet<Path> [
34+
Path("C:", "tmp", "file.txt")
35+
Path("C:", "tmp", "file.tmp")
36+
Path("C:", "tmp", "file.txt") ]
37+
38+
printfn $"Item count: {set.Count}."
39+
40+
// The example displays the following output:
41+
// Item count: 2.
42+
// </Snippet1>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module example3
2+
3+
// <Snippet1>
4+
open System
5+
open System.Collections.Generic
6+
7+
[<Struct; CustomEquality; NoComparison>]
8+
type Path([<ParamArray>]segments: string[]) =
9+
member _.Segments =
10+
Array.AsReadOnly segments
11+
12+
override this.Equals(obj) =
13+
match obj with
14+
| :? Path as o -> (this :> IEquatable<_>).Equals(o)
15+
| _ -> false
16+
17+
interface IEquatable<Path> with
18+
member this.Equals(other: Path) =
19+
Object.ReferenceEquals(this.Segments, other.Segments) ||
20+
not (isNull this.Segments) &&
21+
not (isNull other.Segments) &&
22+
this.Segments.Count = other.Segments.Count &&
23+
Seq.forall2 (fun x y -> String.Equals(x, y, StringComparison.OrdinalIgnoreCase)) this.Segments other.Segments
24+
25+
override this.GetHashCode() =
26+
let hash = HashCode()
27+
28+
for i = 0 to this.Segments.Count - 1 do
29+
hash.Add(this.Segments[i], StringComparer.OrdinalIgnoreCase)
30+
hash.ToHashCode()
31+
32+
let set =
33+
HashSet<Path> [
34+
Path("C:", "tmp", "file.txt")
35+
Path("C:", "tmp", "file.tmp")
36+
Path("C:", "tmp", "file.txt") ]
37+
38+
printfn $"Item count: {set.Count}."
39+
40+
// The example displays the following output:
41+
// Item count: 1.
42+
// </Snippet1>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
module example4
2+
3+
// <Snippet1>
4+
open System
5+
open System.Collections.Generic
6+
7+
module PlatformUtils =
8+
let pathEquals a b = String.Equals(a, b, StringComparison.OrdinalIgnoreCase)
9+
let addPath (hash: byref<HashCode>) path = hash.Add(path, StringComparer.OrdinalIgnoreCase)
10+
11+
[<Struct; CustomEquality; NoComparison>]
12+
type Path([<ParamArray>]segments: string[]) =
13+
member _.Segments =
14+
Array.AsReadOnly segments
15+
16+
override this.Equals(obj) =
17+
match obj with
18+
| :? Path as o -> (this :> IEquatable<_>).Equals(o)
19+
| _ -> false
20+
21+
interface IEquatable<Path> with
22+
member this.Equals(other: Path) =
23+
Object.ReferenceEquals(this.Segments, other.Segments) ||
24+
not (isNull this.Segments) &&
25+
not (isNull other.Segments) &&
26+
this.Segments.Count = other.Segments.Count &&
27+
Seq.forall2 PlatformUtils.pathEquals this.Segments other.Segments
28+
29+
override this.GetHashCode() =
30+
let mutable hash = HashCode()
31+
32+
for i = 0 to this.Segments.Count - 1 do
33+
PlatformUtils.addPath &hash this.Segments[i]
34+
hash.ToHashCode()
35+
36+
37+
let set =
38+
HashSet<Path> [
39+
Path("C:", "tmp", "file.txt")
40+
Path("C:", "TMP", "file.txt")
41+
Path("C:", "tmp", "FILE.TXT") ]
42+
43+
printfn $"Item count: {set.Count}."
44+
45+
// The example displays the following output:
46+
// Item count: 1.
47+
// </Snippet1>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="example1.fs" />
9+
<Compile Include="example2.fs" />
10+
<Compile Include="example3.fs" />
11+
<Compile Include="example4.fs" />
12+
</ItemGroup>
13+
</Project>

xml/System/HashCode.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ single hash code. This structure operates in one of two ways:
5353
The static methods combine the default hash codes of up to eight values.
5454
5555
:::code language="csharp" source="~/snippets/csharp/System/HashCode/Overview/example1.cs" id="Snippet1":::
56+
:::code language="fsharp" source="~/snippets/fsharp/System/HashCode/Overview/example1.fs" id="Snippet1":::
5657
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.hashcode.structure/vb/example1.vb" id="Snippet1":::
5758
5859
### Instance Methods
@@ -63,17 +64,20 @@ The static methods combine the default hash codes of up to eight values.
6364
The instance methods combine the hash codes of more than eight values.
6465
6566
:::code language="csharp" source="~/snippets/csharp/System/HashCode/Overview/example2.cs" id="Snippet1":::
67+
:::code language="fsharp" source="~/snippets/fsharp/System/HashCode/Overview/example2.fs" id="Snippet1":::
6668
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.hashcode.structure/vb/example2.vb" id="Snippet1":::
6769
6870
The instance methods also combine the hash codes produced by a specific
6971
<xref:System.Collections.Generic.IEqualityComparer%601> implementation.
7072
7173
:::code language="csharp" source="~/snippets/csharp/System/HashCode/Overview/example3.cs" id="Snippet1":::
74+
:::code language="fsharp" source="~/snippets/fsharp/System/HashCode/Overview/example3.fs" id="Snippet1":::
7275
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.hashcode.structure/vb/example3.vb" id="Snippet1":::
7376
7477
The <xref:System.HashCode> structure must be passed by-reference to other methods, as it is a value type.
7578
7679
:::code language="csharp" source="~/snippets/csharp/System/HashCode/Overview/example4.cs" id="Snippet1":::
80+
:::code language="fsharp" source="~/snippets/fsharp/System/HashCode/Overview/example4.fs" id="Snippet1":::
7781
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.hashcode.structure/vb/example4.vb" id="Snippet1":::
7882
7983
]]></format>

0 commit comments

Comments
 (0)