From 4927a7a6a890a9b7b7dfe8de58f53945aa7a0e9b Mon Sep 17 00:00:00 2001 From: albert-du <52804499+albert-du@users.noreply.github.com> Date: Fri, 8 Apr 2022 17:27:59 -0700 Subject: [PATCH] Tuple F# snippets --- .../System/TupleT1,T2/Equals/equals1.fs | 39 +++++++++++++ .../System/TupleT1,T2/Equals/equals2.fs | 50 ++++++++++++++++ .../fsharp/System/TupleT1,T2/Equals/fs.fsproj | 11 ++++ .../System/TupleT1,T2/Overview/example1.fs | 33 +++++++++++ .../System/TupleT1,T2/Overview/fs.fsproj | 11 ++++ .../System/TupleT1,T2/Overview/item1.fs | 34 +++++++++++ .../compareto1.fs | 47 +++++++++++++++ .../compareto2.fs | 58 +++++++++++++++++++ .../fs.fsproj | 10 ++++ .../System/TupleT1,T2/ToString/fs.fsproj | 10 ++++ .../System/TupleT1,T2/ToString/tostring1.fs | 24 ++++++++ xml/System/Tuple`2.xml | 11 ++++ 12 files changed, 338 insertions(+) create mode 100644 snippets/fsharp/System/TupleT1,T2/Equals/equals1.fs create mode 100644 snippets/fsharp/System/TupleT1,T2/Equals/equals2.fs create mode 100644 snippets/fsharp/System/TupleT1,T2/Equals/fs.fsproj create mode 100644 snippets/fsharp/System/TupleT1,T2/Overview/example1.fs create mode 100644 snippets/fsharp/System/TupleT1,T2/Overview/fs.fsproj create mode 100644 snippets/fsharp/System/TupleT1,T2/Overview/item1.fs create mode 100644 snippets/fsharp/System/TupleT1,T2/System.Collections.IStructuralComparable.CompareTo/compareto1.fs create mode 100644 snippets/fsharp/System/TupleT1,T2/System.Collections.IStructuralComparable.CompareTo/compareto2.fs create mode 100644 snippets/fsharp/System/TupleT1,T2/System.Collections.IStructuralComparable.CompareTo/fs.fsproj create mode 100644 snippets/fsharp/System/TupleT1,T2/ToString/fs.fsproj create mode 100644 snippets/fsharp/System/TupleT1,T2/ToString/tostring1.fs diff --git a/snippets/fsharp/System/TupleT1,T2/Equals/equals1.fs b/snippets/fsharp/System/TupleT1,T2/Equals/equals1.fs new file mode 100644 index 00000000000..508e97c3442 --- /dev/null +++ b/snippets/fsharp/System/TupleT1,T2/Equals/equals1.fs @@ -0,0 +1,39 @@ +module equals1 + +// +open System + +let scores = + [| Tuple>("Dan", 90) + Tuple>("Ernie", Nullable()) + Tuple>("Jill", 88) + Tuple>("Ernie", Nullable()) + Tuple>("Nancy", 88) + Tuple>("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 +// \ No newline at end of file diff --git a/snippets/fsharp/System/TupleT1,T2/Equals/equals2.fs b/snippets/fsharp/System/TupleT1,T2/Equals/equals2.fs new file mode 100644 index 00000000000..cfe1bdb1d7b --- /dev/null +++ b/snippets/fsharp/System/TupleT1,T2/Equals/equals2.fs @@ -0,0 +1,50 @@ +module equals2 + +// +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())}" + 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 +// \ No newline at end of file diff --git a/snippets/fsharp/System/TupleT1,T2/Equals/fs.fsproj b/snippets/fsharp/System/TupleT1,T2/Equals/fs.fsproj new file mode 100644 index 00000000000..dffbaeb281d --- /dev/null +++ b/snippets/fsharp/System/TupleT1,T2/Equals/fs.fsproj @@ -0,0 +1,11 @@ + + + Exe + net6.0 + + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/TupleT1,T2/Overview/example1.fs b/snippets/fsharp/System/TupleT1,T2/Overview/example1.fs new file mode 100644 index 00000000000..55e2cb4193a --- /dev/null +++ b/snippets/fsharp/System/TupleT1,T2/Overview/example1.fs @@ -0,0 +1,33 @@ +module example1 + +// +open System + +let scores = + [| Tuple>("Jack", 78) + Tuple>("Abbey", 92) + Tuple>("Dave", 88) + Tuple>("Sam", 91) + Tuple>("Ed", Nullable()) + Tuple>("Penelope", 82) + Tuple>("Linda", 99) + Tuple>("Judith", 84) |] + +let computeMean (scores: Tuple>[]) (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) +// \ No newline at end of file diff --git a/snippets/fsharp/System/TupleT1,T2/Overview/fs.fsproj b/snippets/fsharp/System/TupleT1,T2/Overview/fs.fsproj new file mode 100644 index 00000000000..0a1a07516e8 --- /dev/null +++ b/snippets/fsharp/System/TupleT1,T2/Overview/fs.fsproj @@ -0,0 +1,11 @@ + + + Exe + net6.0 + + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/TupleT1,T2/Overview/item1.fs b/snippets/fsharp/System/TupleT1,T2/Overview/item1.fs new file mode 100644 index 00000000000..fc0761f96b6 --- /dev/null +++ b/snippets/fsharp/System/TupleT1,T2/Overview/item1.fs @@ -0,0 +1,34 @@ +module item1 + +// +open System + +let integerDivide (dividend: int) divisor = + try + let quotient, remainder = Math.DivRem(dividend, divisor) + Tuple(quotient, remainder) + with :? DivideByZeroException -> + Unchecked.defaultof> + +[] +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} = " + + 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} = " + 0 +// The example displays the following output: +// 136945 \ 178 = 769, remainder 63 +// 2147483647 \ -2073 = -1035930, remainder 757 +// \ No newline at end of file diff --git a/snippets/fsharp/System/TupleT1,T2/System.Collections.IStructuralComparable.CompareTo/compareto1.fs b/snippets/fsharp/System/TupleT1,T2/System.Collections.IStructuralComparable.CompareTo/compareto1.fs new file mode 100644 index 00000000000..a27af242360 --- /dev/null +++ b/snippets/fsharp/System/TupleT1,T2/System.Collections.IStructuralComparable.CompareTo/compareto1.fs @@ -0,0 +1,47 @@ +module compareto1 + +// +open System + +let scores = + [| Tuple>("Jack", 78) + Tuple>("Abbey", 92) + Tuple>("Dave", 88) + Tuple>("Sam", 91) + Tuple>("Ed", Nullable()) + Tuple>("Penelope", 82) + Tuple>("Linda", 99) + Tuple>("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) +// \ No newline at end of file diff --git a/snippets/fsharp/System/TupleT1,T2/System.Collections.IStructuralComparable.CompareTo/compareto2.fs b/snippets/fsharp/System/TupleT1,T2/System.Collections.IStructuralComparable.CompareTo/compareto2.fs new file mode 100644 index 00000000000..e0a074a466e --- /dev/null +++ b/snippets/fsharp/System/TupleT1,T2/System.Collections.IStructuralComparable.CompareTo/compareto2.fs @@ -0,0 +1,58 @@ +module compareto2 + +// +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>("Jack", 78) + Tuple>("Abbey", 92) + Tuple>("Dave", 88) + Tuple>("Sam", 91) + Tuple>("Ed", Nullable()) + Tuple>("Penelope", 82) + Tuple>("Linda", 99) + Tuple>("Judith", 84) |] + +printfn "The values in unsorted order:" +for score in scores do + printfn $"{score}" + +printfn "" + +Array.Sort(scores, ScoreComparer>()) + +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) +// \ No newline at end of file diff --git a/snippets/fsharp/System/TupleT1,T2/System.Collections.IStructuralComparable.CompareTo/fs.fsproj b/snippets/fsharp/System/TupleT1,T2/System.Collections.IStructuralComparable.CompareTo/fs.fsproj new file mode 100644 index 00000000000..9906e9a1329 --- /dev/null +++ b/snippets/fsharp/System/TupleT1,T2/System.Collections.IStructuralComparable.CompareTo/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/TupleT1,T2/ToString/fs.fsproj b/snippets/fsharp/System/TupleT1,T2/ToString/fs.fsproj new file mode 100644 index 00000000000..18e3d41d05a --- /dev/null +++ b/snippets/fsharp/System/TupleT1,T2/ToString/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/TupleT1,T2/ToString/tostring1.fs b/snippets/fsharp/System/TupleT1,T2/ToString/tostring1.fs new file mode 100644 index 00000000000..78d128af481 --- /dev/null +++ b/snippets/fsharp/System/TupleT1,T2/ToString/tostring1.fs @@ -0,0 +1,24 @@ +// +open System + +let scores = + [| Tuple>("Abbey", 92) + Tuple>("Dave", 88) + Tuple>("Ed", Nullable()) + Tuple>("Jack", 78) + Tuple>("Linda", 99) + Tuple>("Judith", 84) + Tuple>("Penelope", 82) + Tuple>("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) +// \ No newline at end of file diff --git a/xml/System/Tuple`2.xml b/xml/System/Tuple`2.xml index 97b6c4c51c1..d04ecc392dc 100644 --- a/xml/System/Tuple`2.xml +++ b/xml/System/Tuple`2.xml @@ -89,11 +89,13 @@ - To provide easy access to, and manipulation of, a data set. The following example defines an array of objects that contain the names of students and their corresponding test scores. It then iterates the array to calculate the mean test score. :::code language="csharp" source="~/snippets/csharp/System/TupleT1,T2/Overview/example1.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/TupleT1,T2/Overview/example1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple`2.class/vb/example1.vb" id="Snippet1"::: - To return multiple values from a method without the use of `out` parameters (in C#) or `ByRef` parameters (in Visual Basic). For example, the following example uses a object to return the quotient and the remainder that result from integer division. :::code language="csharp" source="~/snippets/csharp/System/TupleT1,T2/Overview/item1.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/TupleT1,T2/Overview/item1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple`2.item1/vb/item1.vb" id="Snippet1"::: - To pass multiple values to a method through a single parameter. For example, the method has a single parameter that lets you supply one value to the method that the thread executes at startup. If you supply a object as the method argument, you can supply the thread's startup routine with two items of data. @@ -156,11 +158,13 @@ You can also use the static method 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 whose components are of type and . :::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 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"::: ]]> @@ -235,6 +239,7 @@ The following example calls the method to determine whether any of the objects in an array of objects are equal to one another. The output reflects the fact that the method returns `true` when comparing objects whose components have equal values. :::code language="csharp" source="~/snippets/csharp/System/TupleT1,T2/Equals/equals1.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/TupleT1,T2/Equals/equals1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple`2.equals/vb/equals1.vb" id="Snippet1"::: ]]> @@ -335,6 +340,7 @@ The example illustrates the use of the and properties to define a method that returns multiple values in the form of a 2-tuple. :::code language="csharp" source="~/snippets/csharp/System/TupleT1,T2/Overview/item1.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/TupleT1,T2/Overview/item1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple`2.item1/vb/item1.vb" id="Snippet1"::: ]]> @@ -394,6 +400,7 @@ The example illustrates the use of the and properties to define a method that returns multiple values in the form of a 2-tuple. :::code language="csharp" source="~/snippets/csharp/System/TupleT1,T2/Overview/item1.cs" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/TupleT1,T2/Overview/item1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple`2.item1/vb/item1.vb" id="Snippet1"::: ]]> @@ -481,6 +488,7 @@ The following example creates an array of objects that consist of a student's name and test score. It displays the component of each tuple in the array in unsorted order, sorts the array, and then calls to display the value of each tuple in sorted order. To sort the array, the example defines a generic `ScoreComparer` class that implements the interface and sorts the objects in ascending order by the value of their second component rather than their first component. Note that the example does not directly call the method. This method is called implicitly by the method for each element in the array. :::code language="csharp" source="~/snippets/csharp/System/TupleT1,T2/System.Collections.IStructuralComparable.CompareTo/compareto2.cs" interactive="try-dotnet" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System/TupleT1,T2/System.Collections.IStructuralComparable.CompareTo/compareto2.fs" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple`2.compareto/vb/compareto2.vb" id="Snippet2"::: ]]> @@ -558,6 +566,7 @@ The following example defines an `Item2Comparer` class that implements the interface and changes the way in which objects are evaluated for equality. The method always returns `true` when it is passed the property values of two objects, and it calls the method to evaluate their property values. As a result, the method tests for equality based only on the value of the property. The output illustrates the result for a data set of objects that record the names of runners and the distances that they ran. :::code language="csharp" source="~/snippets/csharp/System/TupleT1,T2/Equals/equals2.cs" interactive="try-dotnet" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System/TupleT1,T2/Equals/equals2.fs" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple`2.equals/vb/equals2.vb" id="Snippet2"::: ]]> @@ -704,6 +713,7 @@ The following example creates an array of objects that consist of a student's name and test score. It displays the component of each tuple in the array in unsorted order, sorts the array, and then calls to display the value of each tuple in sorted order. The output shows that the array has been sorted by its first component. Note that the example does not directly call the method. This method is called implicitly by the method for each element in the array. :::code language="csharp" source="~/snippets/csharp/System/TupleT1,T2/System.Collections.IStructuralComparable.CompareTo/compareto1.cs" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/TupleT1,T2/System.Collections.IStructuralComparable.CompareTo/compareto1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple`2.compareto/vb/compareto1.vb" id="Snippet1"::: ]]> @@ -858,6 +868,7 @@ The following example illustrates the method. :::code language="csharp" source="~/snippets/csharp/System/TupleT1,T2/ToString/tostring1.cs" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/TupleT1,T2/ToString/tostring1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.tuple`2.tostring/vb/tostring1.vb" id="Snippet1"::: ]]>