File tree Expand file tree Collapse file tree 11 files changed +276
-0
lines changed
snippets/fsharp/System/TupleT1
System.Collections.IStructuralComparable.CompareTo Expand file tree Collapse file tree 11 files changed +276
-0
lines changed Original file line number Diff line number Diff line change 1+ module equals1
2+
3+ // <Snippet1>
4+ open System
5+
6+ let testEquality ( tuple : Tuple < double >) ( obj : obj ) =
7+ printfn $" {tuple} = {obj}: {tuple.Equals obj}"
8+
9+ let doubleTuple1 = Tuple.Create 12.3455
10+ let doubleTuple2 = Tuple.Create 16.8912
11+ let doubleTuple3 = Tuple.Create 12.3455
12+ let singleTuple1 = Tuple.Create 12.3455 f
13+ let tuple2 = Tuple.Create( " James" , 97.3 )
14+
15+ // Compare first tuple with a Tuple(Of Double) with a different value.
16+ testEquality doubleTuple1 doubleTuple2
17+ // Compare first tuple with a Tuple(Of Double) with the same value.
18+ testEquality doubleTuple1 doubleTuple3
19+ // Compare first tuple with a Tuple(Of Single) with the same value.
20+ testEquality doubleTuple1 singleTuple1
21+ // Compare a 1-tuple with a 2-tuple.
22+ testEquality doubleTuple1 tuple2
23+
24+ // The example displays the following output:
25+ // (12.3455) = (16.8912): False
26+ // (12.3455) = (12.3455): True
27+ // (12.3455) = (12.3455): False
28+ // (12.3455) = (James, 97.3): False
29+ // </Snippet1>
Original file line number Diff line number Diff line change 1+ module equals2
2+
3+ // <Snippet2>
4+ open System
5+ open System.Collections
6+
7+ type Tuple1Comparer () =
8+ interface IEqualityComparer with
9+ member _.Equals ( x : obj , y : obj ) =
10+ match x with
11+ | :? double as dblX ->
12+ // Convert to Double values.
13+ let dblY = y :?> double
14+ if Double.IsNaN dblX || Double.IsInfinity dblX ||
15+ Double.IsNaN dblY || Double.IsInfinity dblY then
16+ dblX.Equals dblY
17+ else
18+ abs ( dblX - dblY) <= dblX * 0.0001
19+ | _ ->
20+ x.Equals y
21+
22+ member _.GetHashCode ( obj : obj ) =
23+ obj.GetHashCode()
24+
25+ let testEquality ( tuple : Tuple < double >) ( obj : obj ) =
26+ printfn $" {tuple} = {obj}: {(tuple :> IStructuralEquatable).Equals(obj, Tuple1Comparer())}"
27+
28+ let doubleTuple1 = Tuple.Create 12.3455
29+ let doubleTuple2 = Tuple.Create 16.8912
30+ let doubleTuple3 = Tuple.Create 12.3449599
31+
32+ // Compare first tuple with a Tuple<double> with a different value.
33+ testEquality doubleTuple1 doubleTuple2
34+ //Compare first tuple with a Tuple<double> with the same value.
35+ testEquality doubleTuple1 doubleTuple3
36+
37+ // The example displays the following output:
38+ // (12.3455) = (16.8912): False
39+ // (12.3455) = (12.3449599): True
40+ // </Snippet2>
Original file line number Diff line number Diff line change 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 =" equals1.fs" />
9+ <Compile Include =" equals2.fs" />
10+ </ItemGroup >
11+ </Project >
Original file line number Diff line number Diff line change 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 =" item1.fs" />
9+ </ItemGroup >
10+ </Project >
Original file line number Diff line number Diff line change 1+ // <Snippet1>
2+ open System
3+
4+ let tuple1 = Tuple.Create - 1.23445e-32
5+ // Display information about this singleton.
6+ let tuple1Type = tuple1.GetType()
7+ printfn " First 1-Tuple:"
8+ printfn $" Type: {tuple1Type.Name}"
9+ printfn $" Generic Parameter Type: {tuple1Type.GetGenericArguments()[0]}"
10+ printfn $" Component Value: {tuple1.Item1}"
11+ printfn $" Component Value Type: {tuple1.Item1.GetType().Name}\n "
12+
13+ let tuple2 = Tuple.Create( bigint 1.83789322281780983781356676e103 )
14+ // Display information about this singleton.
15+ let tuple2Type = tuple2.GetType()
16+ printfn " Second 1-Tuple:"
17+ printfn $" Type: {tuple2Type.Name}"
18+ printfn $" Generic Parameter Type: {tuple2Type.GetGenericArguments()[0]}"
19+ printfn $" Component Value: {tuple2.Item1}"
20+ printfn $" Component Value Type: {tuple2.Item1.GetType().Name}"
21+ // The example displays the following output:
22+ // First 1-Tuple:
23+ // Type: Tuple`1
24+ // Generic Parameter Type: System.Double
25+ // Component Value: -1.23445E-32
26+ // Component Value Type: Double
27+ //
28+ // Second 1-Tuple:
29+ // Type: Tuple`1
30+ // Generic Parameter Type: System.Numerics.BigInteger
31+ // Component Value: 1.8378932228178098168858909492E+103
32+ // Component Value Type: BigInteger
33+ // </Snippet1>
Original file line number Diff line number Diff line change 1+ module compareto1
2+
3+ // <Snippet1>
4+ open System
5+
6+ let values =
7+ [| Tuple.Create 13.54
8+ Tuple.Create Double.NaN
9+ Tuple.Create - 189.42993
10+ Tuple.Create Double.PositiveInfinity
11+ Tuple.Create Double.Epsilon
12+ Tuple.Create 1.934E-17
13+ Tuple.Create Double.NegativeInfinity
14+ Tuple.Create - 0.000000000003588
15+ null |]
16+ printfn " The values in unsorted order:"
17+ for value in values do
18+ printfn $" %A {value.Item1}"
19+ printfn " "
20+
21+ Array.Sort values
22+
23+ printfn " The values sorted in descending order:"
24+ for value in values do
25+ printfn $" %A {value.Item1}"
26+ // The example displays the following output:
27+ // The values in unsorted order:
28+ // 13.54
29+ // NaN
30+ // -189.42993
31+ // Infinity
32+ // 4.94065645841247E-324
33+ // 1.934E-17
34+ // -Infinity
35+ // -3.588E-12
36+ //
37+ // The values in sorted order:
38+ // NaN
39+ // -Infinity
40+ // -189.42993
41+ // -3.588E-12
42+ // 4.94065645841247E-324
43+ // 1.934E-17
44+ // 13.54
45+ // Infinity
46+ // </Snippet1>
Original file line number Diff line number Diff line change 1+ module compareto2
2+
3+ // <Snippet2>
4+ open System
5+ open System.Collections .Generic
6+
7+ type DescendingComparer < 'T >() =
8+ interface IComparer< 'T> with
9+ member _.Compare ( x : 'T , y : 'T ) =
10+ - 1 * Comparer< 'T>. Default.Compare( x, y)
11+
12+ let values =
13+ [| Tuple.Create 13.54
14+ Tuple.Create Double.NaN
15+ Tuple.Create - 189.42993
16+ Tuple.Create Double.PositiveInfinity
17+ Tuple.Create Double.Epsilon
18+ Tuple.Create 1.934E-17
19+ Tuple.Create Double.NegativeInfinity
20+ Tuple.Create - 0.000000000003588
21+ null |]
22+ printfn " The values in unsorted order:"
23+ for value in values do
24+ printfn $" %A {value.Item1}"
25+ printfn " "
26+
27+ Array.Sort( values, DescendingComparer< Tuple< Double>>())
28+
29+ printfn " The values sorted in descending order:"
30+ for value in values do
31+ printfn $" %A {value.Item1}"
32+ // The example displays the following output:
33+ // The values in unsorted order:
34+ // 13.54
35+ // NaN
36+ // -189.42993
37+ // Infinity
38+ // 4.94065645841247E-324
39+ // 1.934E-17
40+ // -Infinity
41+ // -3.588E-12
42+ // <null>
43+ //
44+ // The values sorted in descending order:
45+ // Infinity
46+ // 13.54
47+ // 1.934E-17
48+ // 4.94065645841247E-324
49+ // -3.588E-12
50+ // -189.42993
51+ // -Infinity
52+ // NaN
53+ // <null>
54+ // </Snippet2>
Original file line number Diff line number Diff line change 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 =" compareto2.fs" />
9+ <Compile Include =" compareto1.fs" />
10+ </ItemGroup >
11+ </Project >
Original file line number Diff line number Diff line change 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 =" tostring1.fs" />
9+ </ItemGroup >
10+ </Project >
Original file line number Diff line number Diff line change 1+ // <Snippet1>
2+ open System
3+
4+ let displayTuple obj =
5+ // String interpolation implicitly calls .ToString().
6+ printfn $" {obj}"
7+
8+ let tuple1Double = Tuple.Create 3.456e-18
9+ displayTuple tuple1Double
10+
11+ let tuple1String = Tuple.Create " Australia"
12+ displayTuple tuple1String
13+
14+ let tuple1Bool = Tuple.Create true
15+ displayTuple tuple1Bool
16+
17+ let tuple1Char = Tuple.Create 'a'
18+ displayTuple tuple1Char
19+ // The example displays the following output:
20+ // (3.456E-18)
21+ // (Australia)
22+ // (True)
23+ // (a)
24+ // </Snippet1>
You can’t perform that action at this time.
0 commit comments