From 0d123038ce1954c8310e77d7a85907bbaa8d7dc2 Mon Sep 17 00:00:00 2001 From: albert-du <52804499+albert-du@users.noreply.github.com> Date: Wed, 3 Nov 2021 20:20:07 -0700 Subject: [PATCH 1/3] Add System.Action F# snippets --- .../system.Action_PrintExample/fs/action.fs | 30 +++++++++++++++++++ .../fs/action.fsproj | 12 ++++++++ .../system.Action~1/fs/Action1.fs | 20 +++++++++++++ .../system.Action~1/fs/Action1.fsproj | 16 ++++++++++ .../system.Action~1/fs/Delegate.fs | 22 ++++++++++++++ .../system.Action~1/fs/Lambda.fs | 20 +++++++++++++ xml/System/Action`1.xml | 4 +++ 7 files changed, 124 insertions(+) create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action_PrintExample/fs/action.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action_PrintExample/fs/action.fsproj create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Action1.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Action1.fsproj create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Delegate.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Lambda.fs diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action_PrintExample/fs/action.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action_PrintExample/fs/action.fs new file mode 100644 index 00000000000..1027e60caef --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action_PrintExample/fs/action.fs @@ -0,0 +1,30 @@ +open System + +// +// F# provides a type alias for System.Collections.List as ResizeArray. +let names = ResizeArray() +names.Add "Bruce" +names.Add "Alfred" +names.Add "Tim" +names.Add "Richard" + +let print s = printfn "%s" s + +// Display the contents of the list using the print function. +names.ForEach(Action print) + +// The following demonstrates the lambda expression feature of F# +// to display the contents of the list to the console. +names.ForEach(fun s -> printfn "%s" s) + +(* This code will produce output similar to the following: +* Bruce +* Alfred +* Tim +* Richard +* Bruce +* Alfred +* Tim +* Richard +*) +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action_PrintExample/fs/action.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action_PrintExample/fs/action.fsproj new file mode 100644 index 00000000000..678e8a172f3 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action_PrintExample/fs/action.fsproj @@ -0,0 +1,12 @@ + + + + Exe + net5.0 + + + + + + + diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Action1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Action1.fs new file mode 100644 index 00000000000..be9f5e90a42 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Action1.fs @@ -0,0 +1,20 @@ +module TestAction1 + +// +open System +open System.Windows.Forms + +let showWindowsMessage message = + MessageBox.Show message |> ignore + +let messageTarget = + Action( + if Environment.GetCommandLineArgs().Length > 1 then + showWindowsMessage + else + printfn "%s" + ) + +messageTarget.Invoke "Hello, World!" + +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Action1.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Action1.fsproj new file mode 100644 index 00000000000..03c098f7086 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Action1.fsproj @@ -0,0 +1,16 @@ + + + + Exe + net5.0-windows + true + true + + + + + + + + + diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Delegate.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Delegate.fs new file mode 100644 index 00000000000..1e8443c0f59 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Delegate.fs @@ -0,0 +1,22 @@ +module TestCustomDelegate + +// +open System +open System.Windows.Forms + +type DisplayMessage = delegate of message: string -> unit + +let showWindowsMessage message = + MessageBox.Show message |> ignore + +let messageTarget = + DisplayMessage( + if Environment.GetCommandLineArgs().Length > 1 then + showWindowsMessage + else + printfn "%s" + ) + +messageTarget.Invoke "Hello, World!" + +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Lambda.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Lambda.fs new file mode 100644 index 00000000000..96ee5314a41 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Lambda.fs @@ -0,0 +1,20 @@ +module TestLambdaExpression + +// +open System +open System.Windows.Forms + +let showWindowsMessage message = + MessageBox.Show message |> ignore + +let messageTarget = + Action( + if Environment.GetCommandLineArgs().Length > 1 then + fun s -> showWindowsMessage s + else + fun s -> printfn "%s" s + ) + +messageTarget.Invoke "Hello, World!" + +// diff --git a/xml/System/Action`1.xml b/xml/System/Action`1.xml index b6dd1282761..631be3166ba 100644 --- a/xml/System/Action`1.xml +++ b/xml/System/Action`1.xml @@ -69,12 +69,14 @@ :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Action~1/cpp/delegate.cpp" id="Snippet1"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action~1/cs/Delegate.cs" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Delegate.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~1/vb/Delegate.vb" id="Snippet1"::: The following example simplifies this code by instantiating the delegate instead of explicitly defining a new delegate and assigning a named method to it. :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Action~1/cpp/action`1.cpp" id="Snippet2"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action~1/cs/Action1.cs" id="Snippet2"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Action1.fs" id="Snippet2"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~1/vb/Action1.vb" id="Snippet2"::: You can also use the 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).) @@ -84,6 +86,7 @@ You can also assign a lambda expression to an delegate instance, as the following example illustrates. (For an introduction to lambda expressions, see [Lambda Expressions](/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions).) :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action~1/cs/Lambda.cs" id="Snippet4"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Lambda.fs" id="Snippet4"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~1/vb/lambda.vb" id="Snippet4"::: The and methods each take an delegate as a parameter. The method encapsulated by the delegate allows you to perform an action on each element in the array or list. The example uses the method to provide an illustration. @@ -94,6 +97,7 @@ The following example demonstrates the use of the delegate to print the contents of a object. In this example, the `Print` method is used to display the contents of the list to the console. In addition, the C# example also demonstrates the use of anonymous methods to display the contents to the console. Note that the example does not explicitly declare an variable. Instead, it passes a reference to a method that takes a single parameter and that does not return a value to the method, whose single parameter is an delegate. Similarly, in the C# example, an delegate is not explicitly instantiated because the signature of the anonymous method matches the signature of the delegate that is expected by the method. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action_PrintExample/cs/action.cs" interactive="try-dotnet-method" id="Snippet01"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action_PrintExample/fs/action.fs" id="Snippet01"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action_PrintExample/vb/action.vb" id="Snippet01"::: ]]> From d796096ca744541deafe263d5a1d7ffe47b96fc8 Mon Sep 17 00:00:00 2001 From: Albert Du <52804499+albert-du@users.noreply.github.com> Date: Thu, 11 Nov 2021 13:43:56 -0800 Subject: [PATCH 2/3] Update Lambda.fs --- .../VS_Snippets_CLR_System/system.Action~1/fs/Lambda.fs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Lambda.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Lambda.fs index 96ee5314a41..6989f4e5eda 100644 --- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Lambda.fs +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Lambda.fs @@ -1,6 +1,6 @@ module TestLambdaExpression -// +// open System open System.Windows.Forms @@ -17,4 +17,4 @@ let messageTarget = messageTarget.Invoke "Hello, World!" -// +// From c0bcb80f57976748081ee7c021222e9665847917 Mon Sep 17 00:00:00 2001 From: albert-du <52804499+albert-du@users.noreply.github.com> Date: Thu, 11 Nov 2021 14:22:09 -0800 Subject: [PATCH 3/3] Update action.fs --- .../system.Action_PrintExample/fs/action.fs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action_PrintExample/fs/action.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action_PrintExample/fs/action.fs index 1027e60caef..c155903b5b5 100644 --- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action_PrintExample/fs/action.fs +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action_PrintExample/fs/action.fs @@ -1,7 +1,7 @@ open System // -// F# provides a type alias for System.Collections.List as ResizeArray. +// F# provides a type alias for System.Collections.List<'T> as ResizeArray<'T>. let names = ResizeArray() names.Add "Bruce" names.Add "Alfred"