Thanks to visit codestin.com
Credit goes to github.com

Skip to content

System.Predicate<T> F# snippets #7822

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
merged 3 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions snippets/fsharp/System/PredicateT/Overview/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="predicate1.fs" />
<Compile Include="predicateex2.fs" />
<Compile Include="predicateex1.fs" />
</ItemGroup>
</Project>
29 changes: 29 additions & 0 deletions snippets/fsharp/System/PredicateT/Overview/predicate1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module predicate1

// <Snippet3>
open System

type HockeyTeam =
{ Name: string
Founded: int }

let rnd = Random()
let teams = ResizeArray()
teams.AddRange
[| { Name = "Detroit Red Wings"; Founded = 1926 }
{ Name = "Chicago Blackhawks"; Founded = 1926 }
{ Name = "San Jose Sharks"; Founded = 1991 }
{ Name = "Montreal Canadiens"; Founded = 1909 }
{ Name = "St. Louis Blues"; Founded = 1967 }|]

let years = [| 1920; 1930; 1980; 2000 |]
let foundedBeforeYear = years[rnd.Next(0, years.Length)]
printfn $"Teams founded before {foundedBeforeYear}:"
for team in teams.FindAll(fun x -> x.Founded <= foundedBeforeYear) do
printfn $"{team.Name}: {team.Founded}"
// The example displays output similar to the following:
// Teams founded before 1930:
// Detroit Red Wings: 1926
// Chicago Blackhawks: 1926
// Montreal Canadiens: 1909
// </Snippet3>
23 changes: 23 additions & 0 deletions snippets/fsharp/System/PredicateT/Overview/predicateex1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module predicateex1

// <Snippet2>
open System
open System.Drawing

// Create an array of Point structures.
let points =
[| Point(100, 200)
Point(150, 250)
Point(250, 375)
Point(275, 395)
Point(295, 450) |]

// Find the first Point structure for which X times Y
// is greater than 100000.
let first = Array.Find(points, fun x -> x.X * x.Y > 100000)

// Display the first structure found.
printfn $"Found: X = {first.X}, Y = {first.Y}"
// The example displays the following output:
// Found: X = 275, Y = 395
// </Snippet2>
29 changes: 29 additions & 0 deletions snippets/fsharp/System/PredicateT/Overview/predicateex2.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module predicateex2

// <Snippet4>
open System
open System.Drawing

let findPoints (obj: Point) =
obj.X * obj.Y > 100000

// Create an array of Point structures.
let points =
[| Point(100, 200)
Point(150, 250)
Point(250, 375)
Point(275, 395)
Point(295, 450) |]

// Define the Predicate<T> delegate.
let predicate = Predicate<Point> findPoints

// Find the first Point structure for which X times Y
// is greater than 100000.
let first = Array.Find(points, predicate)

// Display the first structure found.
printfn $"Found: X = {first.X}, Y = {first.Y}"
// The example displays the following output:
// Found: X = 275, Y = 395
// </Snippet4>
3 changes: 3 additions & 0 deletions xml/System/Predicate`1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
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.

:::code language="csharp" source="~/snippets/csharp/System/PredicateT/Overview/predicate1.cs" interactive="try-dotnet" id="Snippet3":::
:::code language="fsharp" source="~/snippets/fsharp/System/PredicateT/Overview/predicate1.fs" id="Snippet3":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Predicate`1/vb/predicate1.vb" id="Snippet3":::


Expand All @@ -77,11 +78,13 @@
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.

:::code language="csharp" source="~/snippets/csharp/System/PredicateT/Overview/predicateex2.cs" interactive="try-dotnet" id="Snippet4":::
:::code language="fsharp" source="~/snippets/fsharp/System/PredicateT/Overview/predicateex2.fs" id="Snippet4":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Predicate`1/vb/predicateex2.vb" id="Snippet4":::

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.

:::code language="csharp" source="~/snippets/csharp/System/PredicateT/Overview/predicateex1.cs" interactive="try-dotnet" id="Snippet2":::
:::code language="fsharp" source="~/snippets/fsharp/System/PredicateT/Overview/predicateex1.fs" id="Snippet2":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Predicate`1/vb/predicateex1.vb" id="Snippet2":::

]]></format>
Expand Down