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

Skip to content

System.Tuple<T1, T2> F# snippets #7943

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

// <Snippet1>
open System

let scores =
[| Tuple<string, Nullable<int>>("Dan", 90)
Tuple<string, Nullable<int>>("Ernie", Nullable())
Tuple<string, Nullable<int>>("Jill", 88)
Tuple<string, Nullable<int>>("Ernie", Nullable())
Tuple<string, Nullable<int>>("Nancy", 88)
Tuple<string, Nullable<int>>("Dan", 90) |]

// Compare the Tuple objects
for ctr = 0 to scores.Length - 1 do
for innerCtr = ctr + 1 to scores.Length - 1 do
printfn $"{scores[ctr]} = {scores[innerCtr]}: {scores[ctr].Equals scores[innerCtr]}"
printfn ""
// The example displays the following output:
// (Dan, 90) = (Ernie, ): False
// (Dan, 90) = (Jill, 88): False
// (Dan, 90) = (Ernie, ): False
// (Dan, 90) = (Nancy, 88): False
// (Dan, 90) = (Dan, 90): True
//
// (Ernie, ) = (Jill, 88): False
// (Ernie, ) = (Ernie, ): True
// (Ernie, ) = (Nancy, 88): False
// (Ernie, ) = (Dan, 90): False
//
// (Jill, 88) = (Ernie, ): False
// (Jill, 88) = (Nancy, 88): False
// (Jill, 88) = (Dan, 90): False
//
// (Ernie, ) = (Nancy, 88): False
// (Ernie, ) = (Dan, 90): False
//
// (Nancy, 88) = (Dan, 90): False
// </Snippet1>
50 changes: 50 additions & 0 deletions snippets/fsharp/System/TupleT1,T2/Equals/equals2.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module equals2

// <Snippet2>
open System
open System.Collections

type Item2Comparer<'T1, 'T2 when 'T1: equality and 'T2: equality>() =
interface IEqualityComparer with
member _.GetHashCode(obj) =
match obj with
| :? 'T1 as obj->
obj.GetHashCode()
| _ ->
(obj :?> 'T2).GetHashCode()

member _.Equals(x, y) =
// Return true for all values of Item1.
match x with
| :? 'T1 ->
true
| _ ->
x.Equals y

let distancesWalked =
[| Tuple.Create("Jan", Double.NaN)
Tuple.Create("Joe", Double.NaN)
Tuple.Create("Adam", 1.36)
Tuple.Create("Selena", 2.01)
Tuple.Create("Jake", 1.36) |]

for ctr = 0 to distancesWalked.Length - 1 do
let distanceWalked = distancesWalked[ctr]
for ctr2 = ctr + 1 to distancesWalked.Length - 1 do
printfn $"{distanceWalked} = {distancesWalked[ctr2]}: {(distanceWalked :> IStructuralEquatable).Equals(distancesWalked[ctr2], Item2Comparer<string, double>())}"
printfn ""
// The example displays the following output:
// (Jan, NaN) = (Joe, NaN): True
// (Jan, NaN) = (Adam, 1.36): False
// (Jan, NaN) = (Selena, 2.01): False
// (Jan, NaN) = (Jake, 1.36): False
//
// (Joe, NaN) = (Adam, 1.36): False
// (Joe, NaN) = (Selena, 2.01): False
// (Joe, NaN) = (Jake, 1.36): False
//
// (Adam, 1.36) = (Selena, 2.01): False
// (Adam, 1.36) = (Jake, 1.36): True
//
// (Selena, 2.01) = (Jake, 1.36): False
// </Snippet2>
11 changes: 11 additions & 0 deletions snippets/fsharp/System/TupleT1,T2/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>
33 changes: 33 additions & 0 deletions snippets/fsharp/System/TupleT1,T2/Overview/example1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module example1

// <Snippet1>
open System

let scores =
[| Tuple<string, Nullable<int>>("Jack", 78)
Tuple<string, Nullable<int>>("Abbey", 92)
Tuple<string, Nullable<int>>("Dave", 88)
Tuple<string, Nullable<int>>("Sam", 91)
Tuple<string, Nullable<int>>("Ed", Nullable())
Tuple<string, Nullable<int>>("Penelope", 82)
Tuple<string, Nullable<int>>("Linda", 99)
Tuple<string, Nullable<int>>("Judith", 84) |]

let computeMean (scores: Tuple<string, Nullable<int>>[]) (n: int outref) =
n <- 0
let mutable sum = 0
for _, score in scores do
if score.HasValue then
n <- n + 1
sum <- sum + score.Value
if n > 0 then
double sum / double n
else
0

let mutable number = 0
let mean = computeMean scores &number
printfn $"Average test score: {mean:N2} (n={number})"
// The example displays the following output:
// Average test score: 87.71 (n=7)
// </Snippet1>
11 changes: 11 additions & 0 deletions snippets/fsharp/System/TupleT1,T2/Overview/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="example1.fs" />
<Compile Include="item1.fs" />
</ItemGroup>
</Project>
34 changes: 34 additions & 0 deletions snippets/fsharp/System/TupleT1,T2/Overview/item1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module item1

// <Snippet1>
open System

let integerDivide (dividend: int) divisor =
try
let quotient, remainder = Math.DivRem(dividend, divisor)
Tuple<int, int>(quotient, remainder)
with :? DivideByZeroException ->
Unchecked.defaultof<Tuple<int, int>>

[<EntryPoint>]
let main _ =
let dividend = 136945
let divisor = 178
let result = integerDivide dividend divisor
if box result <> null then
printfn $@"{dividend} \ {divisor} = {result.Item1}, remainder {result.Item2}"
else
printfn $@"{dividend} \ {divisor} = <Error>"

let dividend = Int32.MaxValue
let divisor = -2073
let result = integerDivide dividend divisor
if box result <> null then
printfn $@"{dividend} \ {divisor} = {result.Item1}, remainder {result.Item2}"
else
printfn $@"{dividend} \ {divisor} = <Error>"
0
// The example displays the following output:
// 136945 \ 178 = 769, remainder 63
// 2147483647 \ -2073 = -1035930, remainder 757
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module compareto1

// <Snippet1>
open System

let scores =
[| Tuple<string, Nullable<int>>("Jack", 78)
Tuple<string, Nullable<int>>("Abbey", 92)
Tuple<string, Nullable<int>>("Dave", 88)
Tuple<string, Nullable<int>>("Sam", 91)
Tuple<string, Nullable<int>>("Ed", Nullable())
Tuple<string, Nullable<int>>("Penelope", 82)
Tuple<string, Nullable<int>>("Linda", 99)
Tuple<string, Nullable<int>>("Judith", 84) |]

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)
// (Abbey, 92)
// (Dave, 88)
// (Sam, 91)
// (Ed, )
// (Penelope, 82)
// (Linda, 99)
// (Judith, 84)
//
// The values in sorted order:
// (Abbey, 92)
// (Dave, 88)
// (Ed, )
// (Jack, 78)
// (Judith, 84)
// (Linda, 99)
// (Penelope, 82)
// (Sam, 91)
// </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>() =
interface IComparer with
member _.Compare(x: obj, y: obj) =
match x with
| :? Tuple<'T1, 'T2> as tX ->
let tY = y :?> Tuple<'T1, 'T2>
Comparer<'T2>.Default.Compare(tX.Item2, tY.Item2)
| _ -> 0

