Skip to content

Commit e935c9c

Browse files
authored
System.Array F# snippets (#7432)
* add half of the snippets * Add remaining snippets * Update Array.xml * update snippets * Update Array.xml * Update Array.xml * Update Array.xml * update snippets
1 parent 2f83ee2 commit e935c9c

File tree

95 files changed

+3135
-13
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+3135
-13
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Compile Include="source.fs" />
10+
</ItemGroup>
11+
12+
</Project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// <Snippet1>
2+
open System
3+
open System.Drawing
4+
open System.Collections.Generic
5+
6+
let pointFToPoint (pf: PointF) =
7+
Point(int pf.X, int pf.Y)
8+
9+
// Create an array of PointF objects.
10+
let apf =
11+
[| PointF(27.8f, 32.62f)
12+
PointF(99.3f, 147.273f)
13+
PointF(7.5f, 1412.2f) |]
14+
15+
// Display each element in the PointF array.
16+
printfn ""
17+
for p in apf do
18+
printfn $"{p}"
19+
20+
// Convert each PointF element to a Point object.
21+
let ap = Array.ConvertAll(apf, pointFToPoint)
22+
// let ap = Array.map pointFToPoint apf
23+
24+
// Display each element in the Point array.
25+
printfn ""
26+
for p in ap do
27+
printfn $"{p}"
28+
29+
// This code example produces the following output:
30+
// {X=27.8, Y=32.62}
31+
// {X=99.3, Y=147.273}
32+
// {X=7.5, Y=1412.2}
33+
//
34+
// {X=27,Y=32}
35+
// {X=99,Y=147}
36+
// {X=7,Y=1412}
37+
38+
// </Snippet1>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Compile Include="source.fs" />
10+
</ItemGroup>
11+
12+
</Project>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// <Snippet1>
2+
open System
3+
4+
// Search predicate returns true if a string ends in "saurus".
5+
let endsWithSaurus (s: string) =
6+
s.Length > 5 && s.Substring(s.Length - 6).ToLower() = "saurus"
7+
8+
// Search predicate returns true if a string ends in "raptor".
9+
let endsWithRaptor (s: string) =
10+
s.Length > 5 && s.Substring(s.Length - 6).ToLower() = "raptor"
11+
12+
// Search predicate returns true if a string ends in "tops".
13+
let endsWithTops (s: string) =
14+
s.Length > 3 && s.Substring(s.Length - 4).ToLower() = "tops"
15+
16+
type DinoDiscoverySet =
17+
{ Dinosaurs: string [] }
18+
19+
member this.DiscoverAll() =
20+
printfn ""
21+
for dino in this.Dinosaurs do
22+
printfn $"{dino}"
23+
24+
member this.DiscoverByEnding(ending: string) =
25+
let dinoType =
26+
match ending.ToLower() with
27+
| "raptor" -> endsWithRaptor
28+
| "tops" -> endsWithTops
29+
| "saurus" | _ -> endsWithSaurus
30+
31+
Array.Exists(this.Dinosaurs, dinoType)
32+
|> printfn "\nArray.Exists(dinosaurs, \"%s\"): %b" ending
33+
34+
Array.TrueForAll(this.Dinosaurs, dinoType)
35+
|> printfn "\nArray.TrueForAll(dinosaurs, \"%s\"): %b" ending
36+
37+
Array.Find(this.Dinosaurs, dinoType)
38+
|> printfn "\nArray.Find(dinosaurs, \"%s\"): %s" ending
39+
40+
Array.FindLast(this.Dinosaurs, dinoType)
41+
|> printfn "\nArray.FindLast(dinosaurs, \"%s\"): %s" ending
42+
43+
printfn $"\nArray.FindAll(dinosaurs, \"{ending}\"):"
44+
for dinosaur in Array.FindAll(this.Dinosaurs, dinoType) do
45+
printfn $"{dinosaur}"
46+
47+
let dinosaurs =
48+
[| "Compsognathus"; "Amargasaurus"; "Oviraptor"
49+
"Velociraptor"; "Deinonychus"; "Dilophosaurus"
50+
"Gallimimus"; "Triceratops" |]
51+
52+
let goMesozoic = { Dinosaurs = dinosaurs }
53+
54+
goMesozoic.DiscoverAll()
55+
goMesozoic.DiscoverByEnding "saurus"
56+
57+
58+
// This code example produces the following output:
59+
// Compsognathus
60+
// Amargasaurus
61+
// Oviraptor
62+
// Velociraptor
63+
// Deinonychus
64+
// Dilophosaurus
65+
// Gallimimus
66+
// Triceratops
67+
//
68+
// Array.Exists(dinosaurs, "saurus"): true
69+
//
70+
// Array.TrueForAll(dinosaurs, "saurus"): false
71+
//
72+
// Array.Find(dinosaurs, "saurus"): Amargasaurus
73+
//
74+
// Array.FindLast(dinosaurs, "saurus"): Dilophosaurus
75+
//
76+
// Array.FindAll(dinosaurs, "saurus"):
77+
// Amargasaurus
78+
// Dilophosaurus
79+
// </Snippet1>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Compile Include="source.fs" />
10+
</ItemGroup>
11+
12+
</Project>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// <Snippet1>
2+
open System
3+
4+
// Search predicate returns true if a string ends in "saurus".
5+
let endsWithSaurus (s: string) =
6+
s.Length > 5 && s.Substring(s.Length - 6).ToLower() = "saurus"
7+
8+
let dinosaurs =
9+
[| "Compsognathus"; "Amargasaurus"
10+
"Oviraptor"; "Velociraptor"
11+
"Deinonychus"; "Dilophosaurus"
12+
"Gallimimus"; "Triceratops" |]
13+
14+
printfn ""
15+
for dino in dinosaurs do
16+
printfn $"{dino}"
17+
18+
Array.FindIndex(dinosaurs, endsWithSaurus)
19+
|> printfn "\nArray.FindIndex(dinosaurs, EndsWithSaurus): %i"
20+
21+
Array.FindIndex(dinosaurs, 2, endsWithSaurus)
22+
|> printfn "\nArray.FindIndex(dinosaurs, 2, EndsWithSaurus): %i"
23+
24+
Array.FindIndex(dinosaurs, 2, 3, endsWithSaurus)
25+
|> printfn "\nArray.FindIndex(dinosaurs, 2, 3, EndsWithSaurus): %i"
26+
27+
28+
// This code example produces the following output:
29+
//
30+
// Compsognathus
31+
// Amargasaurus
32+
// Oviraptor
33+
// Velociraptor
34+
// Deinonychus
35+
// Dilophosaurus
36+
// Gallimimus
37+
// Triceratops
38+
//
39+
// Array.FindIndex(dinosaurs, EndsWithSaurus): 1
40+
//
41+
// Array.FindIndex(dinosaurs, 2, EndsWithSaurus): 5
42+
//
43+
// Array.FindIndex(dinosaurs, 2, 3, EndsWithSaurus): -1
44+
45+
// </Snippet1>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Compile Include="source.fs" />
10+
</ItemGroup>
11+
12+
</Project>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// <Snippet1>
2+
open System
3+
4+
// Search predicate returns true if a string ends in "saurus".
5+
let endsWithSaurus (s: string) =
6+
s.Length > 5 && s.Substring(s.Length - 6).ToLower() = "saurus"
7+
8+
let dinosaurs =
9+
[| "Compsognathus"; "Amargasaurus"
10+
"Oviraptor"; "Velociraptor"
11+
"Deinonychus"; "Dilophosaurus"
12+
"Gallimimus"; "Triceratops" |]
13+
14+
printfn ""
15+
for dino in dinosaurs do
16+
printfn $"{dino}"
17+
18+
Array.FindLastIndex(dinosaurs, endsWithSaurus)
19+
|> printfn "\nArray.FindLastIndex(dinosaurs, EndsWithSaurus): %i"
20+
21+
Array.FindLastIndex(dinosaurs, 4, endsWithSaurus)
22+
|> printfn "\nArray.FindLastIndex(dinosaurs, 4, EndsWithSaurus): %i"
23+
24+
Array.FindLastIndex(dinosaurs, 4, 3, endsWithSaurus)
25+
|> printfn "\nArray.FindLastIndex(dinosaurs, 4, 3, EndsWithSaurus): %i"
26+
27+
28+
// This code example produces the following output:
29+
//
30+
// Compsognathus
31+
// Amargasaurus
32+
// Oviraptor
33+
// Velociraptor
34+
// Deinonychus
35+
// Dilophosaurus
36+
// Gallimimus
37+
// Triceratops
38+
//
39+
// Array.FindLastIndex(dinosaurs, EndsWithSaurus): 5
40+
//
41+
// Array.FindLastIndex(dinosaurs, 4, EndsWithSaurus): 1
42+
//
43+
// Array.FindLastIndex(dinosaurs, 4, 3, EndsWithSaurus): -1
44+
45+
// </Snippet1>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Compile Include="source.fs" />
10+
</ItemGroup>
11+
12+
</Project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// <Snippet1>
2+
open System
3+
4+
let dinosaurs =
5+
[| "Tyrannosaurus"
6+
"Amargasaurus"
7+
"Mamenchisaurus"
8+
"Brachiosaurus"
9+
"Deinonychus"
10+
"Tyrannosaurus"
11+
"Compsognathus" |]
12+
13+
printfn ""
14+
for dino in dinosaurs do
15+
printfn $"{dino}"
16+
17+
Array.IndexOf(dinosaurs, "Tyrannosaurus")
18+
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): %i"
19+
20+
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3)
21+
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): %i"
22+
23+
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2)
24+
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): %i"
25+
26+
// This code example produces the following output:
27+
//
28+
// Tyrannosaurus
29+
// Amargasaurus
30+
// Mamenchisaurus
31+
// Brachiosaurus
32+
// Deinonychus
33+
// Tyrannosaurus
34+
// Compsognathus
35+
//
36+
// Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
37+
//
38+
// Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
39+
//
40+
// Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
41+
42+
// </Snippet1>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Compile Include="source.fs" />
10+
</ItemGroup>
11+
12+
</Project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// <Snippet1>
2+
open System
3+
4+
let dinosaurs =
5+
[| "Tyrannosaurus"
6+
"Amargasaurus"
7+
"Mamenchisaurus"
8+
"Brachiosaurus"
9+
"Deinonychus"
10+
"Tyrannosaurus"
11+
"Compsognathus" |]
12+
13+
printfn ""
14+
for dino in dinosaurs do
15+
printfn $"{dino}"
16+
17+
Array.LastIndexOf(dinosaurs, "Tyrannosaurus")
18+
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): %i"
19+
20+
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3)
21+
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): %i"
22+
23+
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4)
24+
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): %i"
25+
26+
// This code example produces the following output:
27+
//
28+
// Tyrannosaurus
29+
// Amargasaurus
30+
// Mamenchisaurus
31+
// Brachiosaurus
32+
// Deinonychus
33+
// Tyrannosaurus
34+
// Compsognathus
35+
//
36+
// Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
37+
//
38+
// Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
39+
//
40+
// Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
41+
42+
// </Snippet1>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Compile Include="source.fs" />
10+
</ItemGroup>
11+
12+
</Project>

0 commit comments

Comments
 (0)