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

Skip to content

Commit bc013de

Browse files
authored
Tuple<T1,T2,T3> F# snippets (#7944)
1 parent 3cdcd2f commit bc013de

File tree

11 files changed

+352
-0
lines changed

11 files changed

+352
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
module equals1
2+
3+
// <Snippet1>
4+
open System
5+
6+
let scores =
7+
[| Tuple.Create("Ed", 78.8, 8)
8+
Tuple.Create("Abbey", 92.1, 9)
9+
Tuple.Create("Ed", 71.2, 9)
10+
Tuple.Create("Sam", 91.7, 8)
11+
Tuple.Create("Ed", 71.2, 5)
12+
Tuple.Create("Penelope", 82.9, 8)
13+
Tuple.Create("Ed", 71.2, 9)
14+
Tuple.Create("Judith", 84.3, 9) |]
15+
16+
// Test each tuple object for equality with every other tuple.
17+
for ctr = 0 to scores.Length - 1 do
18+
let currentTuple = scores[ctr]
19+
for ctr2 = ctr + 1 to scores.Length - 1 do
20+
printfn $"{currentTuple} = {scores[ctr2]}: {currentTuple.Equals scores[ctr2]}"
21+
printfn ""
22+
23+
// The example displays the following output
24+
// (Ed, 78.8, 8) = (Abbey, 92.1, 9): False
25+
// (Ed, 78.8, 8) = (Ed, 71.2, 9): False
26+
// (Ed, 78.8, 8) = (Sam, 91.7, 8): False
27+
// (Ed, 78.8, 8) = (Ed, 71.2, 5): False
28+
// (Ed, 78.8, 8) = (Penelope, 82.9, 8): False
29+
// (Ed, 78.8, 8) = (Ed, 71.2, 9): False
30+
// (Ed, 78.8, 8) = (Judith, 84.3, 9): False
31+
//
32+
// (Abbey, 92.1, 9) = (Ed, 71.2, 9): False
33+
// (Abbey, 92.1, 9) = (Sam, 91.7, 8): False
34+
// (Abbey, 92.1, 9) = (Ed, 71.2, 5): False
35+
// (Abbey, 92.1, 9) = (Penelope, 82.9, 8): False
36+
// (Abbey, 92.1, 9) = (Ed, 71.2, 9): False
37+
// (Abbey, 92.1, 9) = (Judith, 84.3, 9): False
38+
//
39+
// (Ed, 71.2, 9) = (Sam, 91.7, 8): False
40+
// (Ed, 71.2, 9) = (Ed, 71.2, 5): False
41+
// (Ed, 71.2, 9) = (Penelope, 82.9, 8): False
42+
// (Ed, 71.2, 9) = (Ed, 71.2, 9): True
43+
// (Ed, 71.2, 9) = (Judith, 84.3, 9): False
44+
//
45+
// (Sam, 91.7, 8) = (Ed, 71.2, 5): False
46+
// (Sam, 91.7, 8) = (Penelope, 82.9, 8): False
47+
// (Sam, 91.7, 8) = (Ed, 71.2, 9): False
48+
// (Sam, 91.7, 8) = (Judith, 84.3, 9): False
49+
//
50+
// (Ed, 71.2, 5) = (Penelope, 82.9, 8): False
51+
// (Ed, 71.2, 5) = (Ed, 71.2, 9): False
52+
// (Ed, 71.2, 5) = (Judith, 84.3, 9): False
53+
//
54+
// (Penelope, 82.9, 8) = (Ed, 71.2, 9): False
55+
// (Penelope, 82.9, 8) = (Judith, 84.3, 9): False
56+
//
57+
// (Ed, 71.2, 9) = (Judith, 84.3, 9): False
58+
// </Snippet1>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
module equals2
2+
3+
// <Snippet2>
4+
open System
5+
open System.Collections
6+
7+
type Item2Comparer<'T1, 'T2, 'T3 when 'T1: equality and 'T2: equality and 'T3: equality>() =
8+
interface IEqualityComparer with
9+
member _.Equals(x: obj, y: obj) =
10+
// Return true for all values of Item1.
11+
match x with
12+
| :? 'T1 ->
13+
true
14+
| :? 'T2 ->
15+
x.Equals y
16+
| _ -> true
17+
18+
member _.GetHashCode(obj: obj) =
19+
match obj with
20+
| :? 'T1 as obj->
21+
obj.GetHashCode()
22+
| :? 'T2 as obj ->
23+
obj.GetHashCode()
24+
| _ ->
25+
(obj :?> 'T3).GetHashCode()
26+
27+
let scores =
28+
[| Tuple.Create("Ed", 78.8, 8)
29+
Tuple.Create("Abbey", 92.1, 9)
30+
Tuple.Create("Jim", 71.2, 9)
31+
Tuple.Create("Sam", 91.7, 8)
32+
Tuple.Create("Sandy", 71.2, 5)
33+
Tuple.Create("Penelope", 82.9, 8)
34+
Tuple.Create("Serena", 71.2, 9)
35+
Tuple.Create("Judith", 84.3, 9) |]
36+
37+
for ctr = 0 to scores.Length - 1 do
38+
let score : IStructuralEquatable = scores[ctr]
39+
for ctr2 = ctr + 1 to scores.Length - 1 do
40+
printfn $"{score} = {scores[ctr2]}: {score.Equals(scores[ctr2], Item2Comparer<string, double, int>())}"
41+
printfn ""
42+
// The example displays the following output:
43+
// (Ed, 78.8, 8) = (Abbey, 92.1, 9): False
44+
// (Ed, 78.8, 8) = (Jim, 71.2, 9): False
45+
// (Ed, 78.8, 8) = (Sam, 91.7, 8): False
46+
// (Ed, 78.8, 8) = (Sandy, 71.2, 5): False
47+
// (Ed, 78.8, 8) = (Penelope, 82.9, 8): False
48+
// (Ed, 78.8, 8) = (Serena, 71.2, 9): False
49+
// (Ed, 78.8, 8) = (Judith, 84.3, 9): False
50+
//
51+
// (Abbey, 92.1, 9) = (Jim, 71.2, 9): False
52+
// (Abbey, 92.1, 9) = (Sam, 91.7, 8): False
53+
// (Abbey, 92.1, 9) = (Sandy, 71.2, 5): False
54+
// (Abbey, 92.1, 9) = (Penelope, 82.9, 8): False
55+
// (Abbey, 92.1, 9) = (Serena, 71.2, 9): False
56+
// (Abbey, 92.1, 9) = (Judith, 84.3, 9): False
57+
//
58+
// (Jim, 71.2, 9) = (Sam, 91.7, 8): False
59+
// (Jim, 71.2, 9) = (Sandy, 71.2, 5): True
60+
// (Jim, 71.2, 9) = (Penelope, 82.9, 8): False
61+
// (Jim, 71.2, 9) = (Serena, 71.2, 9): True
62+
// (Jim, 71.2, 9) = (Judith, 84.3, 9): False
63+
//
64+
// (Sam, 91.7, 8) = (Sandy, 71.2, 5): False
65+
// (Sam, 91.7, 8) = (Penelope, 82.9, 8): False
66+
// (Sam, 91.7, 8) = (Serena, 71.2, 9): False
67+
// (Sam, 91.7, 8) = (Judith, 84.3, 9): False
68+
//
69+
// (Sandy, 71.2, 5) = (Penelope, 82.9, 8): False
70+
// (Sandy, 71.2, 5) = (Serena, 71.2, 9): True
71+
// (Sandy, 71.2, 5) = (Judith, 84.3, 9): False
72+
//
73+
// (Penelope, 82.9, 8) = (Serena, 71.2, 9): False
74+
// (Penelope, 82.9, 8) = (Judith, 84.3, 9): False
75+
//
76+
// (Serena, 71.2, 9) = (Judith, 84.3, 9): False
77+
// </Snippet2>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// <Snippet1>
2+
open System
3+
4+
let computeStatistics (scores: Tuple<string, double, int>[]) =
5+
let mutable n = 0
6+
let mutable sum = 0.
7+
8+
// Compute the mean.
9+
for score in scores do
10+
n <- n + score.Item3
11+
sum <- sum + score.Item2 * double score.Item3
12+
let mean = sum / double n
13+
14+
// Compute the standard deviation.
15+
let mutable ss = 0.
16+
for score in scores do
17+
ss <- (score.Item2 - mean) ** 2.
18+
let sd = sqrt (ss / double scores.Length)
19+
Tuple.Create(scores.Length, mean, sd)
20+
21+
let scores =
22+
[| Tuple.Create("Jack", 78.8, 8)
23+
Tuple.Create("Abbey", 92.1, 9)
24+
Tuple.Create("Dave", 88.3, 9)
25+
Tuple.Create("Sam", 91.7, 8)
26+
Tuple.Create("Ed", 71.2, 5)
27+
Tuple.Create("Penelope", 82.9, 8)
28+
Tuple.Create("Linda", 99.0, 9)
29+
Tuple.Create("Judith", 84.3, 9) |]
30+
let result = computeStatistics scores
31+
printfn $"Mean score: {result.Item2:N2} (SD={result.Item3:N2}) (n={result.Item1})"
32+
// The example displays the following output:
33+
// Mean score: 87.02 (SD=0.96) (n=8)
34+
// </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="example1.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
module compareto1
2+
3+
// <Snippet1>
4+
open System
5+
6+
let scores =
7+
[| Tuple.Create("Jack", 78.8, 8)
8+
Tuple.Create("Abbey", 92.1, 9)
9+
Tuple.Create("Dave", 88.3, 9)
10+
Tuple.Create("Sam", 91.7, 8)
11+
Tuple.Create("Ed", 71.2, 5)
12+
Tuple.Create("Penelope", 82.9, 8)
13+
Tuple.Create("Linda", 99.0, 9)
14+
Tuple.Create("Judith", 84.3, 9) |]
15+
16+
printfn "The values in unsorted order:"
17+
for score in scores do
18+
printfn $"{score}"
19+
20+
printfn ""
21+
22+
Array.Sort scores
23+
24+
printfn "The values in sorted order"
25+
for score in scores do
26+
printfn $"{score}"
27+
// The example displays the following output
28+
// The values in unsorted order:
29+
// (Jack, 78.8, 8)
30+
// (Abbey, 92.1, 9)
31+
// (Dave, 88.3, 9)
32+
// (Sam, 91.7, 8)
33+
// (Ed, 71.2, 5)
34+
// (Penelope, 82.9, 8)
35+
// (Linda, 99, 9)
36+
// (Judith, 84.3, 9)
37+
//
38+
// The values in sorted order:
39+
// (Abbey, 92.1, 9)
40+
// (Dave, 88.3, 9)
41+
// (Ed, 71.2, 5)
42+
// (Jack, 78.8, 8)
43+
// (Judith, 84.3, 9)
44+
// (Linda, 99, 9)
45+
// (Penelope, 82.9, 8)
46+
// (Sam, 91.7, 8)
47+
// </Snippet1>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
module compareto2
2+
3+
// <Snippet2>
4+
open System
5+
open System.Collections
6+
open System.Collections.Generic
7+
8+
type ScoreComparer<'T1, 'T2, 'T3>() =
9+
interface IComparer with
10+
member _.Compare(x: obj, y: obj) =
11+
match x with
12+
| :? Tuple<'T1, 'T2, 'T3> as tX ->
13+
let tY = y :?> Tuple<'T1, 'T2, 'T3>
14+
Comparer<'T2>.Default.Compare(tX.Item2, tY.Item2)
15+
| _ -> 0
16+
17+
let scores =
18+
[| Tuple.Create("Jack", 78.8, 8)
19+
Tuple.Create("Abbey", 92.1, 9)
20+
Tuple.Create("Dave", 88.3, 9)
21+
Tuple.Create("Sam", 91.7, 8)
22+
Tuple.Create("Ed", 71.2, 5)
23+
Tuple.Create("Penelope", 82.9, 8)
24+
Tuple.Create("Linda", 99.0, 9)
25+
Tuple.Create("Judith", 84.3, 9) |]
26+
27+
printfn "The values in unsorted order:"
28+
for score in scores do
29+
printfn $"{score}"
30+
31+
printfn ""
32+
33+
Array.Sort(scores, ScoreComparer<string, double, int>())
34+
35+
printfn "The values in sorted order:"
36+
for score in scores do
37+
printfn $"{score}"
38+
// The example displays the following output
39+
// The values in unsorted order:
40+
// (Jack, 78.8, 8)
41+
// (Abbey, 92.1, 9)
42+
// (Dave, 88.3, 9)
43+
// (Sam, 91.7, 8)
44+
// (Ed, 71.2, 5)
45+
// (Penelope, 82.9, 8)
46+
// (Linda, 99, 9)
47+
// (Judith, 84.3, 9)
48+
//
49+
// The values in sorted order:
50+
// (Ed, 71.2, 5)
51+
// (Jack, 78.8, 8)
52+
// (Penelope, 82.9, 8)
53+
// (Judith, 84.3, 9)
54+
// (Dave, 88.3, 9)
55+
// (Sam, 91.7, 8)
56+
// (Abbey, 92.1, 9)
57+
// (Linda, 99, 9)
58+
// </Snippet2>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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>
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="tostring1.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// <Snippet1>
2+
open System
3+
4+
let scores =
5+
[| Tuple.Create("Jack", 78.8, 8)
6+
Tuple.Create("Abbey", 92.1, 9)
7+
Tuple.Create("Dave", 88.3, 9)
8+
Tuple.Create("Sam", 91.7, 8)
9+
Tuple.Create("Ed", 71.2, 5)
10+
Tuple.Create("Penelope", 82.9, 8)
11+
Tuple.Create("Linda", 99.0, 9)
12+
Tuple.Create("Judith", 84.3, 9) |]
13+
Array.Sort scores
14+
for score in scores do
15+
printfn $"{score}"
16+
// The example displays the following output
17+
// (Abbey, 92.1, 9)
18+
// (Dave, 88.3, 9)
19+
// (Ed, 71.2, 5)
20+
// (Jack, 78.8, 8)
21+
// (Judith, 84.3, 9)
22+
// (Linda, 99, 9)
23+
// (Penelope, 82.9, 8)
24+
// (Sam, 91.7, 8)
25+
// </Snippet1>

0 commit comments

Comments
 (0)