Skip to content

System.HashCode F# snippets #7730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions snippets/fsharp/System/HashCode/Overview/example1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module example1

// <Snippet1>
open System
open System.Collections.Generic

[<Struct; CustomEquality; NoComparison>]
type OrderOrderLine(orderId: int, orderLineId: int) =
member _.OrderId = orderId
member _.OrderLineId = orderLineId

override _.GetHashCode() =
HashCode.Combine(orderId, orderLineId)

override this.Equals(obj) =
match obj with
| :? OrderOrderLine as o -> (this :> IEquatable<_>).Equals o
| _ -> false

interface IEquatable<OrderOrderLine> with
member _.Equals(other: OrderOrderLine) =
orderId = other.OrderId && orderLineId = other.OrderLineId

let set =
HashSet<OrderOrderLine> [ OrderOrderLine(1, 1); OrderOrderLine(1, 1); OrderOrderLine(1, 2) ]
printfn $"Item count: {set.Count}."

// The example displays the following output:
// Item count: 2.
// </Snippet1>
42 changes: 42 additions & 0 deletions snippets/fsharp/System/HashCode/Overview/example2.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module example2

// <Snippet1>
open System
open System.Collections.Generic

[<Struct; CustomEquality; NoComparison>]
type Path([<ParamArray>]segments: string[]) =
member _.Segments =
Array.AsReadOnly segments

override this.Equals(obj) =
match obj with
| :? Path as o -> (this :> IEquatable<_>).Equals(o)
| _ -> false

interface IEquatable<Path> with
member this.Equals(other: Path) =
Object.ReferenceEquals(this.Segments, other.Segments) ||
not (isNull this.Segments) &&
not (isNull other.Segments) &&
this.Segments.Count = other.Segments.Count &&
Seq.forall2 (=) this.Segments other.Segments

override this.GetHashCode() =
let hash = HashCode()

for i = 0 to this.Segments.Count - 1 do
hash.Add this.Segments[i]
hash.ToHashCode()

let set =
HashSet<Path> [
Path("C:", "tmp", "file.txt")
Path("C:", "tmp", "file.tmp")
Path("C:", "tmp", "file.txt") ]

printfn $"Item count: {set.Count}."

// The example displays the following output:
// Item count: 2.
// </Snippet1>
42 changes: 42 additions & 0 deletions snippets/fsharp/System/HashCode/Overview/example3.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module example3

// <Snippet1>
open System
open System.Collections.Generic

[<Struct; CustomEquality; NoComparison>]
type Path([<ParamArray>]segments: string[]) =
member _.Segments =
Array.AsReadOnly segments

override this.Equals(obj) =
match obj with
| :? Path as o -> (this :> IEquatable<_>).Equals(o)
| _ -> false

interface IEquatable<Path> with
member this.Equals(other: Path) =
Object.ReferenceEquals(this.Segments, other.Segments) ||
not (isNull this.Segments) &&
not (isNull other.Segments) &&
this.Segments.Count = other.Segments.Count &&
Seq.forall2 (fun x y -> String.Equals(x, y, StringComparison.OrdinalIgnoreCase)) this.Segments other.Segments

override this.GetHashCode() =
let hash = HashCode()

for i = 0 to this.Segments.Count - 1 do
hash.Add(this.Segments[i], StringComparer.OrdinalIgnoreCase)
hash.ToHashCode()

let set =
HashSet<Path> [
Path("C:", "tmp", "file.txt")
Path("C:", "tmp", "file.tmp")
Path("C:", "tmp", "file.txt") ]

printfn $"Item count: {set.Count}."

// The example displays the following output:
// Item count: 1.
// </Snippet1>
47 changes: 47 additions & 0 deletions snippets/fsharp/System/HashCode/Overview/example4.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module example4

// <Snippet1>
open System
open System.Collections.Generic

module PlatformUtils =
let pathEquals a b = String.Equals(a, b, StringComparison.OrdinalIgnoreCase)
let addPath (hash: byref<HashCode>) path = hash.Add(path, StringComparer.OrdinalIgnoreCase)

[<Struct; CustomEquality; NoComparison>]
type Path([<ParamArray>]segments: string[]) =
member _.Segments =
Array.AsReadOnly segments

override this.Equals(obj) =
match obj with
| :? Path as o -> (this :> IEquatable<_>).Equals(o)
| _ -> false

interface IEquatable<Path> with
member this.Equals(other: Path) =
Object.ReferenceEquals(this.Segments, other.Segments) ||
not (isNull this.Segments) &&
not (isNull other.Segments) &&
this.Segments.Count = other.Segments.Count &&
Seq.forall2 PlatformUtils.pathEquals this.Segments other.Segments

override this.GetHashCode() =
let mutable hash = HashCode()

for i = 0 to this.Segments.Count - 1 do
PlatformUtils.addPath &hash this.Segments[i]
hash.ToHashCode()


let set =
HashSet<Path> [
Path("C:", "tmp", "file.txt")
Path("C:", "TMP", "file.txt")
Path("C:", "tmp", "FILE.TXT") ]

printfn $"Item count: {set.Count}."

// The example displays the following output:
// Item count: 1.
// </Snippet1>
13 changes: 13 additions & 0 deletions snippets/fsharp/System/HashCode/Overview/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="example1.fs" />
<Compile Include="example2.fs" />
<Compile Include="example3.fs" />
<Compile Include="example4.fs" />
</ItemGroup>
</Project>
4 changes: 4 additions & 0 deletions xml/System/HashCode.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ single hash code. This structure operates in one of two ways:
The static methods combine the default hash codes of up to eight values.

:::code language="csharp" source="~/snippets/csharp/System/HashCode/Overview/example1.cs" id="Snippet1":::
:::code language="fsharp" source="~/snippets/fsharp/System/HashCode/Overview/example1.fs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.hashcode.structure/vb/example1.vb" id="Snippet1":::

### Instance Methods
Expand All @@ -63,17 +64,20 @@ The static methods combine the default hash codes of up to eight values.
The instance methods combine the hash codes of more than eight values.

:::code language="csharp" source="~/snippets/csharp/System/HashCode/Overview/example2.cs" id="Snippet1":::
:::code language="fsharp" source="~/snippets/fsharp/System/HashCode/Overview/example2.fs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.hashcode.structure/vb/example2.vb" id="Snippet1":::

The instance methods also combine the hash codes produced by a specific
<xref:System.Collections.Generic.IEqualityComparer%601> implementation.

:::code language="csharp" source="~/snippets/csharp/System/HashCode/Overview/example3.cs" id="Snippet1":::
:::code language="fsharp" source="~/snippets/fsharp/System/HashCode/Overview/example3.fs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.hashcode.structure/vb/example3.vb" id="Snippet1":::

The <xref:System.HashCode> structure must be passed by-reference to other methods, as it is a value type.

:::code language="csharp" source="~/snippets/csharp/System/HashCode/Overview/example4.cs" id="Snippet1":::
:::code language="fsharp" source="~/snippets/fsharp/System/HashCode/Overview/example4.fs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.hashcode.structure/vb/example4.vb" id="Snippet1":::

]]></format>
Expand Down