-
Notifications
You must be signed in to change notification settings - Fork 1.6k
System.ArraySegment F# snippets #7447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
BillWagner
merged 1 commit into
dotnet:main
from
albert-du:system-arraysegment-fsharp-snippets
Dec 3, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArraySegment/FS/arraysegment.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| module arraysegment | ||
|
|
||
| // The following code example passes an ArraySegment to a method. | ||
|
|
||
| // <Snippet1> | ||
| 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<string>) = | ||
| 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<string>(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<string>(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 | ||
|
|
||
| *) | ||
|
|
||
| // </Snippet1> |
10 changes: 10 additions & 0 deletions
10
samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArraySegment/FS/fs.fsproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net6.0</TargetFramework> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <Compile Include="arraysegment.fs" /> | ||
| <Compile Include="segmentexample.fs" /> | ||
| </ItemGroup> | ||
| </Project> |
40 changes: 40 additions & 0 deletions
40
samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArraySegment/FS/segmentexample.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| module segmentexample | ||
|
|
||
| // <Snippet2> | ||
| 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<int>(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 | ||
| // </Snippet2> |
22 changes: 22 additions & 0 deletions
22
samples/snippets/fsharp/VS_Snippets_CLR_System/system.arraysegment.class/fs/example1.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // <Snippet1> | ||
| open System | ||
|
|
||
| let names = | ||
| [| "Adam"; "Bruce"; "Charles"; "Daniel" | ||
| "Ebenezer"; "Francis"; "Gilbert" | ||
| "Henry"; "Irving"; "John"; "Karl" | ||
| "Lucian"; "Michael" |] | ||
|
|
||
| let partNames = ArraySegment<string>(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 | ||
| // </Snippet1> |
9 changes: 9 additions & 0 deletions
9
samples/snippets/fsharp/VS_Snippets_CLR_System/system.arraysegment.class/fs/fs.fsproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net6.0</TargetFramework> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <Compile Include="example1.fs" /> | ||
| </ItemGroup> | ||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.