Skip to content

Commit bde46e2

Browse files
authored
Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> F# snippets (#8029)
1 parent 585dc6b commit bde46e2

File tree

14 files changed

+386
-0
lines changed

14 files changed

+386
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
module example
2+
3+
open System
4+
5+
let showPopulation year newPopulation =
6+
printfn $"""{year,5} {newPopulation,14:N0} {"n/a",10:P2}"""
7+
8+
let showPopulationChange year newPopulation oldPopulation =
9+
printfn $"{year,5} {newPopulation,14:N0} {(double (newPopulation - oldPopulation) / oldPopulation) / 10.,10:P2}"
10+
11+
// <Snippet19>
12+
let from1980 = Tuple.Create(1203339, 1027974, 951270)
13+
let from1910 = new Tuple<int, int, int, int, int, int, int, Tuple<int, int, int>>(465766, 993078, 1568622, 1623452, 1849568, 1670144, 1511462, from1980)
14+
let population = new Tuple<string, int, int, int, int, int, int, Tuple<int, int, int, int, int, int, int, Tuple<int, int, int>>>("Detroit", 1860, 45619, 79577, 116340, 205876, 285704, from1910)
15+
// </Snippet19>
16+
printfn $"Population of {population.Item1}\n"
17+
printfn "%5s %14s %10s" "Year" "Population" "Change"
18+
19+
do
20+
let year = population.Item2
21+
showPopulation year population.Item3
22+
let year = year + 10
23+
showPopulationChange year population.Item4 population.Item3
24+
let year = year + 10
25+
showPopulationChange year population.Item5 population.Item4
26+
let year = year + 10
27+
showPopulationChange year population.Item6 population.Item5
28+
let year = year + 10
29+
showPopulationChange year population.Item7 population.Item6
30+
let year = year + 10
31+
showPopulationChange year population.Rest.Item1 population.Item7
32+
let year = year + 10
33+
showPopulationChange year population.Rest.Item2 population.Rest.Item1
34+
let year = year + 10
35+
showPopulationChange year population.Rest.Item3 population.Rest.Item2
36+
let year = year + 10
37+
showPopulationChange year population.Rest.Item4 population.Rest.Item3
38+
let year = year + 10
39+
showPopulationChange year population.Rest.Item5 population.Rest.Item4
40+
let year = year + 10
41+
showPopulationChange year population.Rest.Item6 population.Rest.Item5
42+
let year = year + 10
43+
showPopulationChange year population.Rest.Item7 population.Rest.Item6
44+
let year = year + 10
45+
showPopulationChange year population.Rest.Rest.Item1 population.Rest.Item7
46+
let year = year + 10
47+
showPopulationChange year population.Rest.Rest.Item2 population.Rest.Rest.Item1
48+
let year = year + 10
49+
showPopulationChange year population.Rest.Rest.Item3 population.Rest.Rest.Item2
50+
51+
// The example displays the following output:
52+
//
53+
// Population of Detroit
54+
// Year Population Change
55+
// 1860 45,619 n/a
56+
// 1870 79,577 7.44 %
57+
// 1880 116,340 4.62 %
58+
// 1890 205,876 7.70 %
59+
// 1900 285,704 3.88 %
60+
// 1910 465,766 6.30 %
61+
// 1920 993,078 11.32 %
62+
// 1930 1,568,622 5.80 %
63+
// 1940 1,623,452 0.35 %
64+
// 1950 1,849,568 1.39 %
65+
// 1960 1,670,144 -0.97 %
66+
// 1970 1,511,462 -0.95 %
67+
// 1980 1,203,339 -2.04 %
68+
// 1990 1,027,974 -1.46 %
69+
// 2000 951,270 -0.75 %