let scores =
[| Tuple<string, Nullable<int>>("Jack", 78)
Tuple<string, Nullable<int>>("Abbey", 92)
Tuple<string, Nullable<int>>("Dave", 88)
Tuple<string, Nullable<int>>("Sam", 91)
Tuple<string, Nullable<int>>("Ed", Nullable())
Tuple<string, Nullable<int>>("Penelope", 82)
Tuple<string, Nullable<int>>("Linda", 99)
Tuple<string, Nullable<int>>("Judith", 84) |]

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

printfn ""

Array.Sort(scores, ScoreComparer<string, Nullable<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)
// (Abbey, 92)
// (Dave, 88)
// (Sam, 91)
// (Ed, )
// (Penelope, 82)
// (Linda, 99)
// (Judith, 84)
//
// The values in sorted order:
// (Ed, )
// (Jack, 78)
// (Penelope, 82)
// (Judith, 84)
// (Dave, 88)
// (Sam, 91)
// (Abbey, 92)
// (Linda, 99)
// </Snippet2>
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="compareto1.fs" />
<Compile Include="compareto2.fs" />
</ItemGroup>
</Project>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/TupleT1,T2/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>
24 changes: 24 additions & 0 deletions snippets/fsharp/System/TupleT1,T2/ToString/tostring1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//<Snippet1>
open System

let scores =
[| Tuple<string, Nullable<int>>("Abbey", 92)
Tuple<string, Nullable<int>>("Dave", 88)
Tuple<string, Nullable<int>>("Ed", Nullable())
Tuple<string, Nullable<int>>("Jack", 78)
Tuple<string, Nullable<int>>("Linda", 99)
Tuple<string, Nullable<int>>("Judith", 84)
Tuple<string, Nullable<int>>("Penelope", 82)
Tuple<string, Nullable<int>>("Sam", 91) |]
for score in scores do
printfn $"{score.ToString()}"
// The example displays the following output:
// (Abbey, 92)
// (Dave, 88)
// (Ed, )
// (Jack, 78)
// (Linda, 99)
// (Judith, 84)
// (Penelope, 82)
// (Sam, 91)
// </Snippet1>
Loading