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

Skip to content

System.Tuple<T1,T2,T3> F# snippets #7944

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

// <Snippet1>
open System

let scores =
[| Tuple.Create("Ed", 78.8, 8)
Tuple.Create("Abbey", 92.1, 9)
Tuple.Create("Ed", 71.2, 9)
Tuple.Create("Sam", 91.7, 8)
Tuple.Create("Ed", 71.2, 5)
Tuple.Create("Penelope", 82.9, 8)
Tuple.Create("Ed", 71.2, 9)
Tuple.Create("Judith", 84.3, 9) |]

// Test each tuple object for equality with every other tuple.
for ctr = 0 to scores.Length - 1 do
let currentTuple = scores[ctr]
for ctr2 = ctr + 1 to scores.Length - 1 do
printfn $"{currentTuple} = {scores[ctr2]}: {currentTuple.Equals scores[ctr2]}"
printfn ""

// The example displays the following output
// (Ed, 78.8, 8) = (Abbey, 92.1, 9): False
// (Ed, 78.8, 8) = (Ed, 71.2, 9): False
// (Ed, 78.8, 8) = (Sam, 91.7, 8): False
// (Ed, 78.8, 8) = (Ed, 71.2, 5): False
// (Ed, 78.8, 8) = (Penelope, 82.9, 8): False
// (Ed, 78.8, 8) = (Ed, 71.2, 9): False
// (Ed, 78.8, 8) = (Judith, 84.3, 9): False
//
// (Abbey, 92.1, 9) = (Ed, 71.2, 9): False
// (Abbey, 92.1, 9) = (Sam, 91.7, 8): False
// (Abbey, 92.1, 9) = (Ed, 71.2, 5): False
// (Abbey, 92.1, 9) = (Penelope, 82.9, 8): False
// (Abbey, 92.1, 9) = (Ed, 71.2, 9): False
// (Abbey, 92.1, 9) = (Judith, 84.3, 9): False
//
// (Ed, 71.2, 9) = (Sam, 91.7, 8): False
// (Ed, 71.2, 9) = (Ed, 71.2, 5): False
// (Ed, 71.2, 9) = (Penelope, 82.9, 8): False
// (Ed, 71.2, 9) = (Ed, 71.2, 9): True
// (Ed, 71.2, 9) = (Judith, 84.3, 9): False
//
// (Sam, 91.7, 8) = (Ed, 71.2, 5): False
// (Sam, 91.7, 8) = (Penelope, 82.9, 8): False
// (Sam, 91.7, 8) = (Ed, 71.2, 9): False
// (Sam, 91.7, 8) = (Judith, 84.3, 9): False
//
// (Ed, 71.2, 5) = (Penelope, 82.9, 8): False
// (Ed, 71.2, 5) = (Ed, 71.2, 9): False
// (Ed, 71.2, 5) = (Judith, 84.3, 9): False
//
// (Penelope, 82.9, 8) = (Ed, 71.2, 9): False
// (Penelope, 82.9, 8) = (Judith, 84.3, 9): False
//
// (Ed, 71.2, 9) = (Judith, 84.3, 9): False
// </Snippet1>
77 changes: 77 additions & 0 deletions snippets/fsharp/System/TupleT1,T2,T3/Equals/equals2.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
module equals2

// <Snippet2>
open System
open System.Collections

type Item2Comparer<'T1, 'T2, 'T3 when 'T1: equality and 'T2: equality and 'T3: equality>() =
interface IEqualityComparer with
member _.Equals(x: obj, y: obj) =
// Return true for all values of Item1.
match x with
| :? 'T1 ->
true
| :? 'T2 ->
x.Equals y
| _ -> true

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

let scores =
[| Tuple.Create("Ed", 78.8, 8)
Tuple.Create("Abbey", 92.1, 9)
Tuple.Create("Jim", 71.2, 9)
Tuple.Create("Sam", 91.7, 8)
Tuple.Create("Sandy", 71.2, 5)
Tuple.Create("Penelope", 82.9, 8)
Tuple.Create("Serena", 71.2, 9)
Tuple.Create("Judith", 84.3, 9) |]