snippets/fsharp/System/Tuple/Overview/fs.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
<ItemGroup>
88
<Compile Include="example1.fs" />
9+
<Compile Include="example.fs" />
910
<Compile Include="createntuple.fs" />
1011
<Compile Include="create1.fs" />
1112
<Compile Include="ctor8.fs" />
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+
// Create five 8-tuple objects containing prime numbers.
5+
let prime1 =
6+
new Tuple<Int32, Int32, Int32, Int32, Int32, Int32, Int32,
7+
Tuple<Int32>> (2, 3, 5, 7, 11, 13, 17,
8+
new Tuple<Int32>(19))
9+
let prime2 =
10+
new Tuple<Int32, Int32, Int32, Int32, Int32, Int32, Int32,
11+
Tuple<Int32>> (23, 29, 31, 37, 41, 43, 47,
12+
new Tuple<Int32>(55))
13+
let prime3 =
14+
new Tuple<Int32, Int32, Int32, Int32, Int32, Int32, Int32,
15+
Tuple<Int32>> (3, 2, 5, 7, 11, 13, 17,
16+
new Tuple<Int32>(19))
17+
let prime4 =
18+
new Tuple<Int32, Int32, Int32, Int32, Int32, Int32, Int32,
19+
Tuple<Int32, Int32>> (2, 3, 5, 7, 11, 13, 17,
20+
new Tuple<Int32, Int32>(19, 23))
21+
let prime5 =
22+
new Tuple<Int32, Int32, Int32, Int32, Int32, Int32, Int32,
23+
Tuple<Int32>> (2, 3, 5, 7, 11, 13, 17,
24+
new Tuple<Int32>(19))
25+
printfn $"{prime1} = {prime2} : {prime1.Equals prime2}"
26+
printfn $"{prime1} = {prime3} : {prime1.Equals prime3}"
27+
printfn $"{prime1} = {prime4} : {prime1.Equals prime4}"
28+
printfn $"{prime1} = {prime5} : {prime1.Equals prime5}"
29+
// The example displays the following output:
30+
// (2, 3, 5, 7, 11, 13, 17, 19) = (23, 29, 31, 37, 41, 43, 47, 55) : False
31+
// (2, 3, 5, 7, 11, 13, 17, 19) = (3, 2, 5, 7, 11, 13, 17, 19) : False
32+
// (2, 3, 5, 7, 11, 13, 17, 19) = (2, 3, 5, 7, 11, 13, 17, 19, 23) : False
33+
// (2, 3, 5, 7, 11, 13, 17, 19) = (2, 3, 5, 7, 11, 13, 17, 19) : True
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="equals1.fs" />
9+
</ItemGroup>
10+
</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="item1.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
open System
2+
3+
let showPopulation year newPopulation =
4+
printfn $"""{year,5} {newPopulation,14:N0} {"n/a",10:P2}"""
5+
6+
let showPopulationChange year newPopulation oldPopulation =
7+
printfn $"{year,5} {newPopulation,14:N0} {(double (newPopulation - oldPopulation) / oldPopulation) / 10.,10:P2}"
8+
9+
// <Snippet19>
10+
let from1980 = Tuple.Create(1203339, 1027974, 951270)
11+
let from1910 = new Tuple<int, int, int, int, int, int, int, Tuple<int, int, int>>(465766, 993078, 1568622, 1623452, 1849568, 1670144, 1511462, from1980)
12+
let population = new Tuple<string, int, int, int, int, int, int, Tuple<int, int, int, int, int, int, int, Tuple<int, int, int>>>("Detroit", 1860, 45619, 79577, 116340, 205876, 285704, from1910)
13+
// </Snippet19>
14+
printfn $"Population of {population.Item1}\n"
15+
printfn "%5s %14s %10s" "Year" "Population" "Change"
16+
17+
do
18+
let year = population.Item2
19+
showPopulation year population.Item3
20+
let year = year + 10
21+
showPopulationChange year population.Item4 population.Item3
22+
let year = year + 10
23+
showPopulationChange year population.Item5 population.Item4
24+
let year = year + 10
25+
showPopulationChange year population.Item6 population.Item5
26+
let year = year + 10
27+
showPopulationChange year population.Item7 population.Item6
28+
let year = year + 10
29+
showPopulationChange year population.Rest.Item1 population.Item7
30+
let year = year + 10
31+
showPopulationChange year population.Rest.Item2 population.Rest.Item1
32+
let year = year + 10
33+
showPopulationChange year population.Rest.Item3 population.Rest.Item2
34+
let year = year + 10
35+
showPopulationChange year population.Rest.Item4 population.Rest.Item3
36+
let year = year + 10
37+
showPopulationChange year population.Rest.Item5 population.Rest.Item4
38+
let year = year + 10
39+
showPopulationChange year population.Rest.Item6 population.Rest.Item5
40+
let year = year + 10
41+
showPopulationChange year population.Rest.Item7 population.Rest.Item6
42+
let year = year + 10
43+
showPopulationChange year population.Rest.Rest.Item1 population.Rest.Item7
44+
let year = year + 10
45+
showPopulationChange year population.Rest.Rest.Item2 population.Rest.Rest.Item1
46+
let year = year + 10
47+
showPopulationChange year population.Rest.Rest.Item3 population.Rest.Rest.Item2
48+
49+
// The example displays the following output:
50+
//
51+
// Population of Detroit
52+
// Year Population Change
53+
// 1860 45,619 n/a
54+
// 1870 79,577 7.44 %
55+
// 1880 116,340 4.62 %
56+
// 1890 205,876 7.70 %
57+
// 1900 285,704 3.88 %
58+
// 1910 465,766 6.30 %
59+
// 1920 993,078 11.32 %
60+
// 1930 1,568,622 5.80 %
61+
// 1940 1,623,452 0.35 %
62+
// 1950 1,849,568 1.39 %
63+
// 1960 1,670,144 -0.97 %
64+
// 1970 1,511,462 -0.95 %
65+
// 1980 1,203,339 -2.04 %
66+
// 1990 1,027,974 -1.46 %
67+
// 2000 951,270 -0.75 %
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="octuple1.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
open System
2+
3+
// <Snippet1>
4+
let primes = new Tuple<Int32, Int32, Int32, Int32, Int32, Int32, Int32,
5+
Tuple<Int32>> (2, 3, 5, 7, 11, 13, 17, new Tuple<Int32>(19))
6+
// </Snippet1>
7+
printfn $"{primes}"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module compareto1
2+
3+
// <Snippet1>
4+
open System
5+
6+
// Create array of 8-tuple objects containing prime numbers.
7+
let primes =
8+
[| new Tuple<Int32, Int32, Int32, Int32, Int32, Int32, Int32,
9+
Tuple<Int32>>(2, 3, 5, 7, 11, 13, 17, new Tuple<Int32>(19))
10+
new Tuple<Int32, Int32, Int32, Int32, Int32, Int32, Int32,
11+
Tuple<Int32>>(23, 29, 31, 37, 41, 43, 47, new Tuple<Int32>(55))
12+
new Tuple<Int32, Int32, Int32, Int32, Int32, Int32, Int32,
13+
Tuple<Int32>>(3, 2, 5, 7, 11, 13, 17, new Tuple<Int32>(19)) |]
14+
// Display 8-tuples in unsorted order.
15+
for prime in primes do
16+
printfn $"{prime}"
17+
printfn ""
18+
19+
// Sort the array and display its 8-tuples.
20+
Array.Sort primes
21+
for prime in primes do
22+
printfn $"{prime}"
23+
// The example displays the following output:
24+
// (2, 3, 5, 7, 11, 13, 17, 19)
25+
// (23, 29, 31, 37, 41, 43, 47, 55)
26+
// (3, 2, 5, 7, 11, 13, 17, 19)
27+
//
28+
// (2, 3, 5, 7, 11, 13, 17, 19)
29+
// (3, 2, 5, 7, 11, 13, 17, 19)
30+
// (23, 29, 31, 37, 41, 43, 47, 55)
31+
// </Snippet1>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
module compareto2
2+
3+
// <Snippet2>
4+
open System
5+
open System.Collections
6+
open System.Collections.Generic
7+
8+
type PopulationComparer<'T1, 'T2, 'T3, 'T4, 'T5, 'T6, 'T7, 'T8>(itemPosition, descending) =
9+
let multiplier = if descending then -1 else 1
10+
11+
do
12+
if itemPosition <= 0 || itemPosition > 8 then
13+
invalidArg "itemPosition" "The component argument is out of range."
14+
new(itemPosition) = PopulationComparer (itemPosition, true)
15+
16+
interface IComparer with
17+
member _.Compare(x, y) =
18+
match x with
19+
| :? Tuple<'T1, 'T2, 'T3, 'T4, 'T5, 'T6, 'T7, Tuple<'T8>> as tX ->
20+
let tY = y :?> Tuple<'T1, 'T2, 'T3, 'T4, 'T5, 'T6, 'T7, Tuple<'T8>>
21+
match itemPosition with
22+
| 1 ->
23+
Comparer<'T1>.Default.Compare(tX.Item1, tY.Item1) * multiplier
24+
| 2 ->
25+
Comparer<'T2>.Default.Compare(tX.Item2, tY.Item2) * multiplier
26+
| 3 ->
27+
Comparer<'T3>.Default.Compare(tX.Item3, tY.Item3) * multiplier
28+
| 4 ->
29+
Comparer<'T4>.Default.Compare(tX.Item4, tY.Item4) * multiplier
30+
| 5 ->
31+
Comparer<'T5>.Default.Compare(tX.Item5, tY.Item5) * multiplier
32+
| 6 ->
33+
Comparer<'T6>.Default.Compare(tX.Item6, tY.Item6) * multiplier
34+
| 7 ->
35+
Comparer<'T7>.Default.Compare(tX.Item7, tY.Item7) * multiplier
36+
| 8 ->
37+
Comparer<'T8>.Default.Compare(tX.Rest.Item1, tY.Rest.Item1) * multiplier
38+
| _ ->
39+
Comparer<'T1>.Default.Compare(tX.Item1, tY.Item1) * multiplier
40+
| _ -> 0
41+
42+
// Create array of octuples with population data for three U.S.
43+
// cities, 1940-2000.
44+
let cities =
45+
[| Tuple.Create("Los Angeles", 1504277, 1970358, 2479015, 2816061, 2966850, 3485398, 3694820)
46+
Tuple.Create("Chicago", 3396808, 3620962, 3550904, 3366957, 3005072, 2783726, 2896016)
47+
Tuple.Create("New York", 7454995, 7891957, 7781984, 7894862, 7071639, 7322564, 8008278)
48+
Tuple.Create("Detroit", 1623452, 1849568, 1670144, 1511462, 1203339, 1027974, 951270) |]
49+
// Display array in unsorted order.
50+
printfn "In unsorted order:"
51+
for city in cities do
52+
printfn $"{city}"
53+
printfn ""
54+
55+
Array.Sort(cities, PopulationComparer<string, int, int, int, int, int, int, int> 2)
56+
57+
// Display array in sorted order.
58+
printfn "Sorted by population in 1950:"
59+
for city in cities do
60+
printfn $"{city}"
61+
printfn ""
62+
63+
Array.Sort(cities, PopulationComparer<string, int, int, int, int, int, int, int>(8))
64+
65+
// Display array in sorted order.
66+
printfn "Sorted by population in 2000:"
67+
for city in cities do
68+
printfn $"{city}"
69+
// The example displays the following output:
70+
// In unsorted order:
71+
// (Los Angeles, 1504277, 1970358, 2479015, 2816061, 2966850, 3485398, 3694820)
72+
// (New York, 7454995, 7891957, 7781984, 7894862, 7071639, 7322564, 8008278)
73+
// (Chicago, 3396808, 3620962, 3550904, 3366957, 3005072, 2783726, 2896016)
74+
// (Detroit, 1623452, 1849568, 1670144, 1511462, 1203339, 1027974, 951270)
75+
//
76+
// Sorted by population in 1950:
77+
// (New York, 7454995, 7891957, 7781984, 7894862, 7071639, 7322564, 8008278)
78+
// (Chicago, 3396808, 3620962, 3550904, 3366957, 3005072, 2783726, 2896016)
79+
// (Detroit, 1623452, 1849568, 1670144, 1511462, 1203339, 1027974, 951270)
80+
// (Los Angeles, 1504277, 1970358, 2479015, 2816061, 2966850, 3485398, 3694820)
81+
//
82+
// Sorted by population in 2000:
83+
// (New York, 7454995, 7891957, 7781984, 7894862, 7071639, 7322564, 8008278)
84+
// (Los Angeles, 1504277, 1970358, 2479015, 2816061, 2966850, 3485398, 3694820)
85+
// (Chicago, 3396808, 3620962, 3550904, 3366957, 3005072, 2783726, 2896016)
86+
// (Detroit, 1623452, 1849568, 1670144, 1511462, 1203339, 1027974, 951270)
87+
// </Snippet2>

0 commit comments

Comments
 (0)