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

Skip to content

System.Tuple<T1,T2,T3,T4,T5,T6> F# snippet #7981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions snippets/fsharp/System/TupleT1,T2,T3,T4,T5,T6/Equals/equals1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module equals1

// <Snippet1>
open System

// Get population data for New York City and Los Angeles, 1960-2000.
let urbanPopulations =
[| Tuple.Create("New York", 7781984, 7894862, 7071639, 7322564, 8008278)
Tuple.Create("Los Angeles", 2479015, 2816061, 2966850, 3485398, 3694820)
Tuple.Create("New York City", 7781984, 7894862, 7071639, 7322564, 8008278)
Tuple.Create("New York", 7781984, 7894862, 7071639, 7322564, 8008278) |]
// Compare each tuple with every other tuple for equality.
for ctr = 0 to urbanPopulations.Length - 2 do
let urbanPopulation = urbanPopulations[ctr]
printfn $"{urbanPopulation} = "
for innerCtr = ctr + 1 to urbanPopulations.Length - 1 do
printfn $" {urbanPopulations[innerCtr]}: {urbanPopulation.Equals urbanPopulations[innerCtr]}"
printfn ""
// The example displays the following output:
// (New York, 7781984, 7894862, 7071639, 7322564, 8008278) =
// (Los Angeles, 2479015, 2816061, 2966850, 3485398, 3694820): False
// (New York City, 7781984, 7894862, 7071639, 7322564, 8008278): False
// (New York, 7781984, 7894862, 7071639, 7322564, 8008278): True
//
// (Los Angeles, 2479015, 2816061, 2966850, 3485398, 3694820) =
// (New York City, 7781984, 7894862, 7071639, 7322564, 8008278): False
// (New York, 7781984, 7894862, 7071639, 7322564, 8008278): False
//
// (New York City, 7781984, 7894862, 7071639, 7322564, 8008278) =
// (New York, 7781984, 7894862, 7071639, 7322564, 8008278): False
// </Snippet1>
41 changes: 41 additions & 0 deletions snippets/fsharp/System/TupleT1,T2,T3,T4,T5,T6/Equals/equals2.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// <Snippet2>
open System
open System.Collections

type RateComparer<'T1, 'T2, 'T3, 'T4, 'T5, 'T6>() =
let mutable argument = 0

interface IEqualityComparer with
member _.Equals(x, y) =
argument <- argument + 1
if argument = 1 then true
else
match x with
| :? double as fx ->
let fy = y :?> double
Math.Round(fx * 1000.).Equals(Math.Round(fy * 1000.))
| _ ->
x.Equals y

member _.GetHashCode(obj) =
if obj :? Single || obj :? Double then
Math.Round((obj :?> double) * 1000.).GetHashCode()
else
obj.GetHashCode()

let rate1 = Tuple.Create("New York", 0.014505, -0.1042733, 0.0354833, 0.093644, 0.0290792)
let rate2 = Tuple.Create("Unknown City", 0.014505, -0.1042733, 0.0354833, 0.093644, 0.0290792)
let rate3 = Tuple.Create("Unknown City", 0.014505, -0.1042733, 0.0354833, 0.093644, 0.029079)
let rate4 = Tuple.Create("San Francisco", -0.0332858, -0.0512803, 0.0662544, 0.0728964, 0.0491912)
let eq: IStructuralEquatable = rate1
// Compare first tuple with remaining two tuples.
printfn $"{rate1} = "
printfn $" {rate2} : {eq.Equals(rate2, RateComparer<string, double, double, double, double, double>())}"
printfn $" {rate3} : {eq.Equals(rate3, RateComparer<string, double, double, double, double, double>())}"
printfn $" {rate4} : {eq.Equals(rate4, RateComparer<string, double, double, double, double, double>())}"
// The example displays the following output:
// (New York, 0.014505, -0.1042733, 0.0354833, 0.093644, 0.0290792) =
// (Unknown City, 0.014505, -0.1042733, 0.0354833, 0.093644, 0.0290792) : True
// (Unknown City, 0.014505, -0.1042733, 0.0354833, 0.093644, 0.029079) : True
// (San Francisco, -0.0332858, -0.0512803, 0.0662544, 0.0728964, 0.0491912) : False
// </Snippet2>
11 changes: 11 additions & 0 deletions snippets/fsharp/System/TupleT1,T2,T3,T4,T5,T6/Equals/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="equals1.fs" />
<Compile Include="equals2.fs" />
</ItemGroup>
</Project>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/TupleT1,T2,T3,T4,T5,T6/Item1/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="item1.fs" />
</ItemGroup>
</Project>
25 changes: 25 additions & 0 deletions snippets/fsharp/System/TupleT1,T2,T3,T4,T5,T6/Item1/item1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// <Snippet1>
open System

