Skip to content

Commit cbea657

Browse files
authored
FSharp examples for SortedList (#6862) (#9637)
1 parent bfb157f commit cbea657

File tree

4 files changed

+213
-0
lines changed

4 files changed

+213
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net7.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="remarks.fs" />
9+
<Compile Include="source.fs" />
10+
</ItemGroup>
11+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module remarks
2+
open System
3+
open System.Collections.Generic
4+
5+
// Create a new sorted list of strings, with string
6+
// keys.
7+
let mySortedList = SortedList<int, string>()
8+
9+
// Add some elements to the list. There are no
10+
// duplicate keys, but some of the values are duplicates.
11+
mySortedList.Add(0, "notepad.exe")
12+
mySortedList.Add(1, "paint.exe")
13+
mySortedList.Add(2, "paint.exe")
14+
mySortedList.Add(3, "wordpad.exe")
15+
16+
//<Snippet11>
17+
let v = mySortedList.Values[3]
18+
//</Snippet11>
19+
20+
printfn $"Value at index 3: {v}"
21+
22+
//<Snippet12>
23+
for kvp in mySortedList do
24+
printfn $"Key = {kvp.Key}, Value = {kvp.Value}"
25+
//</Snippet12>
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
module source
2+
//<Snippet1>
3+
open System
4+
open System.Collections.Generic
5+
6+
//<Snippet2>
7+
// Create a new sorted list of strings, with string
8+
// keys.
9+
let openWith = SortedList<string, string>()
10+
11+
// Add some elements to the list. There are no
12+
// duplicate keys, but some of the values are duplicates.
13+
openWith.Add("txt", "notepad.exe")
14+
openWith.Add("bmp", "paint.exe")
15+
openWith.Add("dib", "paint.exe")
16+
openWith.Add("rtf", "wordpad.exe")
17+
18+
// The Add method throws an exception if the new key is
19+
// already in the list.
20+
try
21+
openWith.Add("txt", "winword.exe");
22+
with
23+
| :? ArgumentException ->
24+
printfn "An element with Key = \"txt\" already exists."
25+
//</Snippet2>
26+
27+
//<Snippet3>
28+
// The Item property is another name for the indexer, so you
29+
// can omit its name when accessing elements.
30+
printfn $"""For key = "rtf", value = {openWith["rtf"]}."""
31+
32+
// The indexer can be used to change the value associated
33+
// with a key.
34+
openWith["rtf"] <- "winword.exe"
35+
printfn $"""For key = "rtf", value = {openWith["rtf"]}."""
36+
37+
// If a key does not exist, setting the indexer for that key
38+
// adds a new key/value pair.
39+
openWith["doc"] <- "winword.exe";
40+
//</Snippet3>
41+
42+
//<Snippet4>
43+
// The indexer throws an exception if the requested key is
44+
// not in the list.
45+
try
46+
printfn $"""For key = "tif", value = {openWith["tif"]}."""
47+
with
48+
| :? KeyNotFoundException ->
49+
printfn "Key = \"tif\" is not found."
50+
//</Snippet4>
51+
52+
//<Snippet5>
53+
// When a program often has to try keys that turn out not to
54+
// be in the list, TryGetValue can be a more efficient
55+
// way to retrieve values.
56+
match openWith.TryGetValue("tif") with
57+
| true, value ->
58+
printfn "For key = \"tif\", value = {value}."
59+
| false, _ ->
60+
printfn "Key = \"tif\" is not found."
61+
//</Snippet5>
62+
63+
//<Snippet6>
64+
// ContainsKey can be used to test keys before inserting
65+
// them.
66+
if not (openWith.ContainsKey("ht")) then
67+
openWith.Add("ht", "hypertrm.exe");
68+
printfn """Value added for key = "ht": {openWith["ht"]}"""
69+
//</Snippet6>
70+
71+
//<Snippet7>
72+
// When you use foreach to enumerate list elements,
73+
// the elements are retrieved as KeyValuePair objects.
74+
Console.WriteLine()
75+
for kvp in openWith do
76+
printfn $"Key = {kvp.Key}, Value = {kvp.Value}"
77+
//</Snippet7>
78+
79+
//<Snippet8>
80+
// To get the values alone, use the Values property.
81+
let ilistValues = openWith.Values;
82+
83+
// The elements of the list are strongly typed with the
84+
// type that was specified for the SortedList values.
85+
Console.WriteLine()
86+
for s in ilistValues do
87+
printfn $"Value = {s}"
88+
89+
// The Values property is an efficient way to retrieve
90+
// values by index.
91+
printf "\nIndexed retrieval using the Values "
92+
printfn $"property: Values[2] = {openWith.Values[2]}"
93+
//</Snippet8>
94+
95+
//<Snippet9>
96+
// To get the keys alone, use the Keys property.
97+
let ilistKeys = openWith.Keys;
98+
99+
// The elements of the list are strongly typed with the
100+
// type that was specified for the SortedList keys.
101+
Console.WriteLine()
102+
for s in ilistKeys do
103+
printfn $"Key = {s}"
104+
105+
// The Keys property is an efficient way to retrieve
106+
// keys by index.
107+
printf "\nIndexed retrieval using the Keys "
108+
printfn $"property: Keys[2] = {openWith.Keys[2]}"
109+
//</Snippet9>
110+
111+
//<Snippet10>
112+
// Use the Remove method to remove a key/value pair.
113+
printfn "\nRemove(\"doc\")"
114+
openWith.Remove("doc") |> ignore
115+
116+
if not (openWith.ContainsKey("doc")) then
117+
printfn "Key \"doc\" is not found."
118+
//</Snippet10>
119+
120+
(* This code example produces the following output:
121+
122+
An element with Key = "txt" already exists.
123+
For key = "rtf", value = wordpad.exe.
124+
For key = "rtf", value = winword.exe.
125+
Key = "tif" is not found.
126+
Key = "tif" is not found.
127+
Value added for key = "ht": hypertrm.exe
128+
129+
Key = bmp, Value = paint.exe
130+
Key = dib, Value = paint.exe
131+
Key = doc, Value = winword.exe
132+
Key = ht, Value = hypertrm.exe
133+
Key = rtf, Value = winword.exe
134+
Key = txt, Value = notepad.exe
135+
136+
Value = paint.exe
137+
Value = paint.exe
138+
Value = winword.exe
139+
Value = hypertrm.exe
140+
Value = winword.exe
141+
Value = notepad.exe
142+
143+
Indexed retrieval using the Values property: Values[2] = winword.exe
144+
145+
Key = bmp
146+
Key = dib
147+
Key = doc
148+
Key = ht
149+
Key = rtf
150+
Key = txt
151+
152+
Indexed retrieval using the Keys property: Keys[2] = doc
153+
154+
Remove("doc")
155+
Key "doc" is not found.
156+
*)
157+
//</Snippet1>

xml/System.Collections.Generic/SortedList`2.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/remarks.cpp" id="Snippet11":::
133133
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/remarks.cs" id="Snippet11":::
134134
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/remarks.vb" id="Snippet11":::
135+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/remarks.fs" id="Snippet11":::
135136
136137
<xref:System.Collections.Generic.SortedList%602> is implemented as an array of key/value pairs, sorted by the key. Each element can be retrieved as a <xref:System.Collections.Generic.KeyValuePair%602> object.
137138
@@ -148,6 +149,7 @@
148149
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/remarks.cpp" id="Snippet12":::
149150
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/remarks.cs" id="Snippet12":::
150151
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/remarks.vb" id="Snippet12":::
152+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/remarks.fs" id="Snippet12":::
151153
152154
The `foreach` statement is a wrapper around the enumerator, which only allows reading from, not writing to, the collection.
153155
@@ -167,6 +169,7 @@
167169
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet1":::
168170
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" id="Snippet1":::
169171
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet1":::
172+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet1":::
170173
171174
]]></format>
172175
</remarks>
@@ -245,6 +248,7 @@
245248
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet2":::
246249
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" interactive="try-dotnet-method" id="Snippet2":::
247250
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet2":::
251+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet2":::
248252
249253
]]></format>
250254
</remarks>
@@ -707,6 +711,7 @@
707711
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet2":::
708712
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" interactive="try-dotnet-method" id="Snippet2":::
709713
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet2":::
714+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet2":::
710715
711716
]]></format>
712717
</remarks>
@@ -949,12 +954,15 @@
949954
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet6":::
950955
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" id="Snippet6":::
951956
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet6":::
957+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet6":::
952958
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet5":::
953959
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" id="Snippet5":::
954960
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet5":::
961+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet5":::
955962
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet4":::
956963
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" id="Snippet4":::
957964
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet4":::
965+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet4":::
958966
959967
]]></format>
960968
</remarks>
@@ -1397,12 +1405,15 @@
13971405
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet3":::
13981406
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" id="Snippet3":::
13991407
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet3":::
1408+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet3":::
14001409
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet4":::
14011410
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" id="Snippet4":::
14021411
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet4":::
1412+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet4":::
14031413
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet5":::
14041414
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" id="Snippet5":::
14051415
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet5":::
1416+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet5":::
14061417
14071418
]]></format>
14081419
</remarks>
@@ -1468,6 +1479,7 @@
14681479
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/remarks.cpp" id="Snippet11":::
14691480
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/remarks.cs" id="Snippet11":::
14701481
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/remarks.vb" id="Snippet11":::
1482+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/remarks.fs" id="Snippet11":::
14711483
14721484
Retrieving the value of this property is an O(1) operation.
14731485
@@ -1483,9 +1495,11 @@
14831495
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet9":::
14841496
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" id="Snippet9":::
14851497
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet9":::
1498+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet9":::
14861499
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet7":::
14871500
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" id="Snippet7":::
14881501
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet7":::
1502+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet7":::
14891503
14901504
]]></format>
14911505
</remarks>
@@ -1554,6 +1568,7 @@
15541568
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet10":::
15551569
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" id="Snippet10":::
15561570
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet10":::
1571+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet10":::
15571572
15581573
]]></format>
15591574
</remarks>
@@ -3218,9 +3233,11 @@ finally {
32183233
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet5":::
32193234
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" id="Snippet5":::
32203235
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet5":::
3236+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet5":::
32213237
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet4":::
32223238
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" id="Snippet4":::
32233239
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet4":::
3240+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet4":::
32243241
32253242
]]></format>
32263243
</remarks>
@@ -3288,6 +3305,7 @@ finally {
32883305
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/remarks.cpp" id="Snippet11":::
32893306
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/remarks.cs" id="Snippet11":::
32903307
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/remarks.vb" id="Snippet11":::
3308+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/remarks.fs" id="Snippet11":::
32913309
32923310
Retrieving the value of this property is an O(1) operation.
32933311
@@ -3303,9 +3321,11 @@ finally {
33033321
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet8":::
33043322
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" id="Snippet8":::
33053323
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet8":::
3324+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet8":::
33063325
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Generic.SortedList/cpp/source.cpp" id="Snippet7":::
33073326
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs" id="Snippet7":::
33083327
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.SortedList/VB/source.vb" id="Snippet7":::
3328+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.fs" id="Snippet7":::
33093329
33103330
]]></format>
33113331
</remarks>

0 commit comments

Comments
 (0)