diff --git a/docs/fsharp/language-reference/functions/lambda-expressions-the-fun-keyword.md b/docs/fsharp/language-reference/functions/lambda-expressions-the-fun-keyword.md index aa1e905efb595..a0be5d78bd526 100644 --- a/docs/fsharp/language-reference/functions/lambda-expressions-the-fun-keyword.md +++ b/docs/fsharp/language-reference/functions/lambda-expressions-the-fun-keyword.md @@ -13,6 +13,19 @@ The `fun` keyword is used to define a lambda expression, that is, an anonymous f fun parameter-list -> expression ``` +Or using the `_.Property` shorthand notation: + +```fsharp +_. +``` + +`fun`, *parameter-list*, and lambda arrow (`->`) are omitted, and the `_.` is a part of *expression* where `_` replaces the parameter symbol. + +The following snippets are equivalent: + +`(fun x -> x.Property)` +`_.Property` + ## Remarks 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 ## Using Lambda Expressions -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. +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. [!code-fsharp[Main](~/samples/snippets/fsharp/lang-ref-1/snippet302.fs)] +The previous code snippet shows both notations: using the `fun` keyword, and the shorthand `_.Property` notation. + ## See also - [Functions](index.md) diff --git a/samples/snippets/fsharp/lang-ref-1/snippet302.fs b/samples/snippets/fsharp/lang-ref-1/snippet302.fs index 0cb6fc1b946f8..747683ce70e98 100644 --- a/samples/snippets/fsharp/lang-ref-1/snippet302.fs +++ b/samples/snippets/fsharp/lang-ref-1/snippet302.fs @@ -1,2 +1,5 @@ -let list = List.map (fun i -> i + 1) [ 1; 2; 3 ] -printfn "%A" list +let fullNotation = [ "a"; "ab"; "abc" ] |> List.find ( fun text -> text.EndsWith("c") ) +printfn "%A" fullNotation // Output: "abc" + +let shorthandNotation = [ "a"; "ab"; "abc" ] |> List.find ( _.EndsWith("b") ) +printfn "%A" shorthandNotation // Output: "ab"