// Create tuples containing population data for New York, Chicago,
// and Los Angeles, 1960-2000.
let cities =
[| Tuple.Create("New York", 7781984, 7894862, 7071639, 7322564, 8008278)
Tuple.Create("Los Angeles", 2479015, 2816061, 2966850, 3485398, 3694820)
Tuple.Create("Chicago", 3550904, 3366957, 3005072, 2783726, 2896016) |]

// Display tuple data in table.
let header = "Population in"
printfn $"""{"City",-12} {String('-',(60 - header.Length) / 2) + header + String('-', (60 - header.Length) / 2),60}"""
printfn "%25s%12s%12s%12s%12s\n" "1960" "1970" "1980" "1990" "2000"

for city in cities do
printfn $"{city.Item1,-12} {city.Item2,12:N0}{city.Item3,12:N0}{city.Item4,12:N0}{city.Item5,12:N0}{city.Item6,12:N0}"
// The example displays the following output:
// City -----------------------Population in-----------------------
// 1960 1970 1980 1990 2000
//
// New York 7,781,984 7,894,862 7,071,639 7,322,564 8,008,278
// Los Angeles 2,479,015 2,816,061 2,966,850 3,485,398 3,694,820
// Chicago 3,550,904 3,366,957 3,005,072 2,783,726 2,896,016
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// <Snippet1>
open System

let computePopulationChange (data: Tuple<string, int, int, int, int, int>) =
Tuple.Create(data.Item1,
double ((data.Item3 - data.Item2) / data.Item2),
double ((data.Item4 - data.Item3) / data.Item3),
double ((data.Item5 - data.Item4) / data.Item4),
double ((data.Item6 - data.Item5) / data.Item5),
double ((data.Item6 - data.Item2) / data.Item2))

// Get population data for New York City, 1960-2000.
let population =
Tuple.Create("New York", 7781984, 7894862, 7071639, 7322564, 8008278)
let rate = computePopulationChange population
// Display results.
printfn $"Population Change, {population.Item1}, 1960-2000\n"
printfn $"""Year {"Population",10} {"Annual Rate",9}"""
printfn $"""1960 {population.Item2,10:N0} {"NA",11}"""
printfn $"1970 {population.Item3,10:N0} {rate.Item2 / 10.,11:P2}"
printfn $"1980 {population.Item4,10:N0} {rate.Item3 / 10.,11:P2}"
printfn $"1990 {population.Item5,10:N0} {rate.Item4 / 10.,11:P2}"
printfn $"2000 {population.Item6,10:N0} {rate.Item5 / 10.,11:P2}"
printfn $"""1960-2000 {"",10:N0} {rate.Item6 / 50.,11:P2}"""

// The example displays the following output:
// Population Change, New York, 1960-2000
//
// Year Population Annual Rate
// 1960 7,781,984 NA
// 1970 7,894,862 0.15 %
// 1980 7,071,639 -1.04 %
// 1990 7,322,564 0.35 %
// 2000 8,008,278 0.94 %
// 1960-2000 0.06 %
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="example1.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module compareto1

// <Snippet1>
open System

// Create array of sextuple with population data for three U.S.
// cities, 1960-2000.
let cities =
[| Tuple.Create("Los Angeles", 2479015, 2816061, 2966850, 3485398, 3694820)
Tuple.Create("New York", 7781984, 7894862, 7071639, 7322564, 8008278)
Tuple.Create("Chicago", 3550904, 3366957, 3005072, 2783726, 2896016) |]

// Display array in unsorted order.
printfn "In unsorted order:"
for city in cities do
printfn $"{city}"

printfn ""

Array.Sort cities