for ctr = 0 to scores.Length - 1 do
let score : IStructuralEquatable = scores[ctr]
for ctr2 = ctr + 1 to scores.Length - 1 do
printfn $"{score} = {scores[ctr2]}: {score.Equals(scores[ctr2], Item2Comparer<string, double, int>())}"
printfn ""
// The example displays the following output:
// (Ed, 78.8, 8) = (Abbey, 92.1, 9): False
// (Ed, 78.8, 8) = (Jim, 71.2, 9): False
// (Ed, 78.8, 8) = (Sam, 91.7, 8): False
// (Ed, 78.8, 8) = (Sandy, 71.2, 5): False
// (Ed, 78.8, 8) = (Penelope, 82.9, 8): False
// (Ed, 78.8, 8) = (Serena, 71.2, 9): False
// (Ed, 78.8, 8) = (Judith, 84.3, 9): False
//
// (Abbey, 92.1, 9) = (Jim, 71.2, 9): False
// (Abbey, 92.1, 9) = (Sam, 91.7, 8): False
// (Abbey, 92.1, 9) = (Sandy, 71.2, 5): False
// (Abbey, 92.1, 9) = (Penelope, 82.9, 8): False
// (Abbey, 92.1, 9) = (Serena, 71.2, 9): False
// (Abbey, 92.1, 9) = (Judith, 84.3, 9): False
//
// (Jim, 71.2, 9) = (Sam, 91.7, 8): False
// (Jim, 71.2, 9) = (Sandy, 71.2, 5): True
// (Jim, 71.2, 9) = (Penelope, 82.9, 8): False
// (Jim, 71.2, 9) = (Serena, 71.2, 9): True
// (Jim, 71.2, 9) = (Judith, 84.3, 9): False
//
// (Sam, 91.7, 8) = (Sandy, 71.2, 5): False
// (Sam, 91.7, 8) = (Penelope, 82.9, 8): False
// (Sam, 91.7, 8) = (Serena, 71.2, 9): False
// (Sam, 91.7, 8) = (Judith, 84.3, 9): False
//
// (Sandy, 71.2, 5) = (Penelope, 82.9, 8): False
// (Sandy, 71.2, 5) = (Serena, 71.2, 9): True
// (Sandy, 71.2, 5) = (Judith, 84.3, 9): False
//
// (Penelope, 82.9, 8) = (Serena, 71.2, 9): False
// (Penelope, 82.9, 8) = (Judith, 84.3, 9): False
//
// (Serena, 71.2, 9) = (Judith, 84.3, 9): False
// </Snippet2>
11 changes: 11 additions & 0 deletions snippets/fsharp/System/TupleT1,T2,T3/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>
34 changes: 34 additions & 0 deletions snippets/fsharp/System/TupleT1,T2,T3/Overview/example1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// <Snippet1>
open System

let computeStatistics (scores: Tuple<string, double, int>[]) =
let mutable n = 0
let mutable sum = 0.

// Compute the mean.
for score in scores do
n <- n + score.Item3
sum <- sum + score.Item2 * double score.Item3
let mean = sum / double n

// Compute the standard deviation.
let mutable ss = 0.
for score in scores do
ss <- (score.Item2 - mean) ** 2.
let sd = sqrt (ss / double scores.Length)
Tuple.Create(scores.Length, mean, sd)

let scores =
[| Tuple.Create("Jack", 78.8, 8)
Tuple.Create("Abbey", 92.1, 9)
Tuple.Create("Dave", 88.3, 9)
Tuple.Create("Sam", 91.7, 8)
Tuple.Create("Ed", 71.2, 5)
Tuple.Create("Penelope", 82.9, 8)
Tuple.Create("Linda", 99.0, 9)
Tuple.Create("Judith", 84.3, 9) |]
let result = computeStatistics scores
printfn $"Mean score: {result.Item2:N2} (SD={result.Item3:N2}) (n={result.Item1})"
// The example displays the following output:
// Mean score: 87.02 (SD=0.96) (n=8)
// </Snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/TupleT1,T2,T3/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,47 @@
module compareto1

// <Snippet1>
open System

