Skip to content

Commit a8dce7f

Browse files
authored
System.Predicate<T> F# snippets (#7822)
* Predicate<T> F# snippets * Predicate<T> F# snippet * Revert "Predicate<T> F# snippet" This reverts commit 386deb6.
1 parent b023646 commit a8dce7f

File tree

5 files changed

+96
-0
lines changed

5 files changed

+96
-0
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+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="predicate1.fs" />
9+
<Compile Include="predicateex2.fs" />
10+
<Compile Include="predicateex1.fs" />
11+
</ItemGroup>
12+
</Project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module predicate1
2+
3+
// <Snippet3>
4+
open System
5+
6+
type HockeyTeam =
7+
{ Name: string
8+
Founded: int }
9+
10+
let rnd = Random()
11+
let teams = ResizeArray()
12+
teams.AddRange
13+
[| { Name = "Detroit Red Wings"; Founded = 1926 }
14+
{ Name = "Chicago Blackhawks"; Founded = 1926 }
15+
{ Name = "San Jose Sharks"; Founded = 1991 }
16+
{ Name = "Montreal Canadiens"; Founded = 1909 }
17+
{ Name = "St. Louis Blues"; Founded = 1967 }|]
18+
19+
let years = [| 1920; 1930; 1980; 2000 |]
20+
let foundedBeforeYear = years[rnd.Next(0, years.Length)]
21+
printfn $"Teams founded before {foundedBeforeYear}:"
22+
for team in teams.FindAll(fun x -> x.Founded <= foundedBeforeYear) do
23+
printfn $"{team.Name}: {team.Founded}"
24+
// The example displays output similar to the following:
25+
// Teams founded before 1930:
26+
// Detroit Red Wings: 1926
27+
// Chicago Blackhawks: 1926
28+
// Montreal Canadiens: 1909
29+
// </Snippet3>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module predicateex1
2+
3+
// <Snippet2>
4+
open System
5+
open System.Drawing
6+
7+
// Create an array of Point structures.
8+
let points =
9+
[| Point(100, 200)
10+
Point(150, 250)
11+
Point(250, 375)
12+
Point(275, 395)
13+
Point(295, 450) |]
14+
15+
// Find the first Point structure for which X times Y
16+
// is greater than 100000.
17+
let first = Array.Find(points, fun x -> x.X * x.Y > 100000)
18+
19+
// Display the first structure found.
20+
printfn $"Found: X = {first.X}, Y = {first.Y}"
21+
// The example displays the following output:
22+
// Found: X = 275, Y = 395
23+
// </Snippet2>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module predicateex2
2+
3+
// <Snippet4>
4+
open System
5+
open System.Drawing
6+
7+
let findPoints (obj: Point) =
8+
obj.X * obj.Y > 100000
9+
10+
// Create an array of Point structures.
11+
let points =
12+
[| Point(100, 200)
13+
Point(150, 250)
14+
Point(250, 375)
15+
Point(275, 395)
16+
Point(295, 450) |]
17+
18+
// Define the Predicate<T> delegate.
19+
let predicate = Predicate<Point> findPoints
20+
21+
// Find the first Point structure for which X times Y
22+
// is greater than 100000.
23+
let first = Array.Find(points, predicate)
24+
25+
// Display the first structure found.
26+
printfn $"Found: X = {first.X}, Y = {first.Y}"
27+
// The example displays the following output:
28+
// Found: X = 275, Y = 395
29+
// </Snippet4>

xml/System/Predicate`1.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
Typically, the <xref:System.Predicate%601> delegate is represented by a lambda expression. Because locally scoped variables are available to the lambda expression, it is easy to test for a condition that is not precisely known at compile time. This is simulated in the following example, which defines a `HockeyTeam` class that contains information about a National Hockey League team and the year in which it was founded. The example defines an array of integer values that represent years, and randomly assigns one element of the array to `foundedBeforeYear`, which is a variable that is locally scoped to the example's `Main` method. Because locally scoped variables are available to a lambda expression, the lambda expression passed to the <xref:System.Collections.Generic.List%601.FindAll%2A?displayProperty=nameWithType> method is able to return a `HockeyTeam` object for each team founded on or before that year.
7070
7171
:::code language="csharp" source="~/snippets/csharp/System/PredicateT/Overview/predicate1.cs" interactive="try-dotnet" id="Snippet3":::
72+
:::code language="fsharp" source="~/snippets/fsharp/System/PredicateT/Overview/predicate1.fs" id="Snippet3":::
7273
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Predicate`1/vb/predicate1.vb" id="Snippet3":::
7374
7475
@@ -77,11 +78,13 @@
7778
The following code example uses a <xref:System.Predicate%601> delegate with the <xref:System.Array.Find%2A?displayProperty=nameWithType> method to search an array of <xref:System.Drawing.Point> structures. The example explicitly defines a <xref:System.Predicate%601> delegate named `predicate` and assigns it a method named `FindPoints` that returns `true` if the product of the <xref:System.Drawing.Point.X%2A?displayProperty=nameWithType> and <xref:System.Drawing.Point.Y%2A?displayProperty=nameWithType> fields is greater than 100,000. Note that it is customary to use a lambda expression rather than to explicitly define a delegate of type <xref:System.Predicate%601>, as the second example illustrates.
7879
7980
:::code language="csharp" source="~/snippets/csharp/System/PredicateT/Overview/predicateex2.cs" interactive="try-dotnet" id="Snippet4":::
81+
:::code language="fsharp" source="~/snippets/fsharp/System/PredicateT/Overview/predicateex2.fs" id="Snippet4":::
8082
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Predicate`1/vb/predicateex2.vb" id="Snippet4":::
8183
8284
The following example is identical to the previous example, except that it uses a lambda expression to represent the <xref:System.Predicate%601> delegate. Each element of the `points` array is passed to the lambda expression until the expression finds an element that meets the search criteria. In this case, the lambda expression returns `true` if the product of the X and Y fields is greater than 100,000.
8385
8486
:::code language="csharp" source="~/snippets/csharp/System/PredicateT/Overview/predicateex1.cs" interactive="try-dotnet" id="Snippet2":::
87+
:::code language="fsharp" source="~/snippets/fsharp/System/PredicateT/Overview/predicateex1.fs" id="Snippet2":::
8588
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Predicate`1/vb/predicateex1.vb" id="Snippet2":::
8689
8790
]]></format>

0 commit comments

Comments
 (0)