From 759e514705957c2449e1497ff4565b3ded6315c3 Mon Sep 17 00:00:00 2001 From: Evgeniy Andreev Date: Sun, 25 Feb 2024 00:53:09 +0400 Subject: [PATCH] FSharp examples for SortedList (#6862) --- .../SortedListTKey,TValue/Overview/fs.fsproj | 11 ++ .../SortedListTKey,TValue/Overview/remarks.fs | 25 +++ .../SortedListTKey,TValue/Overview/source.fs | 157 ++++++++++++++++++ .../SortedList`2.xml | 20 +++ 4 files changed, 213 insertions(+) create mode 100644 snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/fs.fsproj create mode 100644 snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/remarks.fs create mode 100644 snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs diff --git a/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/fs.fsproj b/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/fs.fsproj new file mode 100644 index 00000000000..42b85e7a623 --- /dev/null +++ b/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/fs.fsproj @@ -0,0 +1,11 @@ + + + Exe + net7.0 + + + + + + + diff --git a/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/remarks.fs b/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/remarks.fs new file mode 100644 index 00000000000..926d0aa7e10 --- /dev/null +++ b/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/remarks.fs @@ -0,0 +1,25 @@ +module remarks +open System +open System.Collections.Generic + +// Create a new sorted list of strings, with string +// keys. +let mySortedList = SortedList() + +// Add some elements to the list. There are no +// duplicate keys, but some of the values are duplicates. +mySortedList.Add(0, "notepad.exe") +mySortedList.Add(1, "paint.exe") +mySortedList.Add(2, "paint.exe") +mySortedList.Add(3, "wordpad.exe") + +// +let v = mySortedList.Values[3] +// + +printfn $"Value at index 3: {v}" + +// +for kvp in mySortedList do + printfn $"Key = {kvp.Key}, Value = {kvp.Value}" +// diff --git a/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs b/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs new file mode 100644 index 00000000000..ef5285e2cf6 --- /dev/null +++ b/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs @@ -0,0 +1,157 @@ +module source +// +open System +open System.Collections.Generic + +// +// Create a new sorted list of strings, with string +// keys. +let openWith = SortedList() + +// Add some elements to the list. There are no +// duplicate keys, but some of the values are duplicates. +openWith.Add("txt", "notepad.exe") +openWith.Add("bmp", "paint.exe") +openWith.Add("dib", "paint.exe") +openWith.Add("rtf", "wordpad.exe") + +// The Add method throws an exception if the new key is +// already in the list. +try + openWith.Add("txt", "winword.exe"); +with + | :? ArgumentException -> + printfn "An element with Key = \"txt\" already exists." +// + +// +// The Item property is another name for the indexer, so you +// can omit its name when accessing elements. +printfn $"""For key = "rtf", value = {openWith["rtf"]}.""" + +// The indexer can be used to change the value associated +// with a key. +openWith["rtf"] <- "winword.exe" +printfn $"""For key = "rtf", value = {openWith["rtf"]}.""" + +// If a key does not exist, setting the indexer for that key +// adds a new key/value pair. +openWith["doc"] <- "winword.exe"; +// + +// +// The indexer throws an exception if the requested key is +// not in the list. +try + printfn $"""For key = "tif", value = {openWith["tif"]}.""" +with + | :? KeyNotFoundException -> + printfn "Key = \"tif\" is not found." +// + +// +// When a program often has to try keys that turn out not to +// be in the list, TryGetValue can be a more efficient +// way to retrieve values. +match openWith.TryGetValue("tif") with +| true, value -> + printfn "For key = \"tif\", value = {value}." +| false, _ -> + printfn "Key = \"tif\" is not found." +// + +// +// ContainsKey can be used to test keys before inserting +// them. +if not (openWith.ContainsKey("ht")) then + openWith.Add("ht", "hypertrm.exe"); + printfn """Value added for key = "ht": {openWith["ht"]}""" +// + +// +// When you use foreach to enumerate list elements, +// the elements are retrieved as KeyValuePair objects. +Console.WriteLine() +for kvp in openWith do + printfn $"Key = {kvp.Key}, Value = {kvp.Value}" +// + +// +// To get the values alone, use the Values property. +let ilistValues = openWith.Values; + +// The elements of the list are strongly typed with the +// type that was specified for the SortedList values. +Console.WriteLine() +for s in ilistValues do + printfn $"Value = {s}" + +// The Values property is an efficient way to retrieve +// values by index. +printf "\nIndexed retrieval using the Values " +printfn $"property: Values[2] = {openWith.Values[2]}" +// + +// +// To get the keys alone, use the Keys property. +let ilistKeys = openWith.Keys; + +// The elements of the list are strongly typed with the +// type that was specified for the SortedList keys. +Console.WriteLine() +for s in ilistKeys do + printfn $"Key = {s}" + +// The Keys property is an efficient way to retrieve +// keys by index. +printf "\nIndexed retrieval using the Keys " +printfn $"property: Keys[2] = {openWith.Keys[2]}" +// + +// +// Use the Remove method to remove a key/value pair. +printfn "\nRemove(\"doc\")" +openWith.Remove("doc") |> ignore + +if not (openWith.ContainsKey("doc")) then + printfn "Key \"doc\" is not found." +// + +(* This code example produces the following output: + +An element with Key = "txt" already exists. +For key = "rtf", value = wordpad.exe. +For key = "rtf", value = winword.exe. +Key = "tif" is not found. +Key = "tif" is not found. +Value added for key = "ht": hypertrm.exe + +Key = bmp, Value = paint.exe +Key = dib, Value = paint.exe +Key = doc, Value = winword.exe +Key = ht, Value = hypertrm.exe +Key = rtf, Value = winword.exe +Key = txt, Value = notepad.exe + +Value = paint.exe +Value = paint.exe +Value = winword.exe +Value = hypertrm.exe +Value = winword.exe +Value = notepad.exe + +Indexed retrieval using the Values property: Values[2] = winword.exe + +Key = bmp +Key = dib +Key = doc +Key = ht +Key = rtf +Key = txt + +Indexed retrieval using the Keys property: Keys[2] = doc + +Remove("doc") +Key "doc" is not found. + *) +// diff --git a/xml/System.Collections.Generic/SortedList`2.xml b/xml/System.Collections.Generic/SortedList`2.xml index 2f95ad9f279..50376343758 100644 --- a/xml/System.Collections.Generic/SortedList`2.xml +++ b/xml/System.Collections.Generic/SortedList`2.xml @@ -132,6 +132,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/remarks.cpp" id="Snippet11"::: :::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/remarks.cs" id="Snippet11"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/remarks.vb" id="Snippet11"::: + :::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/remarks.fs" id="Snippet11"::: is implemented as an array of key/value pairs, sorted by the key. Each element can be retrieved as a object. @@ -148,6 +149,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/remarks.cpp" id="Snippet12"::: :::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/remarks.cs" id="Snippet12"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/remarks.vb" id="Snippet12"::: + :::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/remarks.fs" id="Snippet12"::: The `foreach` statement is a wrapper around the enumerator, which only allows reading from, not writing to, the collection. @@ -167,6 +169,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet1"::: ]]> @@ -245,6 +248,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet2"::: :::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" interactive="try-dotnet-method" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet2"::: ]]> @@ -707,6 +711,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet2"::: :::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" interactive="try-dotnet-method" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet2"::: ]]> @@ -949,12 +954,15 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet6"::: :::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" id="Snippet6"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet6"::: + :::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet6"::: :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet5"::: :::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" id="Snippet5"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet5"::: +:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet5"::: :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet4"::: :::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" id="Snippet4"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet4"::: +:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet4"::: ]]> @@ -1397,12 +1405,15 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet3"::: :::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" id="Snippet3"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet3"::: + :::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet3"::: :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet4"::: :::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" id="Snippet4"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet4"::: +:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet4"::: :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet5"::: :::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" id="Snippet5"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet5"::: +:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet5"::: ]]> @@ -1468,6 +1479,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/remarks.cpp" id="Snippet11"::: :::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/remarks.cs" id="Snippet11"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/remarks.vb" id="Snippet11"::: + :::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/remarks.fs" id="Snippet11"::: Retrieving the value of this property is an O(1) operation. @@ -1483,9 +1495,11 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet9"::: :::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" id="Snippet9"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet9"::: + :::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet9"::: :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet7"::: :::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" id="Snippet7"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet7"::: +:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet7"::: ]]> @@ -1554,6 +1568,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet10"::: :::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" id="Snippet10"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet10"::: + :::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet10"::: ]]> @@ -3218,9 +3233,11 @@ finally { :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet5"::: :::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" id="Snippet5"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet5"::: + :::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet5"::: :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet4"::: :::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" id="Snippet4"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet4"::: +:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet4"::: ]]> @@ -3288,6 +3305,7 @@ finally { :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/remarks.cpp" id="Snippet11"::: :::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/remarks.cs" id="Snippet11"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/remarks.vb" id="Snippet11"::: + :::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/remarks.fs" id="Snippet11"::: Retrieving the value of this property is an O(1) operation. @@ -3303,9 +3321,11 @@ finally { :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet8"::: :::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" id="Snippet8"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet8"::: + :::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet8"::: :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet7"::: :::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" id="Snippet7"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet7"::: +:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet7"::: ]]>