let scores =
[| Tuple.Create("Jack", 78.8, 8)
Tuple.Create("Abbey", 92.1, 9)
Tuple.Create("Dave", 88.3, 9)
Tuple.Create("Sam", 91.7, 8)
Tuple.Create("Ed", 71.2, 5)
Tuple.Create("Penelope", 82.9, 8)
Tuple.Create("Linda", 99.0, 9)
Tuple.Create("Judith", 84.3, 9) |]

printfn "The values in unsorted order:"
for score in scores do
printfn $"{score}"

printfn ""

Array.Sort scores

printfn "The values in sorted order"
for score in scores do
printfn $"{score}"
// The example displays the following output
// The values in unsorted order:
// (Jack, 78.8, 8)
// (Abbey, 92.1, 9)
// (Dave, 88.3, 9)
// (Sam, 91.7, 8)
// (Ed, 71.2, 5)
// (Penelope, 82.9, 8)
// (Linda, 99, 9)
// (Judith, 84.3, 9)
//
// The values in sorted order:
// (Abbey, 92.1, 9)
// (Dave, 88.3, 9)
// (Ed, 71.2, 5)
// (Jack, 78.8, 8)
// (Judith, 84.3, 9)
// (Linda, 99, 9)
// (Penelope, 82.9, 8)
// (Sam, 91.7, 8)
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module compareto2

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

type ScoreComparer<'T1, 'T2, 'T3>() =
interface IComparer with
member _.Compare(x: obj, y: obj) =
match x with
| :? Tuple<'T1, 'T2, 'T3> as tX ->
let tY = y :?> Tuple<'T1, 'T2, 'T3>
Comparer<'T2>.Default.Compare(tX.Item2, tY.Item2)
| _ -> 0

let scores =
[| Tuple.Create("Jack", 78.8, 8)
Tuple.Create("Abbey", 92.1, 9)
Tuple.Create("Dave", 88.3, 9)
Tuple.Create("Sam", 91.7, 8)
Tuple.Create("Ed", 71.2, 5)
Tuple.Create("Penelope", 82.9, 8)
Tuple.Create("Linda", 99.0, 9)
Tuple.Create("Judith", 84.3, 9) |]

printfn "The values in unsorted order:"
for score in scores do
printfn $"{score}"

printfn ""

Array.Sort(scores, ScoreComparer<string, double, int>())

printfn "The values in sorted order:"
for score in scores do
printfn $"{score}"
// The example displays the following output
// The values in unsorted order:
// (Jack, 78.8, 8)
// (Abbey, 92.1, 9)
// (Dave, 88.3, 9)
// (Sam, 91.7, 8)
// (Ed, 71.2, 5)
// (Penelope, 82.9, 8)
// (Linda, 99, 9)
// (Judith, 84.3, 9)
//
// The values in sorted order:
// (Ed, 71.2, 5)
// (Jack, 78.8, 8)
// (Penelope, 82.9, 8)
// (Judith, 84.3, 9)
// (Dave, 88.3, 9)
// (Sam, 91.7, 8)
// (Abbey, 92.1, 9)
// (Linda, 99, 9)
// </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/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>
25 changes: 25 additions & 0 deletions snippets/fsharp/System/TupleT1,T2,T3/ToString/tostring1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// <Snippet1>
open System

let scores =
[| Tuple.Create("Jack", 78.8, 8)
Tuple.Create("Abbey", 92.1, 9)
Tuple.Create("Dave", 88.3, 9)
Tuple.Create("Sam", 91.7, 8)
Tuple.Create("Ed", 71.2, 5)
Tuple.Create("Penelope", 82.9, 8)
Tuple.Create("Linda", 99.0, 9)
Tuple.Create("Judith", 84.3, 9) |]
Array.Sort scores
for score in scores do
printfn $"{score}"
// The example displays the following output
// (Abbey, 92.1, 9)
// (Dave, 88.3, 9)
// (Ed, 71.2, 5)
// (Jack, 78.8, 8)
// (Judith, 84.3, 9)
// (Linda, 99, 9)
// (Penelope, 82.9, 8)
// (Sam, 91.7, 8)
// </Snippet1>
Loading