Skip to content

Commit bfe6aec

Browse files
authored
Tuple<T1,T2,T3,T4,T5,T6> F# snippet (#7981)
1 parent eaca9f9 commit bfe6aec

File tree

13 files changed

+325
-0
lines changed

13 files changed

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

0 commit comments

Comments
 (0)