Skip to content

Commit b291cea

Browse files
authored
Func<T1,T2,TResult> F# snippets (#7615)
1 parent a884c01 commit b291cea

File tree

6 files changed

+94
-2
lines changed

6 files changed

+94
-2
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Delegate
2+
3+
// <Snippet1>
4+
type ExtractMethod = delegate of string * int -> string []
5+
6+
let extractWords (phrase: string) limit =
7+
let delimiters = [| ' ' |]
8+
if limit > 0 then
9+
phrase.Split(delimiters, limit)
10+
else
11+
phrase.Split delimiters
12+
13+
// Instantiate delegate to reference extractWords function
14+
let extractMeth = ExtractMethod extractWords
15+
let title = "The Scarlet Letter"
16+
17+
// Use delegate instance to call extractWords function and display result
18+
for word in extractMeth.Invoke(title, 5) do
19+
printfn $"{word}"
20+
// </Snippet1>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Example
2+
3+
// <Snippet5>
4+
open System
5+
open System.Linq
6+
7+
let predicate = Func<string, int, bool>(fun str index -> str.Length = index)
8+
9+
let words = [ "orange"; "apple"; "Article"; "elephant"; "star"; "and" ]
10+
let aWords = words.Where predicate
11+
12+
for word in aWords do
13+
printfn $"{word}"
14+
// </Snippet5>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Func3
2+
3+
// <Snippet2>
4+
open System
5+
6+
let extractWords (phrase: string) limit =
7+
let delimiters = [| ' ' |]
8+
if limit > 0 then
9+
phrase.Split(delimiters, limit)
10+
else
11+
phrase.Split delimiters
12+
13+
// Instantiate delegate to reference extractWords function
14+
let extractMethod = Func<string, int, string[]> extractWords
15+
let title = "The Scarlet Letter"
16+
17+
// Use delegate instance to call extractWords function and display result
18+
for word in extractMethod.Invoke(title, 5) do
19+
printfn $"{word}"
20+
// </Snippet2>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Lambda
2+
3+
// <Snippet4>
4+
open System
5+
6+
let separators = [| ' ' |]
7+
8+
let extract =
9+
Func<string, int, string []> (fun s i ->
10+
if i > 0 then
11+
s.Split(separators, i)
12+
else
13+
s.Split separators)
14+
15+
let title = "The Scarlet Letter"
16+
17+
// Use Func instance to call lambda expression and display result
18+
for word in extract.Invoke(title, 5) do
19+
printfn $"{word}"
20+
// </Snippet4>
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+
<ItemGroup>
7+
<Compile Include="Delegate.fs" />
8+
<Compile Include="Func3.fs" />
9+
<Compile Include="Lambda.fs" />
10+
<Compile Include="Example.fs" />
11+
</ItemGroup>
12+
</Project>

