Skip to content

Commit 7c0dd73

Browse files
authored
Tuple F# snippets (#7918)
1 parent d0e277b commit 7c0dd73

File tree

6 files changed

+212
-0
lines changed

6 files changed

+212
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
module create1
2+
3+
open System
4+
5+
let create1Tuple () =
6+
// <Snippet1>
7+
let tuple1 = Tuple.Create 12
8+
printfn $"{tuple1.Item1}" // Displays 12
9+
// </Snippet1>
10+
11+
let new1Tuple () =
12+
// <Snippet2>
13+
let tuple1 = Tuple<int> 12
14+
printfn $"{tuple1.Item1}" // Displays 12
15+
// </Snippet2>
16+
17+
let create2Tuple () =
18+
// <Snippet3>
19+
let tuple2 = Tuple.Create("New York", 32.68)
20+
printfn $"{tuple2.Item1}: {tuple2.Item2}"
21+
// Displays New York: 32.68
22+
// </Snippet3>
23+
24+
let new2Tuple () =
25+
// <Snippet4>
26+
let tuple2 = Tuple<string, double>("New York", 32.68)
27+
printfn $"{tuple2.Item1}: {tuple2.Item2}"
28+
// Displays New York: 32.68
29+
// </Snippet4>
30+
31+
let create3Tuple () =
32+
// <Snippet5>
33+
let tuple3 = Tuple.Create("New York", 32.68, 51.87)
34+
printfn $"{tuple3.Item1}: lo {tuple3.Item2}, hi {tuple3.Item3}"
35+
// Displays New York: lo 32.68, hi 51.87
36+
// </Snippet5>
37+
38+
let new3Tuple () =
39+
// <Snippet6>
40+
let tuple3 =
41+
Tuple<string, double, double>("New York", 32.68, 51.87)
42+
43+
printfn $"{tuple3.Item1}: lo {tuple3.Item2}, hi {tuple3.Item3}"
44+
// Displays New York: lo 32.68, hi 51.87
45+
// </Snippet6>
46+
47+
let create4Tuple () =
48+
// <Snippet7>
49+
let tuple4 =
50+
Tuple.Create("New York", 32.68, 51.87, 76.3)
51+
52+
printfn $"{tuple4.Item1}: Hi {tuple4.Item4}, Lo {tuple4.Item2}, Ave {tuple4.Item3}"
53+
// Displays New York: Hi 76.3, Lo 32.68, Ave 51.87
54+
// </Snippet7>
55+
56+
let new4Tuple () =
57+
// <Snippet8>
58+
let tuple4 =
59+
Tuple<string, double, double, double>("New York", 32.68, 51.87, 76.3)
60+
61+
printfn $"{tuple4.Item1}: Hi {tuple4.Item4}, Lo {tuple4.Item2}, Ave {tuple4.Item3}"
62+
// Displays New York: Hi 76.3, Lo 32.68, Ave 51.87
63+
// </Snippet8>
64+
65+
let create5Tuple () =
66+
// <Snippet9>
67+
let tuple5 =
68+
Tuple.Create("New York", 1990, 7322564, 2000, 8008278)
69+
70+
printfn $"{tuple5.Item1}: {tuple5.Item3:N0} in {tuple5.Item2}, {tuple5.Item5:N0} in {tuple5.Item4}"
71+
// Displays New York: 7,322,564 in 1990, 8,008,278 in 2000
72+
// </Snippet9>
73+
74+
let new5Tuple () =
75+
// <Snippet10>
76+
let tuple5 =
77+
Tuple<string, int, int, int, int>("New York", 1990, 7322564, 2000, 8008278)
78+
79+
printfn $"{tuple5.Item1}: {tuple5.Item3:N0} in {tuple5.Item2}, {tuple5.Item5:N0} in {tuple5.Item4}"
80+
// Displays New York: 7,322,564 in 1990, 8,008,278 in 2000
81+
// </Snippet10>
82+
83+
let create6Tuple () =
84+
// <Snippet11>
85+
let tuple6 =
86+
Tuple.Create("Jane", 90, 87, 93, 67, 100)
87+
88+
printfn
89+
$"Test scores for {tuple6.Item1}: {tuple6.Item2}, {tuple6.Item3}, {tuple6.Item4}, {tuple6.Item5}, {tuple6.Item6}"
90+
// Displays Test scores for Jane: 90, 87, 93, 67, 100
91+
// </Snippet11>
92+
93+
let new6Tuple () =
94+
// <Snippet12>
95+
let tuple6 =
96+
Tuple<string, int, int, int, int, int>("Jane", 90, 87, 93, 67, 100)
97+
98+
printfn
99+
$"Test scores for {tuple6.Item1}: {tuple6.Item2}, {tuple6.Item3}, {tuple6.Item4}, {tuple6.Item5}, {tuple6.Item6}"
100+
// Displays Test scores for Jane: 90, 87, 93, 67, 100
101+
// </Snippet12>
102+
103+
let create7Tuple () =
104+
// <Snippet13>
105+
let tuple7 =
106+
Tuple.Create("Jane", 90, 87, 93, 67, 100, 92)
107+
108+
printfn
109+
$"Test scores for {tuple7.Item1}: {tuple7.Item2}, {tuple7.Item3}, {tuple7.Item4}, {tuple7.Item5}, {tuple7.Item6}, {tuple7.Item7}"
110+
// Displays Test scores for Jane: 90, 87, 93, 67, 100, 92
111+
// </Snippet13>
112+
113+
let new7Tuple () =
114+
// <Snippet14>
115+
let tuple7 =
116+
Tuple<string, int, int, int, int, int, int>("Jane", 90, 87, 93, 67, 100, 92)
117+
118+
printfn
119+
$"Test scores for {tuple7.Item1}: {tuple7.Item2}, {tuple7.Item3}, {tuple7.Item4}, {tuple7.Item5}, {tuple7.Item6}, {tuple7.Item7}"
120+
// Displays Test scores for Jane: 90, 87, 93, 67, 100, 92
121+
// </Snippet14>
122+
123+
create1Tuple ()
124+
new1Tuple ()
125+
create2Tuple ()
126+
new2Tuple ()
127+
create3Tuple ()
128+
new3Tuple ()
129+
create4Tuple ()
130+
new4Tuple ()
131+
create5Tuple ()
132+
new5Tuple ()
133+
create6Tuple ()
134+
new6Tuple ()
135+
create7Tuple ()
136+
new7Tuple ()
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module createntuple
2+
3+
// <Snippet17>
4+
open System
5+
6+
let primes = Tuple.Create(2, 3, 5, 7, 11, 13, 17, 19)
7+
printfn $"Prime numbers less than 20: {primes.Item1}, {primes.Item2}, {primes.Item3}, {primes.Item4}, {primes.Item5}, {primes.Item6}, {primes.Item7}, and {primes.Rest.Item1}"
8+
// Prime numbers less than 20: 2, 3, 5, 7, 11, 13, 17, and 19
9+
// </Snippet17>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module ctor8
2+
3+
// <Snippet20>
4+
open System
5+
6+
let primes = new Tuple<int, int, int, int, int, int, int, Tuple<int>>(2, 3, 5, 7, 11, 13, 16, Tuple<int> 19)
7+
// </Snippet20>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module example1
2+
3+
open System
4+
5+
let ctor1 () =
6+
// <Snippet1>
7+
// Create a 7-tuple.
8+
let population = Tuple<string, int, int, int, int, int, int>(
9+
"New York", 7891957, 7781984,
10+
7894862, 7071639, 7322564, 8008278)
11+
// Display the first and last elements.
12+
printfn $"Population of {population.Item1} in 2000: {population.Item7:N0}"
13+
// The example displays the following output:
14+
// Population of New York in 2000: 8,008,278
15+
// </Snippet1>
16+
17+
let factory () =
18+
// <Snippet2>
19+
// Create a 7-tuple.
20+
let population = Tuple.Create("New York", 7891957, 7781984, 7894862, 7071639, 7322564, 8008278)
21+
// Display the first and last elements.
22+
printfn $"Population of {population.Item1} in 2000: {population.Item7:N0}"
23+
// The example displays the following output:
24+
// Population of New York in 2000: 8,008,278
25+
// </Snippet2>
26+
27+
ctor1 ()
28+
factory ()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="example1.fs" />
9+
<Compile Include="createntuple.fs" />
10+
<Compile Include="create1.fs" />
11+
<Compile Include="ctor8.fs" />
12+
</ItemGroup>
13+
</Project>

xml/System/Tuple.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@
6161
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.
6262
6363
:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/example1.cs" interactive="try-dotnet-method" id="Snippet1":::
64+
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/example1.fs" id="Snippet1":::
6465
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.class/vb/example1.vb" id="Snippet1":::
6566
6667
Creating the same tuple object by using a helper method is more straightforward, as the following example shows.
6768
6869
:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/example1.cs" interactive="try-dotnet-method" id="Snippet2":::
70+
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/example1.fs" id="Snippet2":::
6971
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.class/vb/example1.vb" id="Snippet2":::
7072
7173
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.
@@ -79,6 +81,7 @@
7981
The following example creates an 8-tuple (octuple) that contains prime numbers that are less than 20.
8082
8183
:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/createntuple.cs" interactive="try-dotnet-method" id="Snippet17":::
84+
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/createntuple.fs" id="Snippet17":::
8285
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/createntuple.vb" id="Snippet17":::
8386
8487
]]></format>
@@ -158,11 +161,13 @@
158161
<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>.
159162
160163
:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/create1.cs" interactive="try-dotnet-method" id="Snippet1":::
164+
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/create1.fs" id="Snippet1":::
161165
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/create1.vb" id="Snippet1":::
162166
163167
This code is equivalent to the following call to the <xref:System.Tuple%601.%23ctor%2A> class constructor.
164168
165169
:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/create1.cs" interactive="try-dotnet-method" id="Snippet2":::
170+
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/create1.fs" id="Snippet2":::
166171
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/create1.vb" id="Snippet2":::
167172
168173
]]></format>
@@ -226,11 +231,13 @@
226231
<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.
227232
228233
:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/create1.cs" interactive="try-dotnet-method" id="Snippet3":::
234+
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/create1.fs" id="Snippet3":::
229235
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/create1.vb" id="Snippet3":::
230236
231237
This code is equivalent to the following call to the <xref:System.Tuple%602.%23ctor%2A> class constructor.
232238
233239
:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/create1.cs" interactive="try-dotnet-method" id="Snippet4":::
240+
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/create1.fs" id="Snippet4":::
234241
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/create1.vb" id="Snippet4":::
235242
236243
]]></format>
@@ -298,11 +305,13 @@
298305
<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.
299306
300307
:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/create1.cs" interactive="try-dotnet-method" id="Snippet5":::
308+
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/create1.fs" id="Snippet5":::
301309
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/create1.vb" id="Snippet5":::
302310
303311
This code is equivalent to the following call to the <xref:System.Tuple%603.%23ctor%2A?displayProperty=nameWithType> class constructor.
304312
305313
:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/create1.cs" interactive="try-dotnet-method" id="Snippet6":::
314+
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/create1.fs" id="Snippet6":::
306315
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/create1.vb" id="Snippet6":::
307316
308317
]]></format>
@@ -374,11 +383,13 @@
374383
<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.
375384
376385
:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/create1.cs" interactive="try-dotnet-method" id="Snippet7":::
386+
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/create1.fs" id="Snippet7":::
377387
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/create1.vb" id="Snippet7":::
378388
379389
This code is equivalent to the following call to the <xref:System.Tuple%604.%23ctor%2A?displayProperty=nameWithType> class constructor.
380390
381391
:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/create1.cs" interactive="try-dotnet-method" id="Snippet8":::
392+
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/create1.fs" id="Snippet8":::
382393
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/create1.vb" id="Snippet8":::
383394
384395
]]></format>
@@ -454,11 +465,13 @@
454465
<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.
455466
456467
:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/create1.cs" interactive="try-dotnet-method" id="Snippet9":::
468+
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/create1.fs" id="Snippet9":::
457469
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/create1.vb" id="Snippet9":::
458470
459471
This code is equivalent to the following call to the <xref:System.Tuple%605.%23ctor%2A> class constructor.
460472
461473
:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/create1.cs" interactive="try-dotnet-method" id="Snippet10":::
474+
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/create1.fs" id="Snippet10":::
462475
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/create1.vb" id="Snippet10":::
463476
464477
]]></format>
@@ -538,11 +551,13 @@
538551
<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.
539552
540553
:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/create1.cs" interactive="try-dotnet-method" id="Snippet11":::
554+
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/create1.fs" id="Snippet11":::
541555
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/create1.vb" id="Snippet11":::
542556
543557
This code is equivalent to the following call to the <xref:System.Tuple%606.%23ctor%2A> class constructor.
544558
545559
:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/create1.cs" interactive="try-dotnet-method" id="Snippet12":::
560+
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/create1.fs" id="Snippet12":::
546561
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/create1.vb" id="Snippet12":::
547562
548563
]]></format>
@@ -626,11 +641,13 @@
626641
<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.
627642
628643
:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/create1.cs" interactive="try-dotnet-method" id="Snippet13":::
644+
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/create1.fs" id="Snippet13":::
629645
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/create1.vb" id="Snippet13":::
630646
631647
This code is equivalent to the following call to the <xref:System.Tuple%607.%23ctor%2A> class constructor.
632648
633649
:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/create1.cs" interactive="try-dotnet-method" id="Snippet14":::
650+
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/create1.fs" id="Snippet14":::
634651
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/create1.vb" id="Snippet14":::
635652
636653
]]></format>
@@ -726,11 +743,13 @@
726743
The following example creates an 8-tuple whose components are prime numbers that are less than 20.
727744
728745
:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/createntuple.cs" interactive="try-dotnet-method" id="Snippet17":::
746+
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/createntuple.fs" id="Snippet17":::
729747
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/createntuple.vb" id="Snippet17":::
730748
731749
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.
732750
733751
:::code language="csharp" source="~/snippets/csharp/System/Tuple/Overview/ctor8.cs" id="Snippet20":::
752+
:::code language="fsharp" source="~/snippets/fsharp/System/Tuple/Overview/ctor8.fs" id="Snippet20":::
734753
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple.create/vb/ctor8.vb" id="Snippet20":::
735754
736755
]]></format>

0 commit comments

Comments
 (0)