Skip to content

Commit 11e10c0

Browse files
authored
Tuple<T1,T2,T3,T4,T5,T6,T7> F# snippets (#8001)
1 parent de895a5 commit 11e10c0

File tree

13 files changed

+333
-0
lines changed

13 files changed

+333
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module equals1
2+
3+
// <Snippet1>
4+
open System
5+
6+
// Get population data for New York City and Los Angeles, 1960-2000.
7+
let urbanPopulations =
8+
[| Tuple.Create("New York", 7891957, 7781984, 7894862, 7071639, 7322564, 8008278)
9+
Tuple.Create("Los Angeles", 1970358, 2479015, 2816061, 2966850, 3485398, 3694820)
10+
Tuple.Create("New York City", 7891957, 7781984, 7894862, 7071639, 7322564, 8008278)
11+
Tuple.Create("New York", 7891957, 7781984, 7894862, 7071639, 7322564, 8008278) |]
12+
// Compare each tuple with every other tuple for equality.
13+
for ctr = 0 to urbanPopulations.Length - 2 do
14+
let urbanPopulation = urbanPopulations[ctr]
15+
printfn $"{urbanPopulation} = "
16+
for innerCtr = ctr + 1 to urbanPopulations.Length - 1 do
17+
printfn $" {urbanPopulations[innerCtr]}: {urbanPopulation.Equals urbanPopulations[innerCtr]}"
18+
printfn ""
19+
// The example displays the following output:
20+
// (New York, 7891957, 7781984, 7894862, 7071639, 7322564, 8008278) =
21+
// (Los Angeles, 1970358, 2479015, 2816061, 2966850, 3485398, 3694820): False
22+
// (New York City, 7891957, 7781984, 7894862, 7071639, 7322564, 8008278): False
23+
// (New York, 7891957, 7781984, 7894862, 7071639, 7322564, 8008278): True
24+
//
25+
// (Los Angeles, 1970358, 2479015, 2816061, 2966850, 3485398, 3694820) =
26+
// (New York City, 7891957, 7781984, 7894862, 7071639, 7322564, 8008278): False
27+
// (New York, 7891957, 7781984, 7894862, 7071639, 7322564, 8008278): False
28+
//
29+
// (New York City, 7891957, 7781984, 7894862, 7071639, 7322564, 8008278) =
30+
// (New York, 7891957, 7781984, 7894862, 7071639, 7322564, 8008278): False
31+
// </Snippet1>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// <Snippet2>
2+
open System
3+
open System.Collections
4+
5+
type RateComparer<'T1, 'T2, 'T3, 'T4, 'T5, 'T6, 'T7>() =
6+
let mutable argument = 0
7+
8+
interface IEqualityComparer with
9+
member _.Equals(x, y) =
10+
argument <- argument + 1
11+
if argument = 1 then
12+
true
13+
else
14+
if x :? Double || x :? Single then
15+
let fx = x :?> double
16+
let fy = y :?> double
17+
Math.Round(fx * 1000.).Equals(Math.Round(fy * 1000.))
18+
else
19+
x.Equals y
20+
21+
member _.GetHashCode(obj) =
22+
if obj :? Single || obj :? Double then
23+
Math.Round((obj :?> double) * 1000.).GetHashCode()
24+
else
25+
obj.GetHashCode()
26+
27+
let rate1 = Tuple.Create("New York", -0.013934, 0.014505, -0.1042733, 0.0354833, 0.093644, 0.0290792)
28+
let rate2 = Tuple.Create("Unknown City", -0.013934, 0.014505, -0.1042733, 0.0354833, 0.093644, 0.0290792)
29+
let rate3 = Tuple.Create("Unknown City", -0.013934, 0.014505, -0.1042733, 0.0354833, 0.093644, 0.029079)
30+
let rate4 = Tuple.Create("San Francisco", -0.0451934, -0.0332858, -0.0512803, 0.0662544, 0.0728964, 0.0491912)
31+
let eq: IStructuralEquatable = rate1
32+
// Compare first tuple with remaining two tuples.
33+
printfn $"{rate1} = "
34+
printfn $" {rate2} : {eq.Equals(rate2, RateComparer<string, double, double, double, double, double, double>())}"
35+
printfn $" {rate3} : {eq.Equals(rate3, RateComparer<string, double, double, double, double, double, double>())}"
36+
printfn $" {rate4} : {eq.Equals(rate4, RateComparer<string, double, double,double, double, double, double>())}"
37+
// The example displays the following output:
38+
// (New York, -0.013934, 0.014505, -0.1042733, 0.0354833, 0.093644, 0.0290792) =
39+
// (Unknown City, -0.013934, 0.014505, -0.1042733, 0.0354833, 0.093644, 0.0290792) : True
40+
// (Unknown City, -0.013934, 0.014505, -0.1042733, 0.0354833, 0.093644, 0.029079) : True
41+
// (San Francisco, -0.0451934, -0.0332858, -0.0512803, 0.0662544, 0.0728964, 0.0491912) : False
42+
// </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: 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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// <Snippet1>
2+
open System
3+
4+
// Create tuples containing population data for New York, Chicago,
5+
// and Los Angeles, 1960-2000.
6+
let cities =
7+
[| Tuple.Create("New York", 7891957, 7781984, 7894862, 7071639, 7322564, 8008278)
8+
Tuple.Create("Los Angeles", 1970358, 2479015, 2816061, 2966850, 3485398, 3694820)
9+
Tuple.Create("Chicago", 3620962, 3550904, 3366957, 3005072, 2783726, 2896016) |]
10+
11+
// Display tuple data in table.
12+
let header = "Population in"
13+
printfn $"""{"City",-12} {String('-',(66 - header.Length) / 2) + header + String('-', (66 - header.Length) / 2),66}"""
14+
printfn "%24s%11s%11s%11s%11s%11s\n" "1950" "1960" "1970" "1980" "1990" "2000"
15+
16+
for city in cities do
17+
printfn $"{city.Item1,-12} {city.Item2,11:N0}{city.Item3,11:N0}{city.Item4,11:N0}{city.Item5,11:N0}{city.Item6,11:N0}{city.Item7,11:N0}"
18+
// The example displays the following output:
19+
// City --------------------------Population in--------------------------
20+
// 1950 1960 1970 1980 1990 2000
21+
//
22+
// New York 7,891,957 7,781,984 7,894,862 7,071,639 7,322,564 8,008,278
23+
// Los Angeles 1,970,358 2,479,015 2,816,061 2,966,850 3,485,398 3,694,820
24+
// Chicago 3,620,962 3,550,904 3,366,957 3,005,072 2,783,726 2,896,016
25+
// </Snippet1>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// <Snippet1>
2+
open System
3+
4+
let computePopulationChange (data: Tuple<string, int, int, int, int, int, int>) =
5+
Tuple.Create(data.Item1,
6+
double (data.Item3 - data.Item2) / double data.Item2,
7+
double (data.Item4 - data.Item3) / double data.Item3,
8+
double (data.Item5 - data.Item4) / double data.Item4,
9+
double (data.Item6 - data.Item5) / double data.Item5,
10+
double (data.Item7 - data.Item6) / double data.Item6,
11+
double (data.Item7 - data.Item2) / double data.Item2)
12+
13+
// Get population data for New York City, 1950-2000.
14+
let population =
15+
Tuple.Create("New York", 7891957, 7781984, 7894862, 7071639, 7322564, 8008278)
16+
let rate = computePopulationChange population
17+
// Display results.
18+
printfn $"Population Change, {population.Item1}, 1950-2000\n"
19+
printfn $"""Year {"Population",10} {"Annual Rate",9}"""
20+
printfn $"""1950 {population.Item2,10:N0} {"NA",11}"""
21+
printfn $"1960 {population.Item3,10:N0} {rate.Item2 / 10.,11:P2}"
22+
printfn $"1970 {population.Item4,10:N0} {rate.Item3 / 10.,11:P2}"
23+
printfn $"1980 {population.Item5,10:N0} {rate.Item4 / 10.,11:P2}"
24+
printfn $"1990 {population.Item6,10:N0} {rate.Item5 / 10.,11:P2}"
25+
printfn $"2000 {population.Item7,10:N0} {rate.Item6 / 10.,11:P2}"
26+
printfn $"""1950-2000 {"",10:N0} {rate.Item7 / 50.,11:P2}"""
27+
28+
// The example displays the following output:
29+
// Population Change, New York, 1950-2000
30+
//
31+
// Year Population Annual Rate
32+
// 1950 7,891,957 NA
33+
// 1960 7,781,984 -0.14 %
34+
// 1970 7,894,862 0.15 %
35+
// 1980 7,071,639 -1.04 %
36+
// 1990 7,322,564 0.35 %
37+
// 2000 8,008,278 0.94 %
38+
// 1950-2000 0.03 %
39+
// </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>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module compareto1
2+
3+
// <Snippet1>
4+
open System
5+
6+
// Create array of sextuple with population data for three U.S.
7+
// cities, 1950-2000.
8+
let cities =
9+
[| Tuple.Create("Los Angeles", 1970358, 2479015, 2816061, 2966850, 3485398, 3694820)
10+
Tuple.Create("New York", 7891957, 7781984, 7894862, 7071639, 7322564, 8008278)
11+
Tuple.Create("Chicago", 3620962, 3550904, 3366957, 3005072, 2783726, 2896016) |]
12+
13+
// Display array in unsorted order.
14+
printfn $"In unsorted order:"
15+
for city in cities do
16+
printfn $"{city}"
17+
18+
printfn ""
19+
20+
Array.Sort cities
21+
22+
// Display array in sorted order.
23+
printfn "In sorted order:"
24+
for city in cities do
25+
printfn $"{city}"
26+
// The example displays the following output:
27+
// In unsorted order:
28+
// (Los Angeles, 1970358, 2479015, 2816061, 2966850, 3485398, 3694820)
29+
// (New York, 7891957, 7781984, 7894862, 7071639, 7322564, 8008278)
30+
// (Chicago, 3620962, 3550904, 3366957, 3005072, 2783726, 2896016)
31+
//
32+
// In sorted order:
33+
// (Chicago, 3620962, 3550904, 3366957, 3005072, 2783726, 2896016)
34+
// (Los Angeles, 1970358, 2479015, 2816061, 2966850, 3485398, 3694820)
35+
// (New York, 7891957, 7781984, 7894862, 7071639, 7322564, 8008278)
36+
// </Snippet1>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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>(itemPosition, descending) =
9+
let multiplier = if descending then -1 else 1
10+
11+
do
12+
if itemPosition <= 0 || itemPosition > 7 then
13+
invalidArg "itemPosition" "The itemPosition argument is out of range."
14+
15+
new (itemPosition) = PopulationComparer(itemPosition, true)
16+
17+
interface IComparer with
18+
member _.Compare(x, y) =
19+
match x with
20+
| :? Tuple<'T1, 'T2, 'T3, 'T4, 'T5, 'T6, 'T7> as tX ->
21+
let tY = y :?> Tuple<'T1, 'T2, 'T3, 'T4, 'T5, 'T6, 'T7>
22+
match itemPosition with
23+
| 1 ->
24+
Comparer<'T1>.Default.Compare(tX.Item1, tY.Item1) * multiplier
25+
| 2 ->
26+
Comparer<'T2>.Default.Compare(tX.Item2, tY.Item2) * multiplier
27+
| 3 ->
28+
Comparer<'T3>.Default.Compare(tX.Item3, tY.Item3) * multiplier
29+
| 4 ->
30+
Comparer<'T4>.Default.Compare(tX.Item4, tY.Item4) * multiplier
31+
| 5 ->
32+
Comparer<'T5>.Default.Compare(tX.Item5, tY.Item5) * multiplier
33+
| 6 ->
34+
Comparer<'T6>.Default.Compare(tX.Item6, tY.Item6) * multiplier
35+
| 7 ->
36+
Comparer<'T7>.Default.Compare(tX.Item7, tY.Item7) * multiplier
37+
| _ ->
38+
Comparer<'T1>.Default.Compare(tX.Item1, tY.Item1) * multiplier
39+
| _ -> 0
40+
41+
// Create array of sextuple with population data for three U.S.
42+
// cities, 1960-2000.
43+
let cities =
44+
[| Tuple.Create("Los Angeles", 1970358, 2479015, 2816061, 2966850, 3485398, 3694820)
45+
Tuple.Create("New York", 7891957, 7781984, 7894862, 7071639, 7322564, 8008278)
46+
Tuple.Create("Chicago", 3620962, 3550904, 3366957, 3005072, 2783726, 2896016) |]
47+
48+
// Display array in unsorted order.
49+
printfn "In unsorted order:"
50+
for city in cities do
51+
printfn $"{city}"
52+
printfn ""
53+
54+
Array.Sort(cities, PopulationComparer<string, int, int, int, int, int, int> 3)
55+
56+
// Display array in sorted order.
57+
printfn "Sorted by population in 1960:"
58+
for city in cities do
59+
printfn $"{city}"
60+
printfn ""
61+
62+
Array.Sort(cities, PopulationComparer<string, int, int, int, int, int, int> 6)
63+
64+
// Display array in sorted order.
65+
printfn "Sorted by population in 1990:"
66+
for city in cities do
67+
printfn $"{city}"
68+
// The example displays the following output ->
69+
// In unsorted order ->
70+
// (Los Angeles, 1970358, 2479015, 2816061, 2966850, 3485398, 3694820)
71+
// (New York, 7891957, 7781984, 7894862, 7071639, 7322564, 8008278)
72+
// (Chicago, 3620962, 3550904, 3366957, 3005072, 2783726, 2896016)
73+
//
74+
// Sorted by population in 1960 ->
75+
// (New York, 7891957, 7781984, 7894862, 7071639, 7322564, 8008278)
76+
// (Chicago, 3620962, 3550904, 3366957, 3005072, 2783726, 2896016)
77+
// (Los Angeles, 1970358, 2479015, 2816061, 2966850, 3485398, 3694820)
78+
//
79+
// Sorted by population in 1990 ->
80+
// (New York, 7891957, 7781984, 7894862, 7071639, 7322564, 8008278)
81+
// (Los Angeles, 1970358, 2479015, 2816061, 2966850, 3485398, 3694820)
82+
// (Chicago, 3620962, 3550904, 3366957, 3005072, 2783726, 2896016)
83+
// </Snippet2>
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>

0 commit comments

Comments
 (0)