diff --git a/snippets/fsharp/System/IComparable/Overview/fs.fsproj b/snippets/fsharp/System/IComparable/Overview/fs.fsproj new file mode 100644 index 00000000000..f0a89cd967d --- /dev/null +++ b/snippets/fsharp/System/IComparable/Overview/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/IComparable/Overview/source.fs b/snippets/fsharp/System/IComparable/Overview/source.fs new file mode 100644 index 00000000000..ca05722c981 --- /dev/null +++ b/snippets/fsharp/System/IComparable/Overview/source.fs @@ -0,0 +1,59 @@ +// +open System +open System.Collections + +type Temperature() = + // The temperature value + let mutable temperatureF = 0. + + interface IComparable with + member _.CompareTo(obj) = + match obj with + | null -> 1 + | :? Temperature as other -> + temperatureF.CompareTo other.Fahrenheit + | _ -> + invalidArg (nameof obj) "Object is not a Temperature" + + member _.Fahrenheit + with get () = + temperatureF + and set (value) = + temperatureF <- value + + member _.Celsius + with get () = + (temperatureF - 32.) * (5. / 9.) + and set (value) = + temperatureF <- (value * 9. / 5.) + 32. + +let temperatures = ResizeArray() + +// Initialize random number generator. +let rnd = Random() + +// Generate 10 temperatures between 0 and 100 randomly. +for _ = 1 to 10 do + let degrees = rnd.Next(0, 100) + let temp = Temperature(Fahrenheit=degrees) + temperatures.Add temp + +// Sort ResizeArray. +temperatures.Sort() + +for temp in temperatures do + printfn $"{temp.Fahrenheit}" + +// The example displays the following output to the console (individual +// values may vary because they are randomly generated): +// 2 +// 7 +// 16 +// 17 +// 31 +// 37 +// 58 +// 66 +// 72 +// 95 +// \ No newline at end of file diff --git a/snippets/fsharp/System/IComparableT/Overview/fs.fsproj b/snippets/fsharp/System/IComparableT/Overview/fs.fsproj new file mode 100644 index 00000000000..f0a89cd967d --- /dev/null +++ b/snippets/fsharp/System/IComparableT/Overview/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/IComparableT/Overview/source.fs b/snippets/fsharp/System/IComparableT/Overview/source.fs new file mode 100644 index 00000000000..4f6d55f6217 --- /dev/null +++ b/snippets/fsharp/System/IComparableT/Overview/source.fs @@ -0,0 +1,75 @@ +// +open System +open System.Collections.Generic + +type Temperature(kelvins: double) = + // The underlying temperature value. + let mutable kelvins = kelvins + + do + if kelvins < 0. then + invalidArg (nameof kelvins) "Temperature cannot be less than absolute zero." + + // Define the is greater than operator. + static member op_GreaterThan (operand1: Temperature, operand2: Temperature) = + operand1.CompareTo operand2 > 0 + + // Define the is less than operator. + static member op_LessThan (operand1: Temperature, operand2: Temperature) = + operand1.CompareTo operand2 < 0 + + // Define the is greater than or equal to operator. + static member op_GreaterThanOrEqual (operand1: Temperature, operand2: Temperature) = + operand1.CompareTo operand2 >= 0 + + // Define the is less than or equal to operator. + static member op_LessThanOrEqual (operand1: Temperature, operand2: Temperature) = + operand1.CompareTo operand2 <= 0 + + member _.Celsius = + kelvins - 273.15 + + member _.Kelvin + with get () = + kelvins + and set (value) = + if value < 0. then + invalidArg (nameof value) "Temperature cannot be less than absolute zero." + else + kelvins <- value + + // Implement the generic CompareTo method with the Temperature + // class as the Type parameter. + member _.CompareTo(other: Temperature) = + // If other is not a valid object reference, this instance is greater. + match box other with + | null -> 1 + | _ -> + // The temperature comparison depends on the comparison of + // the underlying Double values. + kelvins.CompareTo(other.Kelvin) + + interface IComparable with + member this.CompareTo(other) = this.CompareTo other + +let temps = SortedList() + +// Add entries to the sorted list, out of order. +temps.Add(Temperature 2017.15, "Boiling point of Lead") +temps.Add(Temperature 0., "Absolute zero") +temps.Add(Temperature 273.15, "Freezing point of water") +temps.Add(Temperature 5100.15, "Boiling point of Carbon") +temps.Add(Temperature 373.15, "Boiling point of water") +temps.Add(Temperature 600.65, "Melting point of Lead") + +for kvp in temps do + printfn $"{kvp.Value} is {kvp.Key.Celsius} degrees Celsius." + +// This example displays the following output: +// Absolute zero is -273.15 degrees Celsius. +// Freezing point of water is 0 degrees Celsius. +// Boiling point of water is 100 degrees Celsius. +// Melting point of Lead is 327.5 degrees Celsius. +// Boiling point of Lead is 1744 degrees Celsius. +// Boiling point of Carbon is 4827 degrees Celsius. +// \ No newline at end of file diff --git a/xml/System/IComparable.xml b/xml/System/IComparable.xml index 155fab4262b..41d37f27186 100644 --- a/xml/System/IComparable.xml +++ b/xml/System/IComparable.xml @@ -66,6 +66,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/IComparable Example/CPP/source.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/IComparable/Overview/source.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/IComparable/Overview/source.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/IComparable Example/VB/source.vb" id="Snippet1"::: ]]> @@ -152,6 +153,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/IComparable Example/CPP/source.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/IComparable/Overview/source.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/IComparable/Overview/source.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/IComparable Example/VB/source.vb" id="Snippet1"::: ]]> diff --git a/xml/System/IComparable`1.xml b/xml/System/IComparable`1.xml index 7589cd3ca5e..8e8164f0bf1 100644 --- a/xml/System/IComparable`1.xml +++ b/xml/System/IComparable`1.xml @@ -72,6 +72,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/IComparable`1 Example/CPP/source.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/IComparableT/Overview/source.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/IComparableT/Overview/source.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/IComparable`1 Example/VB/source.vb" id="Snippet1"::: ]]> @@ -162,6 +163,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/IComparable`1 Example/CPP/source.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/IComparableT/Overview/source.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/IComparableT/Overview/source.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/IComparable`1 Example/VB/source.vb" id="Snippet1"::: ]]>