From 7d87399c2b42bd6c60ca13d60ab8190347ecda38 Mon Sep 17 00:00:00 2001 From: albert-du <52804499+albert-du@users.noreply.github.com> Date: Sun, 3 Apr 2022 11:49:07 -0700 Subject: [PATCH] Tuple F# snippets --- .../fsharp/System/Tuple/Overview/create1.fs | 136 ++++++++++++++++++ .../System/Tuple/Overview/createntuple.fs | 9 ++ .../fsharp/System/Tuple/Overview/ctor8.fs | 7 + .../fsharp/System/Tuple/Overview/example1.fs | 28 ++++ .../fsharp/System/Tuple/Overview/fs.fsproj | 13 ++ xml/System/Tuple.xml | 19 +++ 6 files changed, 212 insertions(+) create mode 100644 snippets/fsharp/System/Tuple/Overview/create1.fs create mode 100644 snippets/fsharp/System/Tuple/Overview/createntuple.fs create mode 100644 snippets/fsharp/System/Tuple/Overview/ctor8.fs create mode 100644 snippets/fsharp/System/Tuple/Overview/example1.fs create mode 100644 snippets/fsharp/System/Tuple/Overview/fs.fsproj diff --git a/snippets/fsharp/System/Tuple/Overview/create1.fs b/snippets/fsharp/System/Tuple/Overview/create1.fs new file mode 100644 index 00000000000..fd09b2694c2 --- /dev/null +++ b/snippets/fsharp/System/Tuple/Overview/create1.fs @@ -0,0 +1,136 @@ +module create1 + +open System + +let create1Tuple () = + // + let tuple1 = Tuple.Create 12 + printfn $"{tuple1.Item1}" // Displays 12 + // + +let new1Tuple () = + // + let tuple1 = Tuple 12 + printfn $"{tuple1.Item1}" // Displays 12 + // + +let create2Tuple () = + // + let tuple2 = Tuple.Create("New York", 32.68) + printfn $"{tuple2.Item1}: {tuple2.Item2}" + // Displays New York: 32.68 + // + +let new2Tuple () = + // + let tuple2 = Tuple("New York", 32.68) + printfn $"{tuple2.Item1}: {tuple2.Item2}" + // Displays New York: 32.68 + // + +let create3Tuple () = + // + 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 + // + +let new3Tuple () = + // + let tuple3 = + Tuple("New York", 32.68, 51.87) + + printfn $"{tuple3.Item1}: lo {tuple3.Item2}, hi {tuple3.Item3}" + // Displays New York: lo 32.68, hi 51.87 + // + +let create4Tuple () = + // + 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 + // + +let new4Tuple () = + // + let tuple4 = + Tuple("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 + // + +let create5Tuple () = + // + 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 + // + +let new5Tuple () = + // + let tuple5 = + Tuple("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 + // + +let create6Tuple () = + // + 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 + // + +let new6Tuple () = + // + let tuple6 = + Tuple("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 + // + +let create7Tuple () = + // + 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 + // + +let new7Tuple () = + // + let tuple7 = + Tuple("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 + // + +create1Tuple () +new1Tuple () +create2Tuple () +new2Tuple () +create3Tuple () +new3Tuple () +create4Tuple () +new4Tuple () +create5Tuple () +new5Tuple () +create6Tuple () +new6Tuple () +create7Tuple () +new7Tuple () diff --git a/snippets/fsharp/System/Tuple/Overview/createntuple.fs b/snippets/fsharp/System/Tuple/Overview/createntuple.fs new file mode 100644 index 00000000000..59bb2bf7995 --- /dev/null +++ b/snippets/fsharp/System/Tuple/Overview/createntuple.fs @@ -0,0 +1,9 @@ +module createntuple + +// +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 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Tuple/Overview/ctor8.fs b/snippets/fsharp/System/Tuple/Overview/ctor8.fs new file mode 100644 index 00000000000..ba0bd7f7400 --- /dev/null +++ b/snippets/fsharp/System/Tuple/Overview/ctor8.fs @@ -0,0 +1,7 @@ +module ctor8 + +// +open System + +let primes = new Tuple>(2, 3, 5, 7, 11, 13, 16, Tuple 19) +// \ No newline at end of file diff --git a/snippets/fsharp/System/Tuple/Overview/example1.fs b/snippets/fsharp/System/Tuple/Overview/example1.fs new file mode 100644 index 00000000000..84c17035e5e --- /dev/null +++ b/snippets/fsharp/System/Tuple/Overview/example1.fs @@ -0,0 +1,28 @@ +module example1 + +open System + +let ctor1 () = + // + // Create a 7-tuple. + let population = Tuple( + "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 + // + +let factory () = + // + // 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 + // + +ctor1 () +factory () \ No newline at end of file diff --git a/snippets/fsharp/System/Tuple/Overview/fs.fsproj b/snippets/fsharp/System/Tuple/Overview/fs.fsproj new file mode 100644 index 00000000000..44fa1def7bd --- /dev/null +++ b/snippets/fsharp/System/Tuple/Overview/fs.fsproj @@ -0,0 +1,13 @@ + + + Exe + net6.0 + + + + + + + + + \ No newline at end of file diff --git a/xml/System/Tuple.xml b/xml/System/Tuple.xml index 49b47e01d1d..0f7df2c73bc 100644 --- a/xml/System/Tuple.xml +++ b/xml/System/Tuple.xml @@ -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 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 constructor. @@ -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"::: ]]> @@ -158,11 +161,13 @@ 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 method to instantiate a 1-tuple whose component is of type . :::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 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"::: ]]> @@ -226,11 +231,13 @@ 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 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 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"::: ]]> @@ -298,11 +305,13 @@ 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 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 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"::: ]]> @@ -374,11 +383,13 @@ 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 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 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"::: ]]> @@ -454,11 +465,13 @@ 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 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 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"::: ]]> @@ -538,11 +551,13 @@ 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 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 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"::: ]]> @@ -626,11 +641,13 @@ 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 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 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"::: ]]> @@ -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 class constructor instead of the factory creation method. Note that instantiating a object in this way involves considerably more code, because you must declare a nested object as the 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"::: ]]>