Thanks to visit codestin.com
Credit goes to github.com

Skip to content

System.IComparable F# snippets #7753

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
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
10 changes: 10 additions & 0 deletions snippets/fsharp/System/IComparable/Overview/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="source.fs" />
</ItemGroup>
</Project>
59 changes: 59 additions & 0 deletions snippets/fsharp/System/IComparable/Overview/source.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// <Snippet1>
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
// </Snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/IComparableT/Overview/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="source.fs" />
</ItemGroup>
</Project>
75 changes: 75 additions & 0 deletions snippets/fsharp/System/IComparableT/Overview/source.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//<snippet1>
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<Temperature> 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.
//</snippet1>
2 changes: 2 additions & 0 deletions xml/System/IComparable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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":::

]]></format>
Expand Down Expand Up @@ -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":::

]]></format>
Expand Down
2 changes: 2 additions & 0 deletions xml/System/IComparable`1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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":::

]]></format>
Expand Down Expand Up @@ -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":::

]]></format>
Expand Down