Skip to content

Commit 8dcb4d4

Browse files
authored
Stack<T> F# snippet (#8872)
1 parent 71b41af commit 8dcb4d4

File tree

3 files changed

+102
-0
lines changed

3 files changed

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

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
The <xref:System.Collections.Generic.Stack%601.Contains%2A> method is used to show that the string "four" is in the first copy of the stack, after which the <xref:System.Collections.Generic.Stack%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Stack%601.Count%2A> property shows that the stack is empty.
122122
123123
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/StackT/Overview/source.cs" interactive="try-dotnet" id="Snippet1":::
124+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/StackT/Overview/source.fs" id="Snippet1":::
124125
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.Stack/vb/source.vb" id="Snippet1":::
125126
126127
]]></format>
@@ -201,6 +202,7 @@
201202
The <xref:System.Collections.Generic.Stack%601.Contains%2A> method is used to show that the string "four" is in the first copy of the stack, after which the <xref:System.Collections.Generic.Stack%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Stack%601.Count%2A> property shows that the stack is empty.
202203
203204
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/StackT/Overview/source.cs" interactive="try-dotnet" id="Snippet1":::
205+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/StackT/Overview/source.fs" id="Snippet1":::
204206
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.Stack/vb/source.vb" id="Snippet1":::
205207
206208
]]></format>
@@ -271,6 +273,7 @@
271273
The <xref:System.Collections.Generic.Stack%601.Contains%2A> method is used to show that the string "four" is in the first copy of the stack, after which the <xref:System.Collections.Generic.Stack%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Stack%601.Count%2A> property shows that the stack is empty.
272274
273275
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/StackT/Overview/source.cs" interactive="try-dotnet" id="Snippet1":::
276+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/StackT/Overview/source.fs" id="Snippet1":::
274277
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.Stack/vb/source.vb" id="Snippet1":::
275278
276279
]]></format>
@@ -395,6 +398,7 @@
395398
The <xref:System.Collections.Generic.Stack%601.Contains%2A> method is used to show that the string "four" is in the first copy of the stack, after which the <xref:System.Collections.Generic.Stack%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Stack%601.Count%2A> property shows that the stack is empty.
396399
397400
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/StackT/Overview/source.cs" interactive="try-dotnet" id="Snippet1":::
401+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/StackT/Overview/source.fs" id="Snippet1":::
398402
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.Stack/vb/source.vb" id="Snippet1":::
399403
400404
]]></format>
@@ -464,6 +468,7 @@
464468
The <xref:System.Collections.Generic.Stack%601.Contains%2A> method is used to show that the string "four" is in the first copy of the stack, after which the <xref:System.Collections.Generic.Stack%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Stack%601.Count%2A> property shows that the stack is empty.
465469
466470
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/StackT/Overview/source.cs" interactive="try-dotnet" id="Snippet1":::
471+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/StackT/Overview/source.fs" id="Snippet1":::
467472
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.Stack/vb/source.vb" id="Snippet1":::
468473
469474
]]></format>
@@ -534,6 +539,7 @@
534539
The <xref:System.Collections.Generic.Stack%601.Contains%2A> method is used to show that the string "four" is in the first copy of the stack, after which the <xref:System.Collections.Generic.Stack%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Stack%601.Count%2A> property shows that the stack is empty.
535540
536541
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/StackT/Overview/source.cs" interactive="try-dotnet" id="Snippet1":::
542+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/StackT/Overview/source.fs" id="Snippet1":::
537543
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.Stack/vb/source.vb" id="Snippet1":::
538544
539545
]]></format>
@@ -616,6 +622,7 @@
616622
The <xref:System.Collections.Generic.Stack%601.Contains%2A> method is used to show that the string "four" is in the first copy of the stack, after which the <xref:System.Collections.Generic.Stack%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Stack%601.Count%2A> property shows that the stack is empty.
617623
618624
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/StackT/Overview/source.cs" interactive="try-dotnet" id="Snippet1":::
625+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/StackT/Overview/source.fs" id="Snippet1":::
619626
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.Stack/vb/source.vb" id="Snippet1":::
620627
621628
]]></format>
@@ -727,6 +734,7 @@
727734
The <xref:System.Collections.Generic.Stack%601.Contains%2A> method is used to show that the string "four" is in the first copy of the stack, after which the <xref:System.Collections.Generic.Stack%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Stack%601.Count%2A> property shows that the stack is empty.
728735
729736
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/StackT/Overview/source.cs" interactive="try-dotnet" id="Snippet1":::
737+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/StackT/Overview/source.fs" id="Snippet1":::
730738
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.Stack/vb/source.vb" id="Snippet1":::
731739
732740
]]></format>
@@ -796,6 +804,7 @@
796804
The <xref:System.Collections.Generic.Stack%601.Contains%2A> method is used to show that the string "four" is in the first copy of the stack, after which the <xref:System.Collections.Generic.Stack%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Stack%601.Count%2A> property shows that the stack is empty.
797805
798806
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/StackT/Overview/source.cs" interactive="try-dotnet" id="Snippet1":::
807+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/StackT/Overview/source.fs" id="Snippet1":::
799808
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.Stack/vb/source.vb" id="Snippet1":::
800809
801810
]]></format>
@@ -866,6 +875,7 @@
866875
The <xref:System.Collections.Generic.Stack%601.Contains%2A> method is used to show that the string "four" is in the first copy of the stack, after which the <xref:System.Collections.Generic.Stack%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Stack%601.Count%2A> property shows that the stack is empty.
867876
868877
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/StackT/Overview/source.cs" interactive="try-dotnet" id="Snippet1":::
878+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/StackT/Overview/source.fs" id="Snippet1":::
869879
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.Stack/vb/source.vb" id="Snippet1":::
870880
871881
]]></format>
@@ -940,6 +950,7 @@
940950
The <xref:System.Collections.Generic.Stack%601.Contains%2A> method is used to show that the string "four" is in the first copy of the stack, after which the <xref:System.Collections.Generic.Stack%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Stack%601.Count%2A> property shows that the stack is empty.
941951
942952
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/StackT/Overview/source.cs" interactive="try-dotnet" id="Snippet1":::
953+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/StackT/Overview/source.fs" id="Snippet1":::
943954
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.Stack/vb/source.vb" id="Snippet1":::
944955
945956
]]></format>
@@ -1357,6 +1368,7 @@ finally {
13571368
The <xref:System.Collections.Generic.Stack%601.Contains%2A> method is used to show that the string "four" is in the first copy of the stack, after which the <xref:System.Collections.Generic.Stack%601.Clear%2A> method clears the copy and the <xref:System.Collections.Generic.Stack%601.Count%2A> property shows that the stack is empty.
13581369
13591370
:::code language="csharp" source="~/snippets/csharp/System.Collections.Generic/StackT/Overview/source.cs" interactive="try-dotnet" id="Snippet1":::
1371+
:::code language="fsharp" source="~/snippets/fsharp/System.Collections.Generic/StackT/Overview/source.fs" id="Snippet1":::
13601372
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Generic.Stack/vb/source.vb" id="Snippet1":::
13611373
13621374
]]></format>

0 commit comments

Comments
 (0)