diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArraySegment/FS/arraysegment.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArraySegment/FS/arraysegment.fs new file mode 100644 index 00000000000..e2af2dc6fa4 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArraySegment/FS/arraysegment.fs @@ -0,0 +1,90 @@ +module arraysegment + +// The following code example passes an ArraySegment to a method. + +// +open System + +// Print functions. +let printIndexAndValues (myArr: string []) = + for i = 0 to myArr.Length - 1 do + printfn $" [{i}] : {myArr[i]}" + printfn "" + +let printIndexAndValuesSeg (arrSeg: ArraySegment) = + for i = arrSeg.Offset to arrSeg.Offset + arrSeg.Count - 1 do + printfn $" [{i}] : {arrSeg.Array[i]}" + printfn "" + +// Create and initialize a new string array. +let myArr = [| "The"; "quick"; "brown"; "fox"; "jumps"; "over"; "the"; "lazy"; "dog" |] + +// Display the initial contents of the array. +printfn "The original array initially contains:" +printIndexAndValues myArr + +// Define an array segment that contains the entire array. +let myArrSegAll = ArraySegment(myArr) + +// Display the contents of the ArraySegment. +printfn "The first array segment (with all the array's elements) contains:" +printIndexAndValuesSeg myArrSegAll + +// Define an array segment that contains the middle five values of the array. +let myArrSegMid = ArraySegment(myArr, 2, 5) + +// Display the contents of the ArraySegment. +printfn "The second array segment (with the middle five elements) contains:" +printIndexAndValuesSeg myArrSegMid + +// Modify the fourth element of the first array segment myArrSegAll. +myArrSegAll.Array[3] <- "LION" + +// Display the contents of the second array segment myArrSegMid. +// Note that the value of its second element also changed. +printfn "After the first array segment is modified, the second array segment now contains:" +printIndexAndValuesSeg myArrSegMid + + +(* +This code produces the following output. + +The original array initially contains: + [0] : The + [1] : quick + [2] : brown + [3] : fox + [4] : jumps + [5] : over + [6] : the + [7] : lazy + [8] : dog + +The first array segment (with all the array's elements) contains: + [0] : The + [1] : quick + [2] : brown + [3] : fox + [4] : jumps + [5] : over + [6] : the + [7] : lazy + [8] : dog + +The second array segment (with the middle five elements) contains: + [2] : brown + [3] : fox + [4] : jumps + [5] : over + [6] : the + +After the first array segment is modified, the second array segment now contains: + [2] : brown + [3] : LION + [4] : jumps + [5] : over + [6] : the + +*) + +// \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArraySegment/FS/fs.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArraySegment/FS/fs.fsproj new file mode 100644 index 00000000000..9fa6f8ceabd --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArraySegment/FS/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArraySegment/FS/segmentexample.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArraySegment/FS/segmentexample.fs new file mode 100644 index 00000000000..3a425810db7 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArraySegment/FS/segmentexample.fs @@ -0,0 +1,40 @@ +module segmentexample + +// +open System +open System.Threading.Tasks + +// Create array. +let arr = Array.init 50 (fun i -> i + 1) + +// Handle array in segments of 10. +let tasks = + Array.chunkBySize 10 arr + |> Array.mapi (fun m elements -> + let mutable segment = ArraySegment(arr, m * 10, elements.Length) + task { + for i = 0 to segment.Count - 1 do + segment[i] <- segment[i] * (m + 1) + } :> Task) + +try + Task.WhenAll(tasks).Wait() + let mutable i = 0 + + for value in arr do + printf $"{value, 3} " + i <- i + 1 + if i % 18 = 0 then printfn "" + +with :? AggregateException as e -> + printfn "Errors occurred when working with the array:" + + for inner in e.InnerExceptions do + printfn $"{inner.GetType().Name}: {inner.Message}" + + +// The example displays the following output: +// 1 2 3 4 5 6 7 8 9 10 22 24 26 28 30 32 34 36 +// 38 40 63 66 69 72 75 78 81 84 87 90 124 128 132 136 140 144 +// 148 152 156 160 205 210 215 220 225 230 235 240 245 250 +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.arraysegment.class/fs/example1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.arraysegment.class/fs/example1.fs new file mode 100644 index 00000000000..4b034991760 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.arraysegment.class/fs/example1.fs @@ -0,0 +1,22 @@ +// +open System + +let names = + [| "Adam"; "Bruce"; "Charles"; "Daniel" + "Ebenezer"; "Francis"; "Gilbert" + "Henry"; "Irving"; "John"; "Karl" + "Lucian"; "Michael" |] + +let partNames = ArraySegment(names, 2, 5) + +// Enumerate over the ArraySegment object. +for part in partNames do + printfn $"{part}" + +// The example displays the following output: +// Charles +// Daniel +// Ebenezer +// Francis +// Gilbert +// \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.arraysegment.class/fs/fs.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.arraysegment.class/fs/fs.fsproj new file mode 100644 index 00000000000..7a4a5ea8a1a --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.arraysegment.class/fs/fs.fsproj @@ -0,0 +1,9 @@ + + + Exe + net6.0 + + + + + \ No newline at end of file diff --git a/xml/System/ArraySegment`1.xml b/xml/System/ArraySegment`1.xml index 04dba41d3e3..27ad2da0e99 100644 --- a/xml/System/ArraySegment`1.xml +++ b/xml/System/ArraySegment`1.xml @@ -99,6 +99,7 @@ - For task-based asynchronous operations, you can use an 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 class to manipulate elements in this way changes the values of its underlying array. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.ArraySegment/CS/segmentexample.cs" id="Snippet2"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArraySegment/FS/segmentexample.fs" id="Snippet2"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.ArraySegment/VB/segmentexample.vb" id="Snippet2"::: Note, however, that although the structure can be used to divide an array into distinct segments, the segments are not completely independent of one another. The property returns the entire original array, not a copy of the array; therefore, changes made to the array returned by the property are made to the original array. If this is undesirable, you should perform operations on a copy of the array, rather than an object that represents a portion of the array. @@ -111,9 +112,10 @@ - Have the same number of elements. - If you want to retrieve an element by its index in the object, you must cast it to an object and retrieve it or modify it by using the property. The following example retrieves the element in an object that delimits a section of a string array. + If you want to retrieve an element by its index in the object, you must cast it to an object and retrieve it or modify it by using the property. Note that this is not necessary in F#. The following example retrieves the element in an object that delimits a section of a string array. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.arraysegment.class/cs/example1.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.arraysegment.class/fs/example1.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.arraysegment.class/vb/example1.vb" id="Snippet1"::: @@ -123,6 +125,7 @@ :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.ArraySegment/CPP/arraysegment.cpp" id="Snippet1"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.ArraySegment/CS/arraysegment.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArraySegment/FS/arraysegment.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.ArraySegment/VB/arraysegment.vb" id="Snippet1"::: ]]> @@ -149,6 +152,7 @@ :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.ArraySegment/CPP/arraysegment.cpp" id="Snippet1"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.ArraySegment/CS/arraysegment.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArraySegment/FS/arraysegment.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.ArraySegment/VB/arraysegment.vb" id="Snippet1"::: ]]> @@ -320,6 +324,7 @@ :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.ArraySegment/CPP/arraysegment.cpp" id="Snippet1"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.ArraySegment/CS/arraysegment.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArraySegment/FS/arraysegment.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.ArraySegment/VB/arraysegment.vb" id="Snippet1"::: ]]> @@ -498,6 +503,7 @@ The underlying array of is @@ -829,6 +835,7 @@ The underlying array of is @@ -1585,6 +1592,7 @@ The underlying array of is instance is cast to an interface, as the following example shows. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.arraysegment.class/cs/example1.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.arraysegment.class/fs/example1.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.arraysegment.class/vb/example1.vb" id="Snippet1"::: ]]>