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

Skip to content

System.Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> F# snippets #8029

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
May 2, 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
69 changes: 69 additions & 0 deletions snippets/fsharp/System/Tuple/Overview/example.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
module example

open System

let showPopulation year newPopulation =
printfn $"""{year,5} {newPopulation,14:N0} {"n/a",10:P2}"""

let showPopulationChange year newPopulation oldPopulation =
printfn $"{year,5} {newPopulation,14:N0} {(double (newPopulation - oldPopulation) / oldPopulation) / 10.,10:P2}"

// <Snippet19>
let from1980 = Tuple.Create(1203339, 1027974, 951270)
let from1910 = new Tuple<int, int, int, int, int, int, int, Tuple<int, int, int>>(465766, 993078, 1568622, 1623452, 1849568, 1670144, 1511462, from1980)
let population = new Tuple<string, int, int, int, int, int, int, Tuple<int, int, int, int, int, int, int, Tuple<int, int, int>>>("Detroit", 1860, 45619, 79577, 116340, 205876, 285704, from1910)
// </Snippet19>
printfn $"Population of {population.Item1}\n"
printfn "%5s %14s %10s" "Year" "Population" "Change"

do
let year = population.Item2
showPopulation year population.Item3
let year = year + 10
showPopulationChange year population.Item4 population.Item3
let year = year + 10
showPopulationChange year population.Item5 population.Item4
let year = year + 10
showPopulationChange year population.Item6 population.Item5
let year = year + 10
showPopulationChange year population.Item7 population.Item6
let year = year + 10
showPopulationChange year population.Rest.Item1 population.Item7
let year = year + 10
showPopulationChange year population.Rest.Item2 population.Rest.Item1
let year = year + 10
showPopulationChange year population.Rest.Item3 population.Rest.Item2
let year = year + 10
showPopulationChange year population.Rest.Item4 population.Rest.Item3
let year = year + 10
showPopulationChange year population.Rest.Item5 population.Rest.Item4
let year = year + 10
showPopulationChange year population.Rest.Item6 population.Rest.Item5
let year = year + 10
showPopulationChange year population.Rest.Item7 population.Rest.Item6
let year = year + 10
showPopulationChange year population.Rest.Rest.Item1 population.Rest.Item7
let year = year + 10
showPopulationChange year population.Rest.Rest.Item2 population.Rest.Rest.Item1
let year = year + 10
showPopulationChange year population.Rest.Rest.Item3 population.Rest.Rest.Item2

// The example displays the following output:
//
// Population of Detroit
// Year Population Change
// 1860 45,619 n/a
// 1870 79,577 7.44 %
// 1880 116,340 4.62 %
// 1890 205,876 7.70 %
// 1900 285,704 3.88 %
// 1910 465,766 6.30 %
// 1920 993,078 11.32 %
// 1930 1,568,622 5.80 %
// 1940 1,623,452 0.35 %
// 1950 1,849,568 1.39 %
// 1960 1,670,144 -0.97 %
// 1970 1,511,462 -0.95 %
// 1980 1,203,339 -2.04 %
// 1990 1,027,974 -1.46 %
// 2000 951,270 -0.75 %
1 change: 1 addition & 0 deletions snippets/fsharp/System/Tuple/Overview/fs.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<ItemGroup>
<Compile Include="example1.fs" />
<Compile Include="example.fs" />
<Compile Include="createntuple.fs" />
<Compile Include="create1.fs" />
<Compile Include="ctor8.fs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// <Snippet1>
open System

// Create five 8-tuple objects containing prime numbers.
let prime1 =
new Tuple<Int32, Int32, Int32, Int32, Int32, Int32, Int32,
Tuple<Int32>> (2, 3, 5, 7, 11, 13, 17,
new Tuple<Int32>(19))
let prime2 =
new Tuple<Int32, Int32, Int32, Int32, Int32, Int32, Int32,
Tuple<Int32>> (23, 29, 31, 37, 41, 43, 47,
new Tuple<Int32>(55))
let prime3 =
new Tuple<Int32, Int32, Int32, Int32, Int32, Int32, Int32,
Tuple<Int32>> (3, 2, 5, 7, 11, 13, 17,
new Tuple<Int32>(19))
let prime4 =
new Tuple<Int32, Int32, Int32, Int32, Int32, Int32, Int32,
Tuple<Int32, Int32>> (2, 3, 5, 7, 11, 13, 17,
new Tuple<Int32, Int32>(19, 23))
let prime5 =
new Tuple<Int32, Int32, Int32, Int32, Int32, Int32, Int32,
Tuple<Int32>> (2, 3, 5, 7, 11, 13, 17,
new Tuple<Int32>(19))
printfn $"{prime1} = {prime2} : {prime1.Equals prime2}"
printfn $"{prime1} = {prime3} : {prime1.Equals prime3}"
printfn $"{prime1} = {prime4} : {prime1.Equals prime4}"
printfn $"{prime1} = {prime5} : {prime1.Equals prime5}"
// The example displays the following output:
// (2, 3, 5, 7, 11, 13, 17, 19) = (23, 29, 31, 37, 41, 43, 47, 55) : False
// (2, 3, 5, 7, 11, 13, 17, 19) = (3, 2, 5, 7, 11, 13, 17, 19) : False
// (2, 3, 5, 7, 11, 13, 17, 19) = (2, 3, 5, 7, 11, 13, 17, 19, 23) : False
// (2, 3, 5, 7, 11, 13, 17, 19) = (2, 3, 5, 7, 11, 13, 17, 19) : True
// </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="equals1.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="item1.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
open System
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you forgot the // <Snippet1> tags in the various Item snippets. That's what caused the build warnings. @albert-du @BillWagner

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can see that even if you select F#, the C# example is still shown because it can't find the F# snippet.

https://docs.microsoft.com/en-us/dotnet/api/system.tuple-8.item1?view=net-6.0#examples

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@albert-du Can you make an updated PR, or should I?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops looks like this one fell off my radar a bit, thank you for fixing it!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#8042 should fix these.


let showPopulation year newPopulation =
printfn $"""{year,5} {newPopulation,14:N0} {"n/a",10:P2}"""

let showPopulationChange year newPopulation oldPopulation =
printfn $"{year,5} {newPopulation,14:N0} {(double (newPopulation - oldPopulation) / oldPopulation) / 10.,10:P2}"

// <Snippet19>
let from1980 = Tuple.Create(1203339, 1027974, 951270)
let from1910 = new Tuple<int, int, int, int, int, int, int, Tuple<int, int, int>>(465766, 993078, 1568622, 1623452, 1849568, 1670144, 1511462, from1980)
let population = new Tuple<string, int, int, int, int, int, int, Tuple<int, int, int, int, int, int, int, Tuple<int, int, int>>>("Detroit", 1860, 45619, 79577, 116340, 205876, 285704, from1910)
// </Snippet19>
printfn $"Population of {population.Item1}\n"
printfn "%5s %14s %10s" "Year" "Population" "Change"

do
let year = population.Item2
showPopulation year population.Item3
let year = year + 10
showPopulationChange year population.Item4 population.Item3
let year = year + 10
showPopulationChange year population.Item5 population.Item4
let year = year + 10
showPopulationChange year population.Item6 population.Item5
let year = year + 10
showPopulationChange year population.Item7 population.Item6
let year = year + 10
showPopulationChange year population.Rest.Item1 population.Item7
let year = year + 10
showPopulationChange year population.Rest.Item2 population.Rest.Item1
let year = year + 10
showPopulationChange year population.Rest.Item3 population.Rest.Item2
let year = year + 10
showPopulationChange year population.Rest.Item4 population.Rest.Item3
let year = year + 10
showPopulationChange year population.Rest.Item5 population.Rest.Item4
let year = year + 10
showPopulationChange year population.Rest.Item6 population.Rest.Item5
let year = year + 10
showPopulationChange year population.Rest.Item7 population.Rest.Item6
let year = year + 10
showPopulationChange year population.Rest.Rest.Item1 population.Rest.Item7
let year = year + 10
showPopulationChange year population.Rest.Rest.Item2 population.Rest.Rest.Item1
let year = year + 10
showPopulationChange year population.Rest.Rest.Item3 population.Rest.Rest.Item2

// The example displays the following output:
//
// Population of Detroit
// Year Population Change
// 1860 45,619 n/a
// 1870 79,577 7.44 %
// 1880 116,340 4.62 %
// 1890 205,876 7.70 %
// 1900 285,704 3.88 %
// 1910 465,766 6.30 %
// 1920 993,078 11.32 %
// 1930 1,568,622 5.80 %
// 1940 1,623,452 0.35 %
// 1950 1,849,568 1.39 %
// 1960 1,670,144 -0.97 %
// 1970 1,511,462 -0.95 %
// 1980 1,203,339 -2.04 %
// 1990 1,027,974 -1.46 %
// 2000 951,270 -0.75 %
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="octuple1.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
open System

// <Snippet1>
let primes = new Tuple<Int32, Int32, Int32, Int32, Int32, Int32, Int32,
Tuple<Int32>> (2, 3, 5, 7, 11, 13, 17, new Tuple<Int32>(19))
// </Snippet1>
printfn $"{primes}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module compareto1

// <Snippet1>
open System

// Create array of 8-tuple objects containing prime numbers.
let primes =
[| new Tuple<Int32, Int32, Int32, Int32, Int32, Int32, Int32,
Tuple<Int32>>(2, 3, 5, 7, 11, 13, 17, new Tuple<Int32>(19))
new Tuple<Int32, Int32, Int32, Int32, Int32, Int32, Int32,
Tuple<Int32>>(23, 29, 31, 37, 41, 43, 47, new Tuple<Int32>(55))
new Tuple<Int32, Int32, Int32, Int32, Int32, Int32, Int32,
Tuple<Int32>>(3, 2, 5, 7, 11, 13, 17, new Tuple<Int32>(19)) |]
// Display 8-tuples in unsorted order.
for prime in primes do
printfn $"{prime}"
printfn ""

// Sort the array and display its 8-tuples.
Array.Sort primes
for prime in primes do
printfn $"{prime}"
// The example displays the following output:
// (2, 3, 5, 7, 11, 13, 17, 19)
// (23, 29, 31, 37, 41, 43, 47, 55)
// (3, 2, 5, 7, 11, 13, 17, 19)
//
// (2, 3, 5, 7, 11, 13, 17, 19)
// (3, 2, 5, 7, 11, 13, 17, 19)
// (23, 29, 31, 37, 41, 43, 47, 55)
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
module compareto2

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

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

do
if itemPosition <= 0 || itemPosition > 8 then
invalidArg "itemPosition" "The component argument is out of range."
new(itemPosition) = PopulationComparer (itemPosition, true)

interface IComparer with
member _.Compare(x, y) =
match x with
| :? Tuple<'T1, 'T2, 'T3, 'T4, 'T5, 'T6, 'T7, Tuple<'T8>> as tX ->
let tY = y :?> Tuple<'T1, 'T2, 'T3, 'T4, 'T5, 'T6, 'T7, Tuple<'T8>>
match itemPosition 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
| 7 ->
Comparer<'T7>.Default.Compare(tX.Item7, tY.Item7) * multiplier
| 8 ->
Comparer<'T8>.Default.Compare(tX.Rest.Item1, tY.Rest.Item1) * multiplier
| _ ->
Comparer<'T1>.Default.Compare(tX.Item1, tY.Item1) * multiplier
| _ -> 0

// Create array of octuples with population data for three U.S.
// cities, 1940-2000.
let cities =
[| Tuple.Create("Los Angeles", 1504277, 1970358, 2479015, 2816061, 2966850, 3485398, 3694820)
Tuple.Create("Chicago", 3396808, 3620962, 3550904, 3366957, 3005072, 2783726, 2896016)
Tuple.Create("New York", 7454995, 7891957, 7781984, 7894862, 7071639, 7322564, 8008278)
Tuple.Create("Detroit", 1623452, 1849568, 1670144, 1511462, 1203339, 1027974, 951270) |]
// 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, int, int> 2)

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

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

// 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, 1504277, 1970358, 2479015, 2816061, 2966850, 3485398, 3694820)
// (New York, 7454995, 7891957, 7781984, 7894862, 7071639, 7322564, 8008278)
// (Chicago, 3396808, 3620962, 3550904, 3366957, 3005072, 2783726, 2896016)
// (Detroit, 1623452, 1849568, 1670144, 1511462, 1203339, 1027974, 951270)
//
// Sorted by population in 1950:
// (New York, 7454995, 7891957, 7781984, 7894862, 7071639, 7322564, 8008278)
// (Chicago, 3396808, 3620962, 3550904, 3366957, 3005072, 2783726, 2896016)
// (Detroit, 1623452, 1849568, 1670144, 1511462, 1203339, 1027974, 951270)
// (Los Angeles, 1504277, 1970358, 2479015, 2816061, 2966850, 3485398, 3694820)
//
// Sorted by population in 2000:
// (New York, 7454995, 7891957, 7781984, 7894862, 7071639, 7322564, 8008278)
// (Los Angeles, 1504277, 1970358, 2479015, 2816061, 2966850, 3485398, 3694820)
// (Chicago, 3396808, 3620962, 3550904, 3366957, 3005072, 2783726, 2896016)
// (Detroit, 1623452, 1849568, 1670144, 1511462, 1203339, 1027974, 951270)
// </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,21 @@
// <Snippet1>
open System

let showPopulation year newPopulation =
printfn $"""{year,5} {newPopulation,14:N0} {"n/a",10:P2}"""

let showPopulationChange year newPopulation oldPopulation =
printfn $"{year,5} {newPopulation,14:N0} {(double (newPopulation - oldPopulation) / oldPopulation) / 10.,10:P2}"

let from1980 = Tuple.Create(1203339, 1027974, 951270)
let from1910 =
new Tuple<int, int, int, int, int, int, int, Tuple<int, int, int>>(465766, 993078, 1568622, 1623452, 1849568, 1670144, 1511462, from1980)
let population =
new Tuple<string, int, int, int, int, int, int, Tuple<int, int, int, int, int, int, int, Tuple<int, int, int>>>("Detroit", 1860, 45619, 79577, 116340, 205876, 285704, from1910)

printfn $"{population}"

// The example displays the following output:
// (Detroit, 1860, 45619, 79577, 116340, 205876, 285704, 465766, 993078,
// 1568622, 1623452, 1849568, 1670144, 1511462, 1203339, 1027974, 951270)
// </Snippet1>
Loading