xml/System/Func`3.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,25 +94,28 @@
9494
You can use this delegate to represent a method that can be passed as a parameter without explicitly declaring a custom delegate. The encapsulated method must correspond to the method signature that is defined by this delegate. This means that the encapsulated method must have two parameters, each of which is passed to it by value, and that it must return a value.
9595
9696
> [!NOTE]
97-
> To reference a method that has two parameters and returns `void` (or in Visual Basic, that is declared as a `Sub` rather than as a `Function`), use the generic <xref:System.Action%602> delegate instead.
97+
> To reference a method that has two parameters and returns `void` (`unit` in F#) (or in Visual Basic, that is declared as a `Sub` rather than as a `Function`), use the generic <xref:System.Action%602> delegate instead.
9898
9999
When you use the <xref:System.Func%603> delegate you do not have to explicitly define a delegate that encapsulates a method with two parameters. For example, the following code explicitly declares a delegate named `ExtractMethod` and assigns a reference to the `ExtractWords` method to its delegate instance.
100100
101101
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~3/cs/Delegate.cs" interactive="try-dotnet" id="Snippet1":::
102+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~3/fs/Delegate.fs" id="Snippet1":::
102103
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~3/vb/Delegate.vb" id="Snippet1":::
103104
104105
The following example simplifies this code by instantiating a <xref:System.Func%603> delegate instead of explicitly defining a new delegate and assigning a named method to it.
105106
106107
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~3/cs/Func3.cs" interactive="try-dotnet" id="Snippet2":::
108+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~3/fs/Func3.fs" id="Snippet2":::
107109
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~3/vb/Func3.vb" id="Snippet2":::
108110
109111
You can use the <xref:System.Func%603> delegate with anonymous methods in C#, as the following example illustrates. (For an introduction to anonymous methods, see [Anonymous Methods](/dotnet/csharp/programming-guide/statements-expressions-operators/anonymous-methods).)
110112
111113
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~3/cs/Anon.cs" interactive="try-dotnet" id="Snippet3":::
112114
113-
You can also assign a lambda expression to a <xref:System.Func%603> delegate, as the following example illustrates. (For an introduction to lambda expressions, see [Lambda Expressions](/dotnet/visual-basic/programming-guide/language-features/procedures/lambda-expressions) and [Lambda Expressions](/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions).)
115+
You can also assign a lambda expression to a <xref:System.Func%603> delegate, as the following example illustrates. (For an introduction to lambda expressions, see [Lambda Expressions (VB)](/dotnet/visual-basic/programming-guide/language-features/procedures/lambda-expressions), [Lambda Expressions (C#)](/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions) and [Lambda Expressions (F#)](/dotnet/fsharp/language-reference/functions/lambda-expressions-the-fun-keyword).)
114116
115117
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~3/cs/Lambda.cs" interactive="try-dotnet" id="Snippet4":::
118+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~3/fs/Lambda.fs" id="Snippet4":::
116119
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~3/vb/lambda.vb" id="Snippet4":::
117120
118121
The underlying type of a lambda expression is one of the generic `Func` delegates. This makes it possible to pass a lambda expression as a parameter without explicitly assigning it to a delegate. In particular, because many methods of types in the <xref:System.Linq> namespace have <xref:System.Func%603> parameters, you can pass these methods a lambda expression without explicitly instantiating a <xref:System.Func%603> delegate.
@@ -123,13 +126,16 @@
123126
The following example demonstrates how to declare and use a <xref:System.Func%603> delegate. This example declares a <xref:System.Func%603> variable and assigns it a lambda expression that takes a <xref:System.String> value and an <xref:System.Int32> value as parameters. The lambda expression returns `true` if the length of the <xref:System.String> parameter is equal to the value of the <xref:System.Int32> parameter. The delegate that encapsulates this method is subsequently used in a query to filter strings in an array of strings.
124127
125128
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~3/cs/Example.cs" interactive="try-dotnet" id="Snippet5":::
129+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~3/fs/Example.fs" id="Snippet5":::
126130
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~3/vb/Example.vb" id="Snippet5":::
127131
128132
]]></format>
129133
</remarks>
130134
<related type="Article" href="/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions">Lambda Expressions (C# Programming Guide)</related>
135+
<related type="Article" href="/dotnet/fsharp/language-reference/functions/lambda-expressions-the-fun-keyword">Lambda Expressions: The fun Keyword (F#)</related>
131136
<related type="Article" href="/dotnet/visual-basic/programming-guide/language-features/procedures/lambda-expressions">Lambda Expressions</related>
132137
<related type="Article" href="/dotnet/csharp/programming-guide/delegates/">Delegates (C# Programming Guide)</related>
138+
<related type="Article" href="/dotnet/fsharp/language-reference/delegates/">Delegates (F#)</related>
133139
<related type="Article" href="/dotnet/visual-basic/programming-guide/language-features/delegates/">Delegates in Visual Basic</related>
134140
</Docs>
135141
</Type>

0 commit comments

Comments
 (0)