Skip to content

Commit 76b6db3

Browse files
authored
Add snippets (dotnet#7447)
1 parent 47d8dff commit 76b6db3

File tree

6 files changed

+180
-1
lines changed

6 files changed

+180
-1
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
module arraysegment
2+
3+
// The following code example passes an ArraySegment to a method.
4+
5+
// <Snippet1>
6+
open System
7+
8+
// Print functions.
9+
let printIndexAndValues (myArr: string []) =
10+
for i = 0 to myArr.Length - 1 do
11+
printfn $" [{i}] : {myArr[i]}"
12+
printfn ""
13+
14+
let printIndexAndValuesSeg (arrSeg: ArraySegment<string>) =
15+
for i = arrSeg.Offset to arrSeg.Offset + arrSeg.Count - 1 do
16+
printfn $" [{i}] : {arrSeg.Array[i]}"
17+
printfn ""
18+
19+
// Create and initialize a new string array.
20+
let myArr = [| "The"; "quick"; "brown"; "fox"; "jumps"; "over"; "the"; "lazy"; "dog" |]
21+
22+
// Display the initial contents of the array.
23+
printfn "The original array initially contains:"
24+
printIndexAndValues myArr
25+
26+
// Define an array segment that contains the entire array.
27+
let myArrSegAll = ArraySegment<string>(myArr)
28+
29+
// Display the contents of the ArraySegment.
30+
printfn "The first array segment (with all the array's elements) contains:"
31+
printIndexAndValuesSeg myArrSegAll
32+
33+
// Define an array segment that contains the middle five values of the array.
34+
let myArrSegMid = ArraySegment<string>(myArr, 2, 5)
35+
36+
// Display the contents of the ArraySegment.
37+
printfn "The second array segment (with the middle five elements) contains:"
38+
printIndexAndValuesSeg myArrSegMid
39+
40+
// Modify the fourth element of the first array segment myArrSegAll.
41+
myArrSegAll.Array[3] <- "LION"
42+
43+
// Display the contents of the second array segment myArrSegMid.
44+
// Note that the value of its second element also changed.
45+
printfn "After the first array segment is modified, the second array segment now contains:"
46+
printIndexAndValuesSeg myArrSegMid
47+
48+
49+
(*
50+
This code produces the following output.
51+
52+
The original array initially contains:
53+
[0] : The
54+
[1] : quick
55+
[2] : brown
56+
[3] : fox
57+
[4] : jumps
58+
[5] : over
59+
[6] : the
60+
[7] : lazy
61+
[8] : dog
62+
63+
The first array segment (with all the array's elements) contains:
64+
[0] : The
65+
[1] : quick
66+
[2] : brown
67+
[3] : fox
68+
[4] : jumps
69+
[5] : over
70+
[6] : the
71+
[7] : lazy
72+
[8] : dog
73+
74+
The second array segment (with the middle five elements) contains:
75+
[2] : brown
76+
[3] : fox
77+
[4] : jumps
78+
[5] : over
79+
[6] : the
80+
81+
After the first array segment is modified, the second array segment now contains:
82+
[2] : brown
83+
[3] : LION
84+
[4] : jumps
85+
[5] : over
86+
[6] : the
87+
88+
*)
89+
90+
// </Snippet1>
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>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="arraysegment.fs" />
8+
<Compile Include="segmentexample.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
module segmentexample
2+
3+
// <Snippet2>
4+
open System
5+
open System.Threading.Tasks
6+
7+
// Create array.
8+
let arr = Array.init 50 (fun i -> i + 1)
9+
10+
// Handle array in segments of 10.
11+
let tasks =
12+
Array.chunkBySize 10 arr
13+
|> Array.mapi (fun m elements ->
14+
let mutable segment = ArraySegment<int>(arr, m * 10, elements.Length)
15+
task {
16+
for i = 0 to segment.Count - 1 do
17+
segment[i] <- segment[i] * (m + 1)
18+
} :> Task)
19+
20+
try
21+
Task.WhenAll(tasks).Wait()
22+
let mutable i = 0
23+
24+
for value in arr do
25+
printf $"{value, 3} "
26+
i <- i + 1
27+
if i % 18 = 0 then printfn ""
28+
29+
with :? AggregateException as e ->
30+
printfn "Errors occurred when working with the array:"
31+
32+
for inner in e.InnerExceptions do
33+
printfn $"{inner.GetType().Name}: {inner.Message}"
34+
35+
36+
// The example displays the following output:
37+
// 1 2 3 4 5 6 7 8 9 10 22 24 26 28 30 32 34 36
38+
// 38 40 63 66 69 72 75 78 81 84 87 90 124 128 132 136 140 144
39+
// 148 152 156 160 205 210 215 220 225 230 235 240 245 250
40+
// </Snippet2>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// <Snippet1>
2+
open System
3+
4+
let names =
5+
[| "Adam"; "Bruce"; "Charles"; "Daniel"
6+
"Ebenezer"; "Francis"; "Gilbert"
7+
"Henry"; "Irving"; "John"; "Karl"
8+
"Lucian"; "Michael" |]
9+
10+
let partNames = ArraySegment<string>(names, 2, 5)
11+
12+
// Enumerate over the ArraySegment object.
13+
for part in partNames do
14+
printfn $"{part}"
15+
16+
// The example displays the following output:
17+
// Charles
18+
// Daniel
19+
// Ebenezer
20+
// Francis
21+
// Gilbert
22+
// </Snippet1>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="example1.fs" />
8+
</ItemGroup>
9+
</Project>

xml/System/ArraySegment`1.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
- For task-based asynchronous operations, you can use an <xref:System.ArraySegment%601> object to ensure that each task operates on a distinct segment of the array. The following example divides an array into individual segments with up to ten elements. Each element in the segment is multiplied by its segment number. The result shows that using the <xref:System.ArraySegment%601> class to manipulate elements in this way changes the values of its underlying array.
100100
101101
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.ArraySegment/CS/segmentexample.cs" id="Snippet2":::
102+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArraySegment/FS/segmentexample.fs" id="Snippet2":::
102103
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.ArraySegment/VB/segmentexample.vb" id="Snippet2":::
103104
104105
Note, however, that although the <xref:System.ArraySegment%601> structure can be used to divide an array into distinct segments, the segments are not completely independent of one another. The <xref:System.ArraySegment%601.Array%2A> property returns the entire original array, not a copy of the array; therefore, changes made to the array returned by the <xref:System.ArraySegment%601.Array%2A> property are made to the original array. If this is undesirable, you should perform operations on a copy of the array, rather than an <xref:System.ArraySegment%601> object that represents a portion of the array.
@@ -111,9 +112,10 @@
111112
112113
- Have the same number of elements.
113114
114-
If you want to retrieve an element by its index in the <xref:System.ArraySegment%601> object, you must cast it to an <xref:System.Collections.Generic.IList%601> object and retrieve it or modify it by using the <xref:System.Collections.Generic.IList%601.Item%2A?displayProperty=nameWithType> property. The following example retrieves the element in an <xref:System.ArraySegment%601> object that delimits a section of a string array.
115+
If you want to retrieve an element by its index in the <xref:System.ArraySegment%601> object, you must cast it to an <xref:System.Collections.Generic.IList%601> object and retrieve it or modify it by using the <xref:System.Collections.Generic.IList%601.Item%2A?displayProperty=nameWithType> property. Note that this is not necessary in F#. The following example retrieves the element in an <xref:System.ArraySegment%601> object that delimits a section of a string array.
115116
116117
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.arraysegment.class/cs/example1.cs" interactive="try-dotnet" id="Snippet1":::
118+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.arraysegment.class/fs/example1.fs" id="Snippet1":::
117119
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.arraysegment.class/vb/example1.vb" id="Snippet1":::
118120
119121
@@ -123,6 +125,7 @@
123125
124126
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.ArraySegment/CPP/arraysegment.cpp" id="Snippet1":::
125127
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.ArraySegment/CS/arraysegment.cs" interactive="try-dotnet" id="Snippet1":::
128+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArraySegment/FS/arraysegment.fs" id="Snippet1":::
126129
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.ArraySegment/VB/arraysegment.vb" id="Snippet1":::
127130
128131
]]></format>
@@ -149,6 +152,7 @@
149152
150153
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.ArraySegment/CPP/arraysegment.cpp" id="Snippet1":::
151154
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.ArraySegment/CS/arraysegment.cs" interactive="try-dotnet" id="Snippet1":::
155+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArraySegment/FS/arraysegment.fs" id="Snippet1":::
152156
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.ArraySegment/VB/arraysegment.vb" id="Snippet1":::
153157
154158
]]></format>
@@ -320,6 +324,7 @@
320324
321325
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.ArraySegment/CPP/arraysegment.cpp" id="Snippet1":::
322326
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.ArraySegment/CS/arraysegment.cs" interactive="try-dotnet" id="Snippet1":::
327+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArraySegment/FS/arraysegment.fs" id="Snippet1":::
323328
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.ArraySegment/VB/arraysegment.vb" id="Snippet1":::
324329
325330
]]></format>
@@ -498,6 +503,7 @@ The underlying array of <paramref name="destination" /> is <paramref name="null"
498503
499504
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.ArraySegment/CPP/arraysegment.cpp" id="Snippet1":::
500505
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.ArraySegment/CS/arraysegment.cs" id="Snippet1":::
506+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArraySegment/FS/arraysegment.fs" id="Snippet1":::
501507
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.ArraySegment/VB/arraysegment.vb" id="Snippet1":::
502508
503509
]]></format>
@@ -829,6 +835,7 @@ The underlying array of <paramref name="destination" /> is <paramref name="null"
829835
830836
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.ArraySegment/CPP/arraysegment.cpp" id="Snippet1":::
831837
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.ArraySegment/CS/arraysegment.cs" id="Snippet1":::
838+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArraySegment/FS/arraysegment.fs" id="Snippet1":::
832839
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.ArraySegment/VB/arraysegment.vb" id="Snippet1":::
833840
834841
]]></format>
@@ -1585,6 +1592,7 @@ The underlying array of <paramref name="destination" /> is <paramref name="null"
15851592
This member is an explicit interface member implementation. It can be used only when the <xref:System.ArraySegment%601> instance is cast to an <xref:System.Collections.Generic.IList%601> interface, as the following example shows.
15861593
15871594
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.arraysegment.class/cs/example1.cs" interactive="try-dotnet" id="Snippet1":::
1595+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.arraysegment.class/fs/example1.fs" id="Snippet1":::
15881596
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.arraysegment.class/vb/example1.vb" id="Snippet1":::
15891597
15901598
]]></format>

0 commit comments

Comments
 (0)