Skip to content

Commit c3bab69

Browse files
authored
IComparable F# snippets (#7753)
1 parent 1dcd3df commit c3bab69

File tree

6 files changed

+158
-0
lines changed

6 files changed

+158
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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="source.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// <Snippet1>
2+
open System
3+
open System.Collections
4+
5+
type Temperature() =
6+
// The temperature value
7+
let mutable temperatureF = 0.
8+
9+
interface IComparable with
10+
member _.CompareTo(obj) =
11+
match obj with
12+
| null -> 1
13+
| :? Temperature as other ->
14+
temperatureF.CompareTo other.Fahrenheit
15+
| _ ->
16+
invalidArg (nameof obj) "Object is not a Temperature"
17+
18+
member _.Fahrenheit
19+
with get () =
20+
temperatureF
21+
and set (value) =
22+
temperatureF <- value
23+
24+
member _.Celsius
25+
with get () =
26+
(temperatureF - 32.) * (5. / 9.)
27+
and set (value) =
28+
temperatureF <- (value * 9. / 5.) + 32.
29+
30+
let temperatures = ResizeArray()
31+
32+
// Initialize random number generator.
33+
let rnd = Random()
34+
35+
// Generate 10 temperatures between 0 and 100 randomly.
36+
for _ = 1 to 10 do
37+
let degrees = rnd.Next(0, 100)
38+
let temp = Temperature(Fahrenheit=degrees)
39+
temperatures.Add temp
40+
41+
// Sort ResizeArray.
42+
temperatures.Sort()
43+
44+
for temp in temperatures do
45+
printfn $"{temp.Fahrenheit}"
46+
47+
// The example displays the following output to the console (individual
48+
// values may vary because they are randomly generated):
49+
// 2
50+
// 7
51+
// 16
52+
// 17
53+
// 31
54+
// 37
55+
// 58
56+
// 66
57+
// 72
58+
// 95
59+
// </Snippet1>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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="source.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//<snippet1>
2+
open System
3+
open System.Collections.Generic
4+
5+
type Temperature(kelvins: double) =
6+
// The underlying temperature value.
7+
let mutable kelvins = kelvins
8+
9+
do
10+
if kelvins < 0. then
11+
invalidArg (nameof kelvins) "Temperature cannot be less than absolute zero."
12+
13+
// Define the is greater than operator.
14+
static member op_GreaterThan (operand1: Temperature, operand2: Temperature) =
15+
operand1.CompareTo operand2 > 0
16+
17+
// Define the is less than operator.
18+
static member op_LessThan (operand1: Temperature, operand2: Temperature) =
19+
operand1.CompareTo operand2 < 0
20+
21+
// Define the is greater than or equal to operator.
22+
static member op_GreaterThanOrEqual (operand1: Temperature, operand2: Temperature) =
23+
operand1.CompareTo operand2 >= 0
24+
25+
// Define the is less than or equal to operator.
26+
static member op_LessThanOrEqual (operand1: Temperature, operand2: Temperature) =
27+
operand1.CompareTo operand2 <= 0
28+
29+
member _.Celsius =
30+
kelvins - 273.15
31+
32+
member _.Kelvin
33+
with get () =
34+
kelvins
35+
and set (value) =
36+
if value < 0. then
37+
invalidArg (nameof value) "Temperature cannot be less than absolute zero."
38+
else
39+
kelvins <- value
40+
41+
// Implement the generic CompareTo method with the Temperature
42+
// class as the Type parameter.
43+
member _.CompareTo(other: Temperature) =
44+
// If other is not a valid object reference, this instance is greater.
45+
match box other with
46+
| null -> 1
47+
| _ ->
48+
// The temperature comparison depends on the comparison of
49+
// the underlying Double values.
50+
kelvins.CompareTo(other.Kelvin)
51+
52+
interface IComparable<Temperature> with
53+
member this.CompareTo(other) = this.CompareTo other
54+
55+
let temps = SortedList()
56+
57+
// Add entries to the sorted list, out of order.
58+
temps.Add(Temperature 2017.15, "Boiling point of Lead")
59+
temps.Add(Temperature 0., "Absolute zero")
60+
temps.Add(Temperature 273.15, "Freezing point of water")
61+
temps.Add(Temperature 5100.15, "Boiling point of Carbon")
62+
temps.Add(Temperature 373.15, "Boiling point of water")
63+
temps.Add(Temperature 600.65, "Melting point of Lead")
64+
65+
for kvp in temps do
66+
printfn $"{kvp.Value} is {kvp.Key.Celsius} degrees Celsius."
67+
68+
// This example displays the following output:
69+
// Absolute zero is -273.15 degrees Celsius.
70+
// Freezing point of water is 0 degrees Celsius.
71+
// Boiling point of water is 100 degrees Celsius.
72+
// Melting point of Lead is 327.5 degrees Celsius.
73+
// Boiling point of Lead is 1744 degrees Celsius.
74+
// Boiling point of Carbon is 4827 degrees Celsius.
75+
//</snippet1>

xml/System/IComparable.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
6767
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/IComparable Example/CPP/source.cpp" id="Snippet1":::
6868
:::code language="csharp" source="~/snippets/csharp/System/IComparable/Overview/source.cs" interactive="try-dotnet" id="Snippet1":::
69+
:::code language="fsharp" source="~/snippets/fsharp/System/IComparable/Overview/source.fs" id="Snippet1":::
6970
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/IComparable Example/VB/source.vb" id="Snippet1":::
7071
7172
]]></format>
@@ -152,6 +153,7 @@
152153
153154
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/IComparable Example/CPP/source.cpp" id="Snippet1":::
154155
:::code language="csharp" source="~/snippets/csharp/System/IComparable/Overview/source.cs" interactive="try-dotnet" id="Snippet1":::
156+
:::code language="fsharp" source="~/snippets/fsharp/System/IComparable/Overview/source.fs" id="Snippet1":::
155157
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/IComparable Example/VB/source.vb" id="Snippet1":::
156158
157159
]]></format>

xml/System/IComparable`1.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
7373
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/IComparable`1 Example/CPP/source.cpp" id="Snippet1":::
7474
:::code language="csharp" source="~/snippets/csharp/System/IComparableT/Overview/source.cs" interactive="try-dotnet" id="Snippet1":::
75+
:::code language="fsharp" source="~/snippets/fsharp/System/IComparableT/Overview/source.fs" id="Snippet1":::
7576
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/IComparable`1 Example/VB/source.vb" id="Snippet1":::
7677
7778
]]></format>
@@ -162,6 +163,7 @@
162163
163164
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/IComparable`1 Example/CPP/source.cpp" id="Snippet1":::
164165
:::code language="csharp" source="~/snippets/csharp/System/IComparableT/Overview/source.cs" interactive="try-dotnet" id="Snippet1":::
166+
:::code language="fsharp" source="~/snippets/fsharp/System/IComparableT/Overview/source.fs" id="Snippet1":::
165167
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/IComparable`1 Example/VB/source.vb" id="Snippet1":::
166168
167169
]]></format>

0 commit comments

Comments
 (0)