// Display array in sorted order.
printfn "In sorted order:"
for city in cities do
printfn $"{city}"
// The example displays the following output:
// In unsorted order:
// (Los Angeles, 2479015, 2816061, 2966850, 3485398, 3694820)
// (New York, 7781984, 7894862, 7071639, 7322564, 8008278)
// (Chicago, 3550904, 3366957, 3005072, 2783726, 2896016)
//
// In sorted order:
// (Chicago, 3550904, 3366957, 3005072, 2783726, 2896016)
// (Los Angeles, 2479015, 2816061, 2966850, 3485398, 3694820)
// (New York, 7781984, 7894862, 7071639, 7322564, 8008278)
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
module compareto2

// <Snippet2>
open System
open System.Collections
open System.Collections.Generic

type PopulationComparer<'T1, 'T2, 'T3, 'T4, 'T5, 'T6>(comp, descending) =
let multiplier = if descending then -1 else 1

do
if comp <= 0 || comp > 6 then
invalidArg "comp" "The component argument is out of range."

new (comp) = PopulationComparer(comp, true)

interface IComparer with
member _.Compare(x, y) =
match x with
| :? Tuple<'T1, 'T2, 'T3, 'T4, 'T5, 'T6> as tX ->
let tY = y :?> Tuple<'T1, 'T2, 'T3, 'T4, 'T5, 'T6>
match comp with
| 1 ->
Comparer<'T1>.Default.Compare(tX.Item1, tY.Item1) * multiplier
| 2 ->
Comparer<'T2>.Default.Compare(tX.Item2, tY.Item2) * multiplier
| 3 ->
Comparer<'T3>.Default.Compare(tX.Item3, tY.Item3) * multiplier
| 4 ->
Comparer<'T4>.Default.Compare(tX.Item4, tY.Item4) * multiplier
| 5 ->
Comparer<'T5>.Default.Compare(tX.Item5, tY.Item5) * multiplier
| 6 ->
Comparer<'T6>.Default.Compare(tX.Item6, tY.Item6) * multiplier
| _ ->
Comparer<'T1>.Default.Compare(tX.Item1, tY.Item1) * multiplier
| _ -> 0

// Create array of sextuple with population data for three U.S.
// cities, 1960-2000.
let cities =
[| Tuple.Create("Los Angeles", 2479015, 2816061, 2966850, 3485398, 3694820)
Tuple.Create("New York", 7781984, 7894862, 7071639, 7322564, 8008278)
Tuple.Create("Chicago", 3550904, 3366957, 3005072, 2783726, 2896016) |]

// Display array in unsorted order.
printfn "In unsorted order:"
for city in cities do
printfn $"{city}"
printfn ""

Array.Sort(cities, PopulationComparer<string, int, int, int, int, int> 3)

// Display array in sorted order.
printfn "Sorted by population in 1970:"
for city in cities do
printfn $"{city}"
printfn ""

Array.Sort(cities, PopulationComparer<string, int, int, int, int, int> 6)

// Display array in sorted order.
printfn "Sorted by population in 2000:"
for city in cities do
printfn $"{city}"
// The example displays the following output:
// In unsorted order:
// (Los Angeles, 2479015, 2816061, 2966850, 3485398, 3694820)
// (New York, 7781984, 7894862, 7071639, 7322564, 8008278)
// (Chicago, 3550904, 3366957, 3005072, 2783726, 2896016)
//
// Sorted by population in 1970:
// (New York, 7781984, 7894862, 7071639, 7322564, 8008278)
// (Chicago, 3550904, 3366957, 3005072, 2783726, 2896016)
// (Los Angeles, 2479015, 2816061, 2966850, 3485398, 3694820)
//
// Sorted by population in 2000:
// (New York, 7781984, 7894862, 7071639, 7322564, 8008278)
// (Los Angeles, 2479015, 2816061, 2966850, 3485398, 3694820)
// (Chicago, 3550904, 3366957, 3005072, 2783726, 2896016)
// </Snippet2>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="compareto2.fs" />
<Compile Include="compareto1.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="tostring1.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// <Snippet1>
open System

// Get population data for New York City, 1960-2000.
let population = Tuple.Create("New York", 7781984, 7894862, 7071639, 7322564, 8008278)
printfn $"{population.ToString()}"
// The example displays the following output:
// (New York, 7781984, 7894862, 7071639, 7322564, 8008278)
// </Snippet1>
Loading