From e4457f8f57e3cef91a0414344a66e6ebad1fd95b Mon Sep 17 00:00:00 2001 From: albert-du <52804499+albert-du@users.noreply.github.com> Date: Tue, 15 Feb 2022 20:33:18 -0800 Subject: [PATCH 1/2] HashCode F# snippets --- .../System/HashCode/Overview/example1.fs | 30 ++++++++++++ .../System/HashCode/Overview/example2.fs | 42 +++++++++++++++++ .../System/HashCode/Overview/example3.fs | 42 +++++++++++++++++ .../System/HashCode/Overview/example4.fs | 47 +++++++++++++++++++ .../fsharp/System/HashCode/Overview/fs.fsproj | 13 +++++ xml/System/HashCode.xml | 4 ++ 6 files changed, 178 insertions(+) create mode 100644 snippets/fsharp/System/HashCode/Overview/example1.fs create mode 100644 snippets/fsharp/System/HashCode/Overview/example2.fs create mode 100644 snippets/fsharp/System/HashCode/Overview/example3.fs create mode 100644 snippets/fsharp/System/HashCode/Overview/example4.fs create mode 100644 snippets/fsharp/System/HashCode/Overview/fs.fsproj diff --git a/snippets/fsharp/System/HashCode/Overview/example1.fs b/snippets/fsharp/System/HashCode/Overview/example1.fs new file mode 100644 index 00000000000..6163df257da --- /dev/null +++ b/snippets/fsharp/System/HashCode/Overview/example1.fs @@ -0,0 +1,30 @@ +module example1 + +// +open System +open System.Collections.Generic + +[] +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 with + member _.Equals(other: OrderOrderLine) = + orderId = other.OrderId && orderLineId = other.OrderLineId + +let set = + HashSet [ OrderOrderLine(1, 1); OrderOrderLine(1, 1); OrderOrderLine(1, 2) ] +printfn $"Item count: {set.Count}." + +// The example displays the following output: +// Item count: 2. +// \ No newline at end of file diff --git a/snippets/fsharp/System/HashCode/Overview/example2.fs b/snippets/fsharp/System/HashCode/Overview/example2.fs new file mode 100644 index 00000000000..5a03223d8c1 --- /dev/null +++ b/snippets/fsharp/System/HashCode/Overview/example2.fs @@ -0,0 +1,42 @@ +module example2 + +// +open System +open System.Collections.Generic + +[] +type Path([]segments: string[]) = + member _.Segments = + Array.AsReadOnly segments + + override this.Equals(obj) = + match obj with + | :? Path as o -> (this :> IEquatable<_>).Equals(o) + | _ -> false + + interface IEquatable with + member this.Equals(other: Path) = + Object.ReferenceEquals(this.Segments, other.Segments) && + not (this.Segments <> null) && + 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("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. +// \ No newline at end of file diff --git a/snippets/fsharp/System/HashCode/Overview/example3.fs b/snippets/fsharp/System/HashCode/Overview/example3.fs new file mode 100644 index 00000000000..c235715099b --- /dev/null +++ b/snippets/fsharp/System/HashCode/Overview/example3.fs @@ -0,0 +1,42 @@ +module example3 + +// +open System +open System.Collections.Generic + +[] +type Path([]segments: string[]) = + member _.Segments = + Array.AsReadOnly segments + + override this.Equals(obj) = + match obj with + | :? Path as o -> (this :> IEquatable<_>).Equals(o) + | _ -> false + + interface IEquatable with + member this.Equals(other: Path) = + Object.ReferenceEquals(this.Segments, other.Segments) && + not (this.Segments <> null) && + 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("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. +// \ No newline at end of file diff --git a/snippets/fsharp/System/HashCode/Overview/example4.fs b/snippets/fsharp/System/HashCode/Overview/example4.fs new file mode 100644 index 00000000000..09a49a4566e --- /dev/null +++ b/snippets/fsharp/System/HashCode/Overview/example4.fs @@ -0,0 +1,47 @@ +module example4 + +// +open System +open System.Collections.Generic + +module PlatformUtils = + let pathEquals a b = String.Equals(a, b, StringComparison.OrdinalIgnoreCase) + let addPath (hash: byref) path = hash.Add(path, StringComparer.OrdinalIgnoreCase) + +[] +type Path([]segments: string[]) = + member _.Segments = + Array.AsReadOnly segments + + override this.Equals(obj) = + match obj with + | :? Path as o -> (this :> IEquatable<_>).Equals(o) + | _ -> false + + interface IEquatable with + member this.Equals(other: Path) = + Object.ReferenceEquals(this.Segments, other.Segments) && + not (this.Segments <> null) && + 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("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. +// \ No newline at end of file diff --git a/snippets/fsharp/System/HashCode/Overview/fs.fsproj b/snippets/fsharp/System/HashCode/Overview/fs.fsproj new file mode 100644 index 00000000000..81a6369fde3 --- /dev/null +++ b/snippets/fsharp/System/HashCode/Overview/fs.fsproj @@ -0,0 +1,13 @@ + + + Exe + net6.0 + + + + + + + + + \ No newline at end of file diff --git a/xml/System/HashCode.xml b/xml/System/HashCode.xml index c4f933f435a..26c10951e79 100644 --- a/xml/System/HashCode.xml +++ b/xml/System/HashCode.xml @@ -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 @@ -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 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 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"::: ]]> From 5b7cf69fdaa1c92221b60ab40851b2ff8208df76 Mon Sep 17 00:00:00 2001 From: albert-du <52804499+albert-du@users.noreply.github.com> Date: Wed, 16 Feb 2022 19:01:56 -0800 Subject: [PATCH 2/2] fix snippets --- snippets/fsharp/System/HashCode/Overview/example2.fs | 4 ++-- snippets/fsharp/System/HashCode/Overview/example3.fs | 4 ++-- snippets/fsharp/System/HashCode/Overview/example4.fs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/snippets/fsharp/System/HashCode/Overview/example2.fs b/snippets/fsharp/System/HashCode/Overview/example2.fs index 5a03223d8c1..65b78e491ed 100644 --- a/snippets/fsharp/System/HashCode/Overview/example2.fs +++ b/snippets/fsharp/System/HashCode/Overview/example2.fs @@ -16,8 +16,8 @@ type Path([]segments: string[]) = interface IEquatable with member this.Equals(other: Path) = - Object.ReferenceEquals(this.Segments, other.Segments) && - not (this.Segments <> null) && + 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 diff --git a/snippets/fsharp/System/HashCode/Overview/example3.fs b/snippets/fsharp/System/HashCode/Overview/example3.fs index c235715099b..a63dcda55f0 100644 --- a/snippets/fsharp/System/HashCode/Overview/example3.fs +++ b/snippets/fsharp/System/HashCode/Overview/example3.fs @@ -16,8 +16,8 @@ type Path([]segments: string[]) = interface IEquatable with member this.Equals(other: Path) = - Object.ReferenceEquals(this.Segments, other.Segments) && - not (this.Segments <> null) && + 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 diff --git a/snippets/fsharp/System/HashCode/Overview/example4.fs b/snippets/fsharp/System/HashCode/Overview/example4.fs index 09a49a4566e..9b6730a5423 100644 --- a/snippets/fsharp/System/HashCode/Overview/example4.fs +++ b/snippets/fsharp/System/HashCode/Overview/example4.fs @@ -20,8 +20,8 @@ type Path([]segments: string[]) = interface IEquatable with member this.Equals(other: Path) = - Object.ReferenceEquals(this.Segments, other.Segments) && - not (this.Segments <> null) && + 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