Skip to content

Commit 2192711

Browse files
BartoszKlonowskiT-GrovzarytovskiigewarrenBillWagner
authored
Include lambda shorthand notation in "Lambda Expressions: The fun Keyword" page (#43675)
* Mention the alternate notation of lambda expression in Syntax * Modify lambda usage examples to show both notations * Emphasize on the snippets being equivalent Co-authored-by: Tomas Grosup <[email protected]> * Update lambda-expressions-the-fun-keyword.md Co-authored-by: Genevieve Warren <[email protected]> * Update lambda-expressions-the-fun-keyword.md Co-authored-by: Genevieve Warren <[email protected]> * Update lambda-expressions-the-fun-keyword.md Co-authored-by: Genevieve Warren <[email protected]> * Update lambda-expressions-the-fun-keyword.md Co-authored-by: Genevieve Warren <[email protected]> * Update docs/fsharp/language-reference/functions/lambda-expressions-the-fun-keyword.md --------- Co-authored-by: Tomas Grosup <[email protected]> Co-authored-by: Vlad Zarytovskii <[email protected]> Co-authored-by: Genevieve Warren <[email protected]> Co-authored-by: Bill Wagner <[email protected]>
1 parent b440e46 commit 2192711

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

docs/fsharp/language-reference/functions/lambda-expressions-the-fun-keyword.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ The `fun` keyword is used to define a lambda expression, that is, an anonymous f
1313
fun parameter-list -> expression
1414
```
1515

16+
Or using the `_.Property` shorthand notation:
17+
18+
```fsharp
19+
_.
20+
```
21+
22+
`fun`, *parameter-list*, and lambda arrow (`->`) are omitted, and the `_.` is a part of *expression* where `_` replaces the parameter symbol.
23+
24+
The following snippets are equivalent:
25+
26+
`(fun x -> x.Property)`
27+
`_.Property`
28+
1629
## Remarks
1730

1831
The *parameter-list* typically consists of names and, optionally, types of parameters. More generally, the *parameter-list* can be composed of any F# patterns. For a full list of possible patterns, see [Pattern Matching](../pattern-matching.md). Lists of valid parameters include the following examples.
@@ -41,10 +54,12 @@ The *expression* is the body of the function, the last expression of which gener
4154

4255
## Using Lambda Expressions
4356

44-
Lambda expressions are especially useful when you want to perform operations on a list or other collection and want to avoid the extra work of defining a function. Many F# library functions take function values as arguments, and it can be especially convenient to use a lambda expression in those cases. The following code applies a lambda expression to elements of a list. In this case, the anonymous function adds 1 to every element of a list.
57+
Lambda expressions are especially useful when you want to perform operations on a list or other collection and want to avoid the extra work of defining a function. Many F# library functions take function values as arguments, and it can be especially convenient to use a lambda expression in those cases. The following code applies a lambda expression to elements of a list. In this case, the anonymous function checks if an element is text that ends with specified characters.
4558

4659
[!code-fsharp[Main](~/samples/snippets/fsharp/lang-ref-1/snippet302.fs)]
4760

61+
The previous code snippet shows both notations: using the `fun` keyword, and the shorthand `_.Property` notation.
62+
4863
## See also
4964

5065
- [Functions](index.md)
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
let list = List.map (fun i -> i + 1) [ 1; 2; 3 ]
2-
printfn "%A" list
1+
let fullNotation = [ "a"; "ab"; "abc" ] |> List.find ( fun text -> text.EndsWith("c") )
2+
printfn "%A" fullNotation // Output: "abc"
3+
4+
let shorthandNotation = [ "a"; "ab"; "abc" ] |> List.find ( _.EndsWith("b") )
5+
printfn "%A" shorthandNotation // Output: "ab"

0 commit comments

Comments
 (0)