From 37dbeb68875e8e71c7ca1ad20d886407587eaac1 Mon Sep 17 00:00:00 2001 From: Bartosz Klonowski Date: Wed, 20 Nov 2024 21:50:18 +0100 Subject: [PATCH 1/8] Mention the alternate notation of lambda expression in Syntax --- .../functions/lambda-expressions-the-fun-keyword.md | 8 ++++++++ 1 file changed, 8 insertions(+) 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..f43df0f155a15 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,14 @@ 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 +_. +``` + +where `fun`, *parameter-list* and lambda arrow (`->`) is omitted and the `_.` is a part of *expression* where `_` replaces the parameter call. + ## 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. From 06ebe8702c61f143278b2a72532d38a543cb6c47 Mon Sep 17 00:00:00 2001 From: Bartosz Klonowski Date: Wed, 20 Nov 2024 22:38:36 +0100 Subject: [PATCH 2/8] Modify lambda usage examples to show both notations --- .../functions/lambda-expressions-the-fun-keyword.md | 4 +++- samples/snippets/fsharp/lang-ref-1/snippet302.fs | 7 +++++-- 2 files changed, 8 insertions(+), 3 deletions(-) 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 f43df0f155a15..12298b8e87c5c 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 @@ -49,10 +49,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 a text ending with specified characters. [!code-fsharp[Main](~/samples/snippets/fsharp/lang-ref-1/snippet302.fs)] +The above 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" From 29b04e4b9d3a15bb00970960a4abba98f2456270 Mon Sep 17 00:00:00 2001 From: Bartosz Klonowski <70535775+BartoszKlonowski@users.noreply.github.com> Date: Fri, 22 Nov 2024 14:42:12 +0100 Subject: [PATCH 3/8] Emphasize on the snippets being equivalent Co-authored-by: Tomas Grosup --- .../functions/lambda-expressions-the-fun-keyword.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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 12298b8e87c5c..07af7256e23ed 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 @@ -19,7 +19,12 @@ or using the _.Property shorthand notation: _. ``` -where `fun`, *parameter-list* and lambda arrow (`->`) is omitted and the `_.` is a part of *expression* where `_` replaces the parameter call. +where `fun`, *parameter-list* and lambda arrow (`->`) is omitted and the `_.` is a part of *expression* where `_` replaces the parameter symbol. + +The following snippets are equivalent: +`(fun x -> x.Property)` +`_.Property` + ## Remarks From ada7506bb1e4df63fdd36e338366346924d774bc Mon Sep 17 00:00:00 2001 From: Vlad Zarytovskii Date: Fri, 22 Nov 2024 20:38:48 +0100 Subject: [PATCH 4/8] Update lambda-expressions-the-fun-keyword.md Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --- .../functions/lambda-expressions-the-fun-keyword.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 07af7256e23ed..22fee6cead65d 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 @@ -22,10 +22,10 @@ _. where `fun`, *parameter-list* and lambda arrow (`->`) is 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. From f67c77749757cc456bcc276557e3feee70630c76 Mon Sep 17 00:00:00 2001 From: Vlad Zarytovskii Date: Fri, 22 Nov 2024 20:38:54 +0100 Subject: [PATCH 5/8] Update lambda-expressions-the-fun-keyword.md Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --- .../functions/lambda-expressions-the-fun-keyword.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 22fee6cead65d..7b5c7491582d4 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 @@ -58,7 +58,7 @@ Lambda expressions are especially useful when you want to perform operations on [!code-fsharp[Main](~/samples/snippets/fsharp/lang-ref-1/snippet302.fs)] -The above shows both notations: using the `fun` keyword, and the shorthand `_.Property` notation. +The previous code snippet shows both notations: using the `fun` keyword, and the shorthand `_.Property` notation. ## See also From 2a1468647c613cb2819e2f52388ca933939956cf Mon Sep 17 00:00:00 2001 From: Vlad Zarytovskii Date: Fri, 22 Nov 2024 20:39:05 +0100 Subject: [PATCH 6/8] Update lambda-expressions-the-fun-keyword.md Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --- .../functions/lambda-expressions-the-fun-keyword.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 7b5c7491582d4..4d1f4d42d5316 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,7 +13,7 @@ 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: +Or using the `_.Property` shorthand notation: ```fsharp _. From 8f45cda5b7e3ebceba5e806c120c8caf74ef9ccc Mon Sep 17 00:00:00 2001 From: Vlad Zarytovskii Date: Fri, 22 Nov 2024 20:39:30 +0100 Subject: [PATCH 7/8] Update lambda-expressions-the-fun-keyword.md Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --- .../functions/lambda-expressions-the-fun-keyword.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 4d1f4d42d5316..9475712215160 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 @@ -19,7 +19,7 @@ Or using the `_.Property` shorthand notation: _. ``` -where `fun`, *parameter-list* and lambda arrow (`->`) is omitted and the `_.` is a part of *expression* where `_` replaces the parameter symbol. +`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: From 8df533a8366e7aae35987df0460a683331a51a3d Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 25 Nov 2024 09:53:25 -0500 Subject: [PATCH 8/8] Update docs/fsharp/language-reference/functions/lambda-expressions-the-fun-keyword.md --- .../functions/lambda-expressions-the-fun-keyword.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 9475712215160..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 @@ -54,7 +54,7 @@ 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 checks if an element is a text ending with specified characters. +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)]