From 07b3f646370513bb834338db45bc621052481973 Mon Sep 17 00:00:00 2001 From: albert-du <52804499+albert-du@users.noreply.github.com> Date: Tue, 28 Dec 2021 14:10:51 -0800 Subject: [PATCH] System.Comparison F# Snippets --- .../List`1_SortComparison/fs/fs.fsproj | 9 +++ .../List`1_SortComparison/fs/source.fs | 66 +++++++++++++++++++ .../system.comparison`1/fs/comparisont1.fs | 64 ++++++++++++++++++ .../system.comparison`1/fs/fs.fsproj | 9 +++ xml/System/Comparison`1.xml | 2 + 5 files changed, 150 insertions(+) create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR/List`1_SortComparison/fs/fs.fsproj create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR/List`1_SortComparison/fs/source.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.comparison`1/fs/comparisont1.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.comparison`1/fs/fs.fsproj diff --git a/samples/snippets/fsharp/VS_Snippets_CLR/List`1_SortComparison/fs/fs.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR/List`1_SortComparison/fs/fs.fsproj new file mode 100644 index 00000000000..fbff4133403 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR/List`1_SortComparison/fs/fs.fsproj @@ -0,0 +1,9 @@ + + + Exe + net6.0 + + + + + \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR/List`1_SortComparison/fs/source.fs b/samples/snippets/fsharp/VS_Snippets_CLR/List`1_SortComparison/fs/source.fs new file mode 100644 index 00000000000..1e52fa1a852 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR/List`1_SortComparison/fs/source.fs @@ -0,0 +1,66 @@ +// +open System + +let compareDinosByLength (x: string) (y: string) = + match x with + | null when isNull y -> + // If x is null and y is null, they're equal. + 0 + | null -> + // If x is null and y is not null, y is greater. + -1 + | _ when isNull y-> + // If x is not null and y is null, x is greater. + 1 + | _ -> + // If x is not null and y is not null, compare the lengths of the two strings. + let retval = x.Length.CompareTo y.Length + + if retval <> 0 then + // If the strings are not of equal length, the longer string is greater. + retval + else + // If the strings are of equal length, sort them with ordinary string comparison. + x.CompareTo y + +let display list = + printfn "" + for s in list do + if isNull s then + printfn "(null)" + else + printfn $"\"%s{s}\"" + + +let dinosaurs = ResizeArray() +dinosaurs.Add "Pachycephalosaurus" +dinosaurs.Add "Amargasaurus" +dinosaurs.Add "" +dinosaurs.Add null +dinosaurs.Add "Mamenchisaurus" +dinosaurs.Add "Deinonychus" +display dinosaurs + +printfn "\nSort with generic Comparison delegate:" +dinosaurs.Sort compareDinosByLength +display dinosaurs + + +// This code example produces the following output: +// +// "Pachycephalosaurus" +// "Amargasaurus" +// "" +// (null) +// "Mamenchisaurus" +// "Deinonychus" +// +// Sort with generic Comparison delegate: +// +// (null) +// "" +// "Deinonychus" +// "Amargasaurus" +// "Mamenchisaurus" +// "Pachycephalosaurus" +// \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.comparison`1/fs/comparisont1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.comparison`1/fs/comparisont1.fs new file mode 100644 index 00000000000..0cca9a9b933 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.comparison`1/fs/comparisont1.fs @@ -0,0 +1,64 @@ +// +open System + +type CityInfo = + { City: string + Country: string + Population: int } + + static member CompareByName city1 city2 = + String.Compare(city1.City, city2.City) + + static member CompareByPopulation city1 city2 = + city1.Population.CompareTo city2.Population + + static member CompareByNames city1 city2 = + String.Compare(city1.Country + city1.City, city2.Country + city2.City) + +let display cities = + printfn $"""{"City",-20} {"Country",-25} {"Population",10}""" + for city in cities do + printfn $"{city.City,-20} {city.Country,-25} {city.Population,10:N0}" + printfn "" + +let NYC = { City = "New York City"; Country = "United States of America"; Population = 8175133 } +let Det = { City = "Detroit"; Country = "United States of America"; Population = 713777 } +let Paris = { City = "Paris"; Country = "France"; Population = 2193031 } +let cities = [| NYC; Det; Paris |] +// Display ordered array. +display cities + +// Sort array by city name. +Array.Sort(cities, CityInfo.CompareByName) +display cities + +// Sort array by population. +Array.Sort(cities, CityInfo.CompareByPopulation); +display cities + +// Sort array by country + city name. +Array.Sort(cities, CityInfo.CompareByNames); +display cities + + +// The example displays the following output: +// City Country Population +// New York City United States of America 8,175,133 +// Detroit United States of America 713,777 +// Paris France 2,193,031 +// +// City Country Population +// Detroit United States of America 713,777 +// New York City United States of America 8,175,133 +// Paris France 2,193,031 +// +// City Country Population +// Detroit United States of America 713,777 +// Paris France 2,193,031 +// New York City United States of America 8,175,133 +// +// City Country Population +// Paris France 2,193,031 +// Detroit United States of America 713,777 +// New York City United States of America 8,175,133 +// \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.comparison`1/fs/fs.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.comparison`1/fs/fs.fsproj new file mode 100644 index 00000000000..202f2c88cf0 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.comparison`1/fs/fs.fsproj @@ -0,0 +1,9 @@ + + + Exe + net6.0 + + + + + \ No newline at end of file diff --git a/xml/System/Comparison`1.xml b/xml/System/Comparison`1.xml index 395b192cc57..e0938673472 100644 --- a/xml/System/Comparison`1.xml +++ b/xml/System/Comparison`1.xml @@ -94,11 +94,13 @@ :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR/List`1_SortComparison/cpp/source.cpp" id="Snippet1"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR/List`1_SortComparison/cs/source.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR/List`1_SortComparison/fs/source.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR/List`1_SortComparison/vb/source.vb" id="Snippet1"::: The following example uses the delegate to sort the elements of a collection of `CityInfo` objects. `CityInfo` is an application-defined class that contains information about a city and its population. The example defines three methods, `CompareByName`, `CompareByPopulation`, and `CompareByNames`, that offer three different ways of ordering the `CityInfo` objects. Each method is assigned to the `comparison` argument of the method. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.comparison`1/cs/comparisont1.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.comparison`1/fs/comparisont1.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.comparison`1/vb/comparisont1.vb" id="Snippet1"::: ]]>