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

Skip to content

System.Tuple F# snippets #7918

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

open System

let create1Tuple () =
// <Snippet1>
let tuple1 = Tuple.Create 12
printfn $"{tuple1.Item1}" // Displays 12
// </Snippet1>

let new1Tuple () =
// <Snippet2>
let tuple1 = Tuple<int> 12
printfn $"{tuple1.Item1}" // Displays 12
// </Snippet2>

let create2Tuple () =
// <Snippet3>
let tuple2 = Tuple.Create("New York", 32.68)
printfn $"{tuple2.Item1}: {tuple2.Item2}"
// Displays New York: 32.68
// </Snippet3>

let new2Tuple () =
// <Snippet4>
let tuple2 = Tuple<string, double>("New York", 32.68)
printfn $"{tuple2.Item1}: {tuple2.Item2}"
// Displays New York: 32.68
// </Snippet4>

let create3Tuple () =
// <Snippet5>
let tuple3 = Tuple.Create("New York", 32.68, 51.87)
printfn $"{tuple3.Item1}: lo {tuple3.Item2}, hi {tuple3.Item3}"
// Displays New York: lo 32.68, hi 51.87
// </Snippet5>

let new3Tuple () =
// <Snippet6>
let tuple3 =
Tuple<string, double, double>("New York", 32.68, 51.87)

printfn $"{tuple3.Item1}: lo {tuple3.Item2}, hi {tuple3.Item3}"
// Displays New York: lo 32.68, hi 51.87
// </Snippet6>

let create4Tuple () =
// <Snippet7>
let tuple4 =
Tuple.Create("New York", 32.68, 51.87, 76.3)

printfn $"{tuple4.Item1}: Hi {tuple4.Item4}, Lo {tuple4.Item2}, Ave {tuple4.Item3}"
// Displays New York: Hi 76.3, Lo 32.68, Ave 51.87
// </Snippet7>

let new4Tuple () =
// <Snippet8>
let tuple4 =
Tuple<string, double, double, double>("New York", 32.68, 51.87, 76.3)

printfn $"{tuple4.Item1}: Hi {tuple4.Item4}, Lo {tuple4.Item2}, Ave {tuple4.Item3}"
// Displays New York: Hi 76.3, Lo 32.68, Ave 51.87
// </Snippet8>

let create5Tuple () =
// <Snippet9>
let tuple5 =
Tuple.Create("New York", 1990, 7322564, 2000, 8008278)

printfn $"{tuple5.Item1}: {tuple5.Item3:N0} in {tuple5.Item2}, {tuple5.Item5:N0} in {tuple5.Item4}"
// Displays New York: 7,322,564 in 1990, 8,008,278 in 2000
// </Snippet9>

let new5Tuple () =
// <Snippet10>
let tuple5 =
Tuple<string, int, int, int, int>("New York", 1990, 7322564, 2000, 8008278)

printfn $"{tuple5.Item1}: {tuple5.Item3:N0} in {tuple5.Item2}, {tuple5.Item5:N0} in {tuple5.Item4}"
// Displays New York: 7,322,564 in 1990, 8,008,278 in 2000
// </Snippet10>

let create6Tuple () =
// <Snippet11>
let tuple6 =
Tuple.Create("Jane", 90, 87, 93, 67, 100)

printfn
$"Test scores for {tuple6.Item1}: {tuple6.Item2}, {tuple6.Item3}, {tuple6.Item4}, {tuple6.Item5}, {tuple6.Item6}"
// Displays Test scores for Jane: 90, 87, 93, 67, 100
// </Snippet11>

let new6Tuple () =
// <Snippet12>
let tuple6 =
Tuple<string, int, int, int, int, int>("Jane", 90, 87, 93, 67, 100)

printfn
$"Test scores for {tuple6.Item1}: {tuple6.Item2}, {tuple6.Item3}, {tuple6.Item4}, {tuple6.Item5}, {tuple6.Item6}"
// Displays Test scores for Jane: 90, 87, 93, 67, 100
// </Snippet12>

let create7Tuple () =
// <Snippet13>
let tuple7 =
Tuple.Create("Jane", 90, 87, 93, 67, 100, 92)

printfn
$"Test scores for {tuple7.Item1}: {tuple7.Item2}, {tuple7.Item3}, {tuple7.Item4}, {tuple7.Item5}, {tuple7.Item6}, {tuple7.Item7}"
// Displays Test scores for Jane: 90, 87, 93, 67, 100, 92
// </Snippet13>

let new7Tuple () =
// <Snippet14>
let tuple7 =
Tuple<string, int, int, int, int, int, int>("Jane", 90, 87, 93, 67, 100, 92)

printfn
$"Test scores for {tuple7.Item1}: {tuple7.Item2}, {tuple7.Item3}, {tuple7.Item4}, {tuple7.Item5}, {tuple7.Item6}, {tuple7.Item7}"
// Displays Test scores for Jane: 90, 87, 93, 67, 100, 92
// </Snippet14>

create1Tuple ()
new1Tuple ()
create2Tuple ()
new2Tuple ()
create3Tuple ()
new3Tuple ()
create4Tuple ()
new4Tuple ()
create5Tuple ()
new5Tuple ()
create6Tuple ()
new6Tuple ()
create7Tuple ()
new7Tuple ()
9 changes: 9 additions & 0 deletions snippets/fsharp/System/Tuple/Overview/createntuple.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module createntuple

// <Snippet17>
open System

let primes = Tuple.Create(2, 3, 5, 7, 11, 13, 17, 19)
printfn $"Prime numbers less than 20: {primes.Item1}, {primes.Item2}, {primes.Item3}, {primes.Item4}, {primes.Item5}, {primes.Item6}, {primes.Item7}, and {primes.Rest.Item1}"
// Prime numbers less than 20: 2, 3, 5, 7, 11, 13, 17, and 19
// </Snippet17>
7 changes: 7 additions & 0 deletions snippets/fsharp/System/Tuple/Overview/ctor8.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module ctor8

// <Snippet20>
open System

let primes = new Tuple<int, int, int, int, int, int, int, Tuple<int>>(2, 3, 5, 7, 11, 13, 16, Tuple<int> 19)
// </Snippet20>
28 changes: 28 additions & 0 deletions snippets/fsharp/System/Tuple/Overview/example1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module example1

open System

let ctor1 () =
// <Snippet1>
// Create a 7-tuple.
let population = Tuple<string, int, int, int, int, int, int>(
"New York", 7891957, 7781984,
7894862, 7071639, 7322564, 8008278)
// Display the first and last elements.
printfn $"Population of {population.Item1} in 2000: {population.Item7:N0}"
// The example displays the following output:
// Population of New York in 2000: 8,008,278
// </Snippet1>

let factory () =
// <Snippet2>
// Create a 7-tuple.
let population = Tuple.Create("New York", 7891957, 7781984, 7894862, 7071639, 7322564, 8008278)
// Display the first and last elements.
printfn $"Population of {population.Item1} in 2000: {population.Item7:N0}"
// The example displays the following output:
// Population of New York in 2000: 8,008,278
// </Snippet2>

ctor1 ()
factory ()
13 changes: 13 additions & 0 deletions snippets/fsharp/System/Tuple/Overview/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="example1.fs" />
<Compile Include="createntuple.fs" />
<Compile Include="create1.fs" />
<Compile Include="ctor8.fs" />
</ItemGroup>
</Project>
19 changes: 19 additions & 0 deletions xml/System/Tuple.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@
Although you can create an instance of a tuple class by calling its class constructor, the code to do so can be cumbersome. The following example uses a class constructor to create a 7-tuple or septuple that contains population data for New York City for each census from 1950 through 2000.

:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/example1.cs" interactive="try-dotnet-method" id="Snippet1":::
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/example1.fs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.class/vb/example1.vb" id="Snippet1":::

Creating the same tuple object by using a helper method is more straightforward, as the following example shows.

:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/example1.cs" interactive="try-dotnet-method" id="Snippet2":::
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/example1.fs" id="Snippet2":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.class/vb/example1.vb" id="Snippet2":::

The <xref:System.Tuple.Create%2A> helper methods directly support the creation of tuple objects that have from one to eight components (that is, singletons through octuples). Although there is no practical limit to the number of components a tuple may have, helper methods are not available to create a tuple with nine or more components. To create such a tuple, you must call the <xref:System.Tuple%608.%23ctor%2A?displayProperty=nameWithType> constructor.
Expand All @@ -79,6 +81,7 @@
The following example creates an 8-tuple (octuple) that contains prime numbers that are less than 20.

:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/createntuple.cs" interactive="try-dotnet-method" id="Snippet17":::
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/createntuple.fs" id="Snippet17":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/createntuple.vb" id="Snippet17":::

]]></format>
Expand Down Expand Up @@ -158,11 +161,13 @@
<xref:System.Tuple.Create%2A> is a helper method that you can call to instantiate a 1-tuple object without having to explicitly specify the type of its component. The following example uses the <xref:System.Tuple.Create%2A> method to instantiate a 1-tuple whose component is of type <xref:System.Int32>.

:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/create1.cs" interactive="try-dotnet-method" id="Snippet1":::
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/create1.fs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/create1.vb" id="Snippet1":::

This code is equivalent to the following call to the <xref:System.Tuple%601.%23ctor%2A> class constructor.

:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/create1.cs" interactive="try-dotnet-method" id="Snippet2":::
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/create1.fs" id="Snippet2":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/create1.vb" id="Snippet2":::

]]></format>
Expand Down Expand Up @@ -226,11 +231,13 @@
<xref:System.Tuple.Create%2A> is a helper method that you can call to instantiate a 2-tuple object without having to explicitly specify the types of its components. The following example uses the <xref:System.Tuple.Create%2A> method to instantiate a 2-tuple.

:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/create1.cs" interactive="try-dotnet-method" id="Snippet3":::
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/create1.fs" id="Snippet3":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/create1.vb" id="Snippet3":::

This code is equivalent to the following call to the <xref:System.Tuple%602.%23ctor%2A> class constructor.

:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/create1.cs" interactive="try-dotnet-method" id="Snippet4":::
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/create1.fs" id="Snippet4":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/create1.vb" id="Snippet4":::

]]></format>
Expand Down Expand Up @@ -298,11 +305,13 @@
<xref:System.Tuple.Create%2A> is a helper method that you can call to instantiate a 3-tuple object without having to explicitly specify the types of its components. The following example uses the <xref:System.Tuple.Create%2A> method to instantiate a 3-tuple.

:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/create1.cs" interactive="try-dotnet-method" id="Snippet5":::
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/create1.fs" id="Snippet5":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/create1.vb" id="Snippet5":::

This code is equivalent to the following call to the <xref:System.Tuple%603.%23ctor%2A?displayProperty=nameWithType> class constructor.

:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/create1.cs" interactive="try-dotnet-method" id="Snippet6":::
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/create1.fs" id="Snippet6":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/create1.vb" id="Snippet6":::

]]></format>
Expand Down Expand Up @@ -374,11 +383,13 @@
<xref:System.Tuple.Create%2A> is a helper method that you can call to instantiate a 4-tuple object without having to explicitly specify the types of its components. The following example uses the <xref:System.Tuple.Create%2A> method to instantiate a 4-tuple.

:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/create1.cs" interactive="try-dotnet-method" id="Snippet7":::
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/create1.fs" id="Snippet7":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/create1.vb" id="Snippet7":::

This code is equivalent to the following call to the <xref:System.Tuple%604.%23ctor%2A?displayProperty=nameWithType> class constructor.

:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/create1.cs" interactive="try-dotnet-method" id="Snippet8":::
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/create1.fs" id="Snippet8":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/create1.vb" id="Snippet8":::

]]></format>
Expand Down Expand Up @@ -454,11 +465,13 @@
<xref:System.Tuple.Create%2A> is a helper method that you can call to instantiate a 5-tuple object without having to explicitly specify the types of its components. The following example uses the <xref:System.Tuple.Create%2A> method to instantiate a 5-tuple.

:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/create1.cs" interactive="try-dotnet-method" id="Snippet9":::
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/create1.fs" id="Snippet9":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/create1.vb" id="Snippet9":::

This code is equivalent to the following call to the <xref:System.Tuple%605.%23ctor%2A> class constructor.

:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/create1.cs" interactive="try-dotnet-method" id="Snippet10":::
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/create1.fs" id="Snippet10":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/create1.vb" id="Snippet10":::

]]></format>
Expand Down Expand Up @@ -538,11 +551,13 @@
<xref:System.Tuple.Create%2A> is a helper method that you can call to instantiate a 6-tuple object without having to explicitly specify the types of its components. The following example uses the <xref:System.Tuple.Create%2A> method to instantiate a 6-tuple.

:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/create1.cs" interactive="try-dotnet-method" id="Snippet11":::
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/create1.fs" id="Snippet11":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/create1.vb" id="Snippet11":::

This code is equivalent to the following call to the <xref:System.Tuple%606.%23ctor%2A> class constructor.

:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/create1.cs" interactive="try-dotnet-method" id="Snippet12":::
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/create1.fs" id="Snippet12":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/create1.vb" id="Snippet12":::

]]></format>
Expand Down Expand Up @@ -626,11 +641,13 @@
<xref:System.Tuple.Create%2A> is a helper method that you can call to instantiate a 7-tuple object without having to explicitly specify the types of its components. The following example uses the <xref:System.Tuple.Create%2A> method to instantiate a 7-tuple.

:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/create1.cs" interactive="try-dotnet-method" id="Snippet13":::
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/create1.fs" id="Snippet13":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/create1.vb" id="Snippet13":::

This code is equivalent to the following call to the <xref:System.Tuple%607.%23ctor%2A> class constructor.

:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/create1.cs" interactive="try-dotnet-method" id="Snippet14":::
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/create1.fs" id="Snippet14":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/create1.vb" id="Snippet14":::

]]></format>
Expand Down Expand Up @@ -726,11 +743,13 @@
The following example creates an 8-tuple whose components are prime numbers that are less than 20.

:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/createntuple.cs" interactive="try-dotnet-method" id="Snippet17":::
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/createntuple.fs" id="Snippet17":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/createntuple.vb" id="Snippet17":::

This is equivalent to the following example, which uses the <xref:System.Tuple%608> class constructor instead of the <xref:System.Tuple.Create%2A> factory creation method. Note that instantiating a <xref:System.Tuple%608> object in this way involves considerably more code, because you must declare a nested <xref:System.Tuple%601> object as the <xref:System.Tuple%608> object's eighth component to produce an octuple.

:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/ctor8.cs" id="Snippet20":::
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/ctor8.fs" id="Snippet20":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/ctor8.vb" id="Snippet20":::

]]></format>
Expand Down