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

Skip to content

System.Tuple<T1,T2,T3,T4,T5> F# snippets #7948

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 12, 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
40 changes: 40 additions & 0 deletions snippets/fsharp/System/TupleT1,T2,T3,T4,T5/Equals/equals1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module equals1

// <Snippet1>
open System

let temperatureInfos =
[| Tuple.Create(2, 97.9, 97.8, 98.0, 98.2)
Tuple.Create(1, 98.6, 98.8, 98.8, 99.0)
Tuple.Create(2, 98.6, 98.6, 98.6, 98.4)
Tuple.Create(1, 98.4, 98.6, 99.0, 99.2)
Tuple.Create(2, 98.6, 98.6, 98.6, 98.4)
Tuple.Create(1, 98.6, 98.8, 98.8, 99.0) |]
// Compare each item with every other item for equality.
for ctr = 0 to temperatureInfos.Length - 1 do
let temperatureInfo = temperatureInfos[ctr]
for ctr2 = ctr + 1 to temperatureInfos.Length - 1 do
printfn $"{temperatureInfo} = {temperatureInfos[ctr2]}: {temperatureInfo.Equals temperatureInfos[ctr2]}"
printfn ""

// The example displays the following output:
// (2, 97.9, 97.8, 98, 98.2) = (1, 98.6, 98.8, 98.8, 99): False
// (2, 97.9, 97.8, 98, 98.2) = (2, 98.6, 98.6, 98.6, 98.4): False
// (2, 97.9, 97.8, 98, 98.2) = (1, 98.4, 98.6, 99, 99.2): False
// (2, 97.9, 97.8, 98, 98.2) = (2, 98.6, 98.6, 98.6, 98.4): False
// (2, 97.9, 97.8, 98, 98.2) = (1, 98.6, 98.8, 98.8, 99): False
//
// (1, 98.6, 98.8, 98.8, 99) = (2, 98.6, 98.6, 98.6, 98.4): False
// (1, 98.6, 98.8, 98.8, 99) = (1, 98.4, 98.6, 99, 99.2): False
// (1, 98.6, 98.8, 98.8, 99) = (2, 98.6, 98.6, 98.6, 98.4): False
// (1, 98.6, 98.8, 98.8, 99) = (1, 98.6, 98.8, 98.8, 99): True
//
// (2, 98.6, 98.6, 98.6, 98.4) = (1, 98.4, 98.6, 99, 99.2): False
// (2, 98.6, 98.6, 98.6, 98.4) = (2, 98.6, 98.6, 98.6, 98.4): True
// (2, 98.6, 98.6, 98.6, 98.4) = (1, 98.6, 98.8, 98.8, 99): False
//
// (1, 98.4, 98.6, 99, 99.2) = (2, 98.6, 98.6, 98.6, 98.4): False
// (1, 98.4, 98.6, 99, 99.2) = (1, 98.6, 98.8, 98.8, 99): False
//
// (2, 98.6, 98.6, 98.6, 98.4) = (1, 98.6, 98.8, 98.8, 99): False
// </Snippet1>
40 changes: 40 additions & 0 deletions snippets/fsharp/System/TupleT1,T2,T3,T4,T5/Equals/equals2.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module equals2

// <Snippet2>
open System
open System.Collections

type DoubleComparer<'T1, 'T2, 'T3, 'T4, 'T5 when 'T1: equality and 'T2: equality and 'T3: equality and 'T4: equality and 'T5: equality>(difference: double) =
let mutable argument = 0

interface IEqualityComparer with
member _.Equals(x, y) =
argument <- argument + 1

// Return true for Item1.
if argument = 1 then
true
else
let d1 = x :?> double
let d2 = y :?> double

d1 - d2 < d1 * difference

member _.GetHashCode(obj) =
match obj with
| :? 'T1 as obj -> obj.GetHashCode()
| :? 'T2 as obj -> obj.GetHashCode()
| :? 'T3 as obj -> obj.GetHashCode()
| :? 'T4 as obj -> obj.GetHashCode()
| _ -> (obj :?> 'T5).GetHashCode()

let getValues ctr =
// Generate four random numbers between 0 and 1
let rnd = Random(DateTime.Now.Ticks >>> 32 >>> ctr |> int)
Tuple.Create(ctr, rnd.NextDouble(), rnd.NextDouble(), rnd.NextDouble(), rnd.NextDouble())

let value1 = getValues 1
let value2 = getValues 2
let iValue1 = value1
printfn $"{value1} =\n{value2} :\n{(DoubleComparer<int, double, double, double, double> 0.01 :> IEqualityComparer).Equals(iValue1, value2)}"
// </Snippet2>
11 changes: 11 additions & 0 deletions snippets/fsharp/System/TupleT1,T2,T3,T4,T5/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/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>
22 changes: 22 additions & 0 deletions snippets/fsharp/System/TupleT1,T2,T3,T4,T5/Item1/item1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module item1

// <Snippet1>
open System

// Define array of tuples reflecting population change by state, 1990-2000.
let statesData =
[| Tuple.Create("California", 29760021, 33871648, 4111627, 13.8)
Tuple.Create("Illinois", 11430602, 12419293, 988691, 8.6)
Tuple.Create("Washington", 4866692, 5894121, 1027429, 21.1) |]

// Display the items of each tuple
printfn "%-12s%18s%18s%15s%12s\n" "State" "Population 1990" "Population 2000" "Change" "% Change"
for stateData in statesData do
printfn $"{stateData.Item1,-12}{stateData.Item2,18:N0}{stateData.Item3,18:N0}{stateData.Item4,15:N0}{stateData.Item5,12:P1}"
// The example displays the following output:
// State Population 1990 Population 2000 Change % Change
//
// California 29,760,021 33,871,648 4,111,627 13.8 %
// Illinois 11,430,602 12,419,293 988,691 8.6 %
// Washington 4,866,692 5,894,121 1,027,429 21.1 %
// </Snippet1>
52 changes: 52 additions & 0 deletions snippets/fsharp/System/TupleT1,T2,T3,T4,T5/Overview/example1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// <Snippet1>
open System

let computeStatistics (players: Tuple<string, int, int, int, int>[]) =
[| for player in players do
// Create result object containing player name and statistics.
Tuple.Create(player.Item1,
double player.Item3 / double player.Item2,
double player.Item4 / double player.Item2,
double player.Item4 / double player.Item3,
double player.Item5 / double player.Item3) |]


// Organization of runningBacks 5-tuple:
// Component 1: Player name
// Component 2: Number of games played
// Component 3: Number of attempts (carries)
// Component 4: Number of yards gained
// Component 5: Number of touchdowns
let runningBacks =
[| Tuple.Create("Payton, Walter", 190, 3838, 16726, 110)
Tuple.Create("Sanders, Barry", 153, 3062, 15269, 99)
Tuple.Create("Brown, Jim", 118, 2359, 12312, 106)
Tuple.Create("Dickerson, Eric", 144, 2996, 13259, 90)
Tuple.Create("Faulk, Marshall", 176, 2836, 12279, 100) |]
// Calculate statistics.
// Organization of runningStats 5-tuple:
// Component 1: Player name
// Component 2: Number of attempts per game
// Component 3: Number of yards per game
// Component 4: Number of yards per attempt
// Component 5: Number of touchdowns per attempt
let runningStats = computeStatistics runningBacks

// Display the result.
printfn "%-16s %5s %6s %7s %7s %7s %7s %5s %7s\n" "Name" "Games" "Att" "Att/Gm" "Yards" "Yds/Gm" "Yds/Att" "TD" "TD/Att"
for i = 0 to runningBacks.Length - 1 do
printfn $"{runningBacks[i].Item1,-16} {runningBacks[i].Item2,5} {runningBacks[i].Item3,6:N0} {runningBacks[i].Item2,7:N1} {runningBacks[i].Item4,7:N0} {runningBacks[i].Item3,7:N1} {runningBacks[i].Item4,7:N2} {runningBacks[i].Item5,5} {runningBacks[i].Item5,7:N3}\n"

// The example displays the following output:
// Name Games Att Att/Gm Yards Yds/Gm Yds/Att TD TD/Att
//
// Payton, Walter 190 3,838 20.2 16,726 88.0 4.36 110 0.029
//
// Sanders, Barry 153 3,062 20.0 15,269 99.8 4.99 99 0.032
//
// Brown, Jim 118 2,359 20.0 12,312 104.3 5.22 106 0.045
//
// Dickerson, Eric 144 2,996 20.8 13,259 92.1 4.43 90 0.030
//
// Faulk, Marshall 176 2,836 16.1 12,279 69.8 4.33 100 0.035
// </Snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/TupleT1,T2,T3,T4,T5/Overview/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="example1.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module compareto1

// <Snippet1>
open System

// Organization of runningBacks 5-tuple:
// Component 1: Player name
// Component 2: Number of games played
// Component 3: Number of attempts (carries)
// Component 4: Number of yards gained
// Component 5: Number of touchdowns
let runningBacks =
[| Tuple.Create("Payton, Walter", 190, 3838, 16726, 110)
Tuple.Create("Sanders, Barry", 153, 3062, 15269, 99)
Tuple.Create("Brown, Jim", 118, 2359, 12312, 106)
Tuple.Create("Dickerson, Eric", 144, 2996, 13259, 90)
Tuple.Create("Faulk, Marshall", 176, 2836, 12279, 100) |]

// Display the array in unsorted order.
printfn "The values in unsorted order:"
for runningBack in runningBacks do
printfn $"{runningBack}"
printfn ""

// Sort the array
Array.Sort runningBacks

// Display the array in sorted order.
printfn "The values in sorted order:"
for runningBack in runningBacks do
printfn $"{runningBack}"
// The example displays the following output:
// The values in unsorted order:
// (Payton, Walter, 190, 3838, 16726, 110)
// (Sanders, Barry, 153, 3062, 15269, 99)
// (Brown, Jim, 118, 2359, 12312, 106)
// (Dickerson, Eric, 144, 2996, 13259, 90)
// (Faulk, Marshall, 176, 2836, 12279, 100)
//
// The values in sorted order:
// (Brown, Jim, 118, 2359, 12312, 106)
// (Dickerson, Eric, 144, 2996, 13259, 90)
// (Faulk, Marshall, 176, 2836, 12279, 100)
// (Payton, Walter, 190, 3838, 16726, 110)
// (Sanders, Barry, 153, 3062, 15269, 99)
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module compareto2

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

type YardsGained<'T1, 'T2, 'T3, 'T4, 'T5>() =
interface IComparer with
member _.Compare(x, y) =
match x with
| :? Tuple<'T1, 'T2, 'T3, 'T4, 'T5> as tX ->
let tY = y :?> Tuple<'T1, 'T2, 'T3, 'T4, 'T5>
-1 * Comparer<'T4>.Default.Compare(tX.Item4, tY.Item4)
| _ -> 0

// Organization of runningBacks 5-tuple:
// Component 1: Player name
// Component 2: Number of games played
// Component 3: Number of attempts (carries)
// Component 4: Number of yards gained
// Component 5: Number of touchdowns
let runningBacks =
[| Tuple.Create("Payton, Walter", 190, 3838, 16726, 110)
Tuple.Create("Sanders, Barry", 153, 3062, 15269, 99)
Tuple.Create("Brown, Jim", 118, 2359, 12312, 106)
Tuple.Create("Dickerson, Eric", 144, 2996, 13259, 90)
Tuple.Create("Faulk, Marshall", 176, 2836, 12279, 100) |]

// Display the array in unsorted order.
printfn "The values in unsorted order:"
for runningBack in runningBacks do
printfn $"{runningBack}"
printfn ""

// Sort the array
Array.Sort(runningBacks, YardsGained<string, int, int, int, int>())

// Display the array in sorted order.
printfn "The values in sorted order:"
for runningBack in runningBacks do
printfn $"{runningBack}"
// The example displays the following output:
// The values in unsorted order:
// (Payton, Walter, 190, 3838, 16726, 110)
// (Sanders, Barry, 153, 3062, 15269, 99)
// (Brown, Jim, 118, 2359, 12312, 106)
// (Dickerson, Eric, 144, 2996, 13259, 90)
// (Faulk, Marshall, 176, 2836, 12279, 100)
//
// The values in sorted order:
// (Brown, Jim, 118, 2359, 12312, 106)
// (Dickerson, Eric, 144, 2996, 13259, 90)
// (Faulk, Marshall, 176, 2836, 12279, 100)
// (Payton, Walter, 190, 3838, 16726, 110)
// (Sanders, Barry, 153, 3062, 15269, 99)
// </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>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/TupleT1,T2,T3,T4,T5/ToString/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="tostring1.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// <Snippet1>
open System

// Define array of tuples reflecting population change by state, 1990-2000.
let populationChanges =
[| Tuple.Create("California", 29760021, 33871648, 4111627, 13.8)
Tuple.Create("Illinois", 11430602, 12419293, 988691, 8.6)
Tuple.Create("Washington", 4866692, 5894121, 1027429, 21.1) |]
// Display each tuple.
for item in populationChanges do
printfn $"{item.ToString()}"
// The example displays the following output:
// (California, 29760021, 33871648, 4111627, 13.8)
// (Illinois, 11430602, 12419293, 988691, 8.6)
// (Washington, 4866692, 5894121, 1027429, 21.1)
// </Snippet1>
Loading