Skip to content

Commit 71b41af

Browse files
authored
Queue<T> F# snippet (#8871)
1 parent 55cf9a0 commit 71b41af

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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="source.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
module source
2+
//<Snippet1>
3+
open System
4+
open System.Collections.Generic
5+
6+
let numbers = Queue()
7+
numbers.Enqueue "one"
8+
numbers.Enqueue "two"
9+
numbers.Enqueue "three"
10+
numbers.Enqueue "four"
11+
numbers.Enqueue "five"
12+
13+
// A queue can be enumerated without disturbing its contents.
14+
for number in numbers do
15+
printfn $"{number}"
16+
17+
printfn $"\nDequeuing '{numbers.Dequeue()}'"
18+
printfn $"Peek at next item to dequeue: {numbers.Peek()}"
19+
printfn $"Dequeuing '{numbers.Dequeue()}'"
20+
21+
// Create a copy of the queue, using the ToArray method and the
22+
// constructor that accepts an IEnumerable<T>.
23+
let queueCopy = numbers.ToArray() |> Queue
24+
25+
printfn $"\nContents of the first copy:"
26+
for number in queueCopy do
27+
printfn $"{number}"
28+
29+
// Create an array twice the size of the queue and copy the
30+
// elements of the queue, starting at the middle of the
31+
// array.
32+
let array2 = numbers.Count * 2 |> Array.zeroCreate
33+
numbers.CopyTo(array2, numbers.Count)
34+
35+
// Create a second queue, using the constructor that accepts an
36+
// IEnumerable(Of T).
37+
let queueCopy2 = Queue array2
38+
39+
printfn $"\nContents of the second copy, with duplicates and nulls:"
40+
for number in queueCopy2 do
41+
printfn $"{number}"
42+
printfn $"""\nqueueCopy.Contains "four" = {queueCopy.Contains "four"}"""
43+
44+
printfn $"\nqueueCopy.Clear()"
45+
queueCopy.Clear()
46+
printfn $"queueCopy.Count = {queueCopy.Count}"
47+
48+
// This code example produces the following output:
49+
// one
50+
// two
51+
// three
52+
// four
53+
// five
54+
//
55+
// Dequeuing 'one'
56+
// Peek at next item to dequeue: two
57+
// Dequeuing 'two'
58+
//
59+
// Contents of the first copy:
60+
// three
61+
// four
62+
// five
63+
//
64+
// Contents of the second copy, with duplicates and nulls:
65+
//
66+
//
67+
//
68+
// three
69+
// four
70+
// five
71+
//
72+
// queueCopy.Contains "four" = True
73+
//
74+
// queueCopy.Clear()
75+
//
76+
// queueCopy.Count = 0
77+
//</Snippet1>

xml/System.Collections.Generic/Queue`1.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
The <xref:System.Collections.Generic.Queue%601.Contains%2A> method is used to show that the string "four" is in the first copy of the queue, after which the <xref:System.Collections.Generic.Queue%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Queue%601.Count%2A> property shows that the queue is empty.
114114
115115
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/QueueT/Overview/source.cs" interactive="try-dotnet" id="Snippet1":::
116+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/QueueT/Overview/source.fs" id="Snippet1":::
116117
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.Queue/vb/source.vb" id="Snippet1":::
117118
118119
]]></format>
@@ -145,6 +146,7 @@
145146
The <xref:System.Collections.Generic.Queue%601.Contains%2A> method is used to show that the string "four" is in the first copy of the queue, after which the <xref:System.Collections.Generic.Queue%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Queue%601.Count%2A> property shows that the queue is empty.
146147
147148
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/QueueT/Overview/source.cs" interactive="try-dotnet" id="Snippet1":::
149+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/QueueT/Overview/source.fs" id="Snippet1":::
148150
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.Queue/vb/source.vb" id="Snippet1":::
149151
150152
]]></format>
@@ -369,6 +371,7 @@
369371
The <xref:System.Collections.Generic.Queue%601.Contains%2A> method is used to show that the string "four" is in the first copy of the queue, after which the <xref:System.Collections.Generic.Queue%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Queue%601.Count%2A> property shows that the queue is empty.
370372
371373
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/QueueT/Overview/source.cs" interactive="try-dotnet" id="Snippet1":::
374+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/QueueT/Overview/source.fs" id="Snippet1":::
372375
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.Queue/vb/source.vb" id="Snippet1":::
373376
374377
]]></format>
@@ -439,6 +442,7 @@
439442
The <xref:System.Collections.Generic.Queue%601.Contains%2A> method is used to show that the string "four" is in the first copy of the queue, after which the <xref:System.Collections.Generic.Queue%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Queue%601.Count%2A> property shows that the queue is empty.
440443
441444
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/QueueT/Overview/source.cs" interactive="try-dotnet" id="Snippet1":::
445+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/QueueT/Overview/source.fs" id="Snippet1":::
442446
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.Queue/vb/source.vb" id="Snippet1":::
443447
444448
]]></format>
@@ -574,6 +578,7 @@
574578
The <xref:System.Collections.Generic.Queue%601.Contains%2A> method is used to show that the string "four" is in the first copy of the queue, after which the <xref:System.Collections.Generic.Queue%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Queue%601.Count%2A> property shows that the queue is empty.
575579
576580
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/QueueT/Overview/source.cs" interactive="try-dotnet" id="Snippet1":::
581+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/QueueT/Overview/source.fs" id="Snippet1":::
577582
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.Queue/vb/source.vb" id="Snippet1":::
578583
579584
]]></format>
@@ -641,6 +646,7 @@
641646
The <xref:System.Collections.Generic.Queue%601.Contains%2A> method is used to show that the string "four" is in the first copy of the queue, after which the <xref:System.Collections.Generic.Queue%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Queue%601.Count%2A> property shows that the queue is empty.
642647
643648
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/QueueT/Overview/source.cs" interactive="try-dotnet" id="Snippet1":::
649+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/QueueT/Overview/source.fs" id="Snippet1":::
644650
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.Queue/vb/source.vb" id="Snippet1":::
645651
646652
]]></format>
@@ -711,6 +717,7 @@
711717
The <xref:System.Collections.Generic.Queue%601.Contains%2A> method is used to show that the string "four" is in the first copy of the queue, after which the <xref:System.Collections.Generic.Queue%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Queue%601.Count%2A> property shows that the queue is empty.
712718
713719
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/QueueT/Overview/source.cs" interactive="try-dotnet" id="Snippet1":::
720+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/QueueT/Overview/source.fs" id="Snippet1":::
714721
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.Queue/vb/source.vb" id="Snippet1":::
715722
716723
]]></format>
@@ -824,6 +831,7 @@
824831
The <xref:System.Collections.Generic.Queue%601.Contains%2A> method is used to show that the string "four" is in the first copy of the queue, after which the <xref:System.Collections.Generic.Queue%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Queue%601.Count%2A> property shows that the queue is empty.
825832
826833
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/QueueT/Overview/source.cs" interactive="try-dotnet" id="Snippet1":::
834+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/QueueT/Overview/source.fs" id="Snippet1":::
827835
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.Queue/vb/source.vb" id="Snippet1":::
828836
829837
]]></format>
@@ -893,6 +901,7 @@
893901
The <xref:System.Collections.Generic.Queue%601.Contains%2A> method is used to show that the string "four" is in the first copy of the queue, after which the <xref:System.Collections.Generic.Queue%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Queue%601.Count%2A> property shows that the queue is empty.
894902
895903
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/QueueT/Overview/source.cs" interactive="try-dotnet" id="Snippet1":::
904+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/QueueT/Overview/source.fs" id="Snippet1":::
896905
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.Queue/vb/source.vb" id="Snippet1":::
897906
898907
]]></format>
@@ -1313,6 +1322,7 @@ finally {
13131322
The <xref:System.Collections.Generic.Queue%601.Contains%2A> method is used to show that the string "four" is in the first copy of the queue, after which the <xref:System.Collections.Generic.Queue%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Queue%601.Count%2A> property shows that the queue is empty.
13141323
13151324
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/QueueT/Overview/source.cs" interactive="try-dotnet" id="Snippet1":::
1325+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/QueueT/Overview/source.fs" id="Snippet1":::
13161326
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.Queue/vb/source.vb" id="Snippet1":::
13171327
13181328
]]></format>

0 commit comments

Comments
 (0)