From 8a361433959ad60a9c96beb7a44d754c4022c097 Mon Sep 17 00:00:00 2001 From: Tenacom developer Date: Sun, 25 Jun 2023 04:54:05 +0200 Subject: [PATCH 1/2] Add Invoke overloads (closes #51) --- CHANGELOG.md | 2 + src/Louis/Fluency/FluentExtensions-Invoke.cs | 104 +++++++++++++++++++ src/Louis/PublicAPI.Unshipped.txt | 4 + 3 files changed, 110 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a8765e3..bd12742 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### New features +- Added overloads of `Louis.Fluency.FluentExtensions.Invoke` that allow for additional arguments to be passed to lambdas to avoid the allocation of closure objects. + ### Changes to existing features ### Bugs fixed in this release diff --git a/src/Louis/Fluency/FluentExtensions-Invoke.cs b/src/Louis/Fluency/FluentExtensions-Invoke.cs index d6730c8..f33b727 100644 --- a/src/Louis/Fluency/FluentExtensions-Invoke.cs +++ b/src/Louis/Fluency/FluentExtensions-Invoke.cs @@ -28,4 +28,108 @@ public static T Invoke(this T @this, Action action) action(@this); return @this; } + + /// + /// Invokes an action on an object and an additional argument and returns the same object. + /// + /// The type of the object. + /// The type of the additional parameter to . + /// The object on which this method was called. + /// The additional argument to pass tp . + /// The action to perform on and . + /// A reference to after returns. + /// + /// is . + /// - or - + /// is . + /// + public static T Invoke(this T @this, TArg arg, Action action) + { + Guard.IsNotNull(@this); + Guard.IsNotNull(action); + + action(@this, arg); + return @this; + } + + /// + /// Invokes an action on an object and additional arguments and returns the same object. + /// + /// The type of the object. + /// The type of the first additional parameter to . + /// The type of the second additional parameter to . + /// The object on which this method was called. + /// The First additional argument to pass tp . + /// The second additional argument to pass tp . + /// The action to perform on and the additional arguments. + /// A reference to after returns. + /// + /// is . + /// - or - + /// is . + /// + public static T Invoke(this T @this, TArg1 arg1, TArg2 arg2, Action action) + { + Guard.IsNotNull(@this); + Guard.IsNotNull(action); + + action(@this, arg1, arg2); + return @this; + } + + /// + /// Invokes an action on an object and additional arguments and returns the same object. + /// + /// The type of the object. + /// The type of the first additional parameter to . + /// The type of the second additional parameter to . + /// The type of the third additional parameter to . + /// The object on which this method was called. + /// The First additional argument to pass tp . + /// The second additional argument to pass tp . + /// The third additional argument to pass tp . + /// The action to perform on and the additional arguments. + /// A reference to after returns. + /// + /// is . + /// - or - + /// is . + /// + public static T Invoke(this T @this, TArg1 arg1, TArg2 arg2, TArg3 arg3, Action action) + { + Guard.IsNotNull(@this); + Guard.IsNotNull(action); + + action(@this, arg1, arg2, arg3); + return @this; + } + + /// + /// Invokes an action on an object and additional arguments and returns the same object. + /// + /// The type of the object. + /// The type of the first additional parameter to . + /// The type of the second additional parameter to . + /// The type of the third additional parameter to . + /// The type of the fourth additional parameter to . + /// The object on which this method was called. + /// The First additional argument to pass tp . + /// The second additional argument to pass tp . + /// The third additional argument to pass tp . + /// The fourth additional argument to pass tp . + /// The action to perform on and the additional arguments. + /// A reference to after returns. + /// + /// is . + /// - or - + /// is . + /// + public static T Invoke(this T @this, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, Action action) + { + Guard.IsNotNull(@this); + Guard.IsNotNull(action); + + action(@this, arg1, arg2, arg3, arg4); + return @this; + } } diff --git a/src/Louis/PublicAPI.Unshipped.txt b/src/Louis/PublicAPI.Unshipped.txt index 5de894d..d16ca48 100644 --- a/src/Louis/PublicAPI.Unshipped.txt +++ b/src/Louis/PublicAPI.Unshipped.txt @@ -86,6 +86,10 @@ static Louis.Fluency.FluentExtensions.IfElse(this T this, bool condition, Lou static Louis.Fluency.FluentExtensions.IfElse(this T this, bool condition, System.Action! then, System.Action! else) -> T static Louis.Fluency.FluentExtensions.IfNotNull(this T this, T1? arg, Louis.Fluency.FluentAction! then) -> T static Louis.Fluency.FluentExtensions.IfNotNull(this T this, T1? arg, System.Action! then) -> T +static Louis.Fluency.FluentExtensions.Invoke(this T this, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, System.Action! action) -> T +static Louis.Fluency.FluentExtensions.Invoke(this T this, TArg1 arg1, TArg2 arg2, TArg3 arg3, System.Action! action) -> T +static Louis.Fluency.FluentExtensions.Invoke(this T this, TArg1 arg1, TArg2 arg2, System.Action! action) -> T +static Louis.Fluency.FluentExtensions.Invoke(this T this, TArg arg, System.Action! action) -> T static Louis.Fluency.FluentExtensions.Invoke(this T this, System.Action! action) -> T static Louis.Fluency.FluentExtensions.Switch(this T this, TValue value, Louis.Fluency.FluentAction? default, params (TValue Comparand, Louis.Fluency.FluentAction? Action)[]! cases) -> T static Louis.Fluency.FluentExtensions.Switch(this T this, TValue value, Louis.Fluency.FluentAction? default, params (TValue Comparand, System.Action? Action)[]! cases) -> T From 1e32064fb55015854040f08093a57c54cfb30a0e Mon Sep 17 00:00:00 2001 From: Tenacom developer Date: Sun, 25 Jun 2023 05:16:37 +0200 Subject: [PATCH 2/2] Add InvokeIf method (closes #52) --- CHANGELOG.md | 1 + .../Fluency/FluentExtensions-InvokeIf.cs | 160 ++++++++++++++++++ src/Louis/PublicAPI.Unshipped.txt | 5 + 3 files changed, 166 insertions(+) create mode 100644 src/Louis/Fluency/FluentExtensions-InvokeIf.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index bd12742..71ee6f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### New features - Added overloads of `Louis.Fluency.FluentExtensions.Invoke` that allow for additional arguments to be passed to lambdas to avoid the allocation of closure objects. +- Added method `Louis.Fluency.FluentExtensions.InvokeIf` that calls the provided action only if a condition is `true`. ### Changes to existing features diff --git a/src/Louis/Fluency/FluentExtensions-InvokeIf.cs b/src/Louis/Fluency/FluentExtensions-InvokeIf.cs new file mode 100644 index 0000000..6684ba5 --- /dev/null +++ b/src/Louis/Fluency/FluentExtensions-InvokeIf.cs @@ -0,0 +1,160 @@ +// Copyright (c) Tenacom and contributors. Licensed under the MIT license. +// See the LICENSE file in the project root for full license information. + +using System; +using CommunityToolkit.Diagnostics; + +namespace Louis.Fluency; + +partial class FluentExtensions +{ + /// + /// Invokes an action on an object if a condition if satisfied, then returns the same object. + /// + /// The type of the object. + /// The object on which this method was called. + /// The condition to check. + /// The action to perform on . + /// A reference to after returns. + /// + /// is . + /// - or - + /// is . + /// + public static T InvokeIf(this T @this, bool condition, Action action) + { + Guard.IsNotNull(@this); + Guard.IsNotNull(action); + + if (condition) + { + action(@this); + } + + return @this; + } + + /// + /// Invokes an action on an object and an additional argument if a condition if satisfied, then returns the same object. + /// + /// The type of the object. + /// The type of the additional parameter to . + /// The object on which this method was called. + /// The condition to check. + /// The additional argument to pass tp . + /// The action to perform on and . + /// A reference to after returns. + /// + /// is . + /// - or - + /// is . + /// + public static T InvokeIf(this T @this, bool condition, TArg arg, Action action) + { + Guard.IsNotNull(@this); + Guard.IsNotNull(action); + + if (condition) + { + action(@this, arg); + } + + return @this; + } + + /// + /// Invokes an action on an object and additional arguments if a condition if satisfied, then returns the same object. + /// + /// The type of the object. + /// The type of the first additional parameter to . + /// The type of the second additional parameter to . + /// The object on which this method was called. + /// The condition to check. + /// The First additional argument to pass tp . + /// The second additional argument to pass tp . + /// The action to perform on and the additional arguments. + /// A reference to after returns. + /// + /// is . + /// - or - + /// is . + /// + public static T InvokeIf(this T @this, bool condition, TArg1 arg1, TArg2 arg2, Action action) + { + Guard.IsNotNull(@this); + Guard.IsNotNull(action); + + if (condition) + { + action(@this, arg1, arg2); + } + + return @this; + } + + /// + /// Invokes an action on an object and additional arguments if a condition if satisfied, then returns the same object. + /// + /// The type of the object. + /// The type of the first additional parameter to . + /// The type of the second additional parameter to . + /// The type of the third additional parameter to . + /// The object on which this method was called. + /// The condition to check. + /// The First additional argument to pass tp . + /// The second additional argument to pass tp . + /// The third additional argument to pass tp . + /// The action to perform on and the additional arguments. + /// A reference to after returns. + /// + /// is . + /// - or - + /// is . + /// + public static T InvokeIf(this T @this, bool condition, TArg1 arg1, TArg2 arg2, TArg3 arg3, Action action) + { + Guard.IsNotNull(@this); + Guard.IsNotNull(action); + + if (condition) + { + action(@this, arg1, arg2, arg3); + } + + return @this; + } + + /// + /// Invokes an action on an object and additional arguments if a condition if satisfied, then returns the same object. + /// + /// The type of the object. + /// The type of the first additional parameter to . + /// The type of the second additional parameter to . + /// The type of the third additional parameter to . + /// The type of the fourth additional parameter to . + /// The object on which this method was called. + /// The condition to check. + /// The First additional argument to pass tp . + /// The second additional argument to pass tp . + /// The third additional argument to pass tp . + /// The fourth additional argument to pass tp . + /// The action to perform on and the additional arguments. + /// A reference to after returns. + /// + /// is . + /// - or - + /// is . + /// + public static T InvokeIf(this T @this, bool condition, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, Action action) + { + Guard.IsNotNull(@this); + Guard.IsNotNull(action); + + if (condition) + { + action(@this, arg1, arg2, arg3, arg4); + } + + return @this; + } +} diff --git a/src/Louis/PublicAPI.Unshipped.txt b/src/Louis/PublicAPI.Unshipped.txt index d16ca48..336f9cf 100644 --- a/src/Louis/PublicAPI.Unshipped.txt +++ b/src/Louis/PublicAPI.Unshipped.txt @@ -91,6 +91,11 @@ static Louis.Fluency.FluentExtensions.Invoke(this T this static Louis.Fluency.FluentExtensions.Invoke(this T this, TArg1 arg1, TArg2 arg2, System.Action! action) -> T static Louis.Fluency.FluentExtensions.Invoke(this T this, TArg arg, System.Action! action) -> T static Louis.Fluency.FluentExtensions.Invoke(this T this, System.Action! action) -> T +static Louis.Fluency.FluentExtensions.InvokeIf(this T this, bool condition, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, System.Action! action) -> T +static Louis.Fluency.FluentExtensions.InvokeIf(this T this, bool condition, TArg1 arg1, TArg2 arg2, TArg3 arg3, System.Action! action) -> T +static Louis.Fluency.FluentExtensions.InvokeIf(this T this, bool condition, TArg1 arg1, TArg2 arg2, System.Action! action) -> T +static Louis.Fluency.FluentExtensions.InvokeIf(this T this, bool condition, TArg arg, System.Action! action) -> T +static Louis.Fluency.FluentExtensions.InvokeIf(this T this, bool condition, System.Action! action) -> T static Louis.Fluency.FluentExtensions.Switch(this T this, TValue value, Louis.Fluency.FluentAction? default, params (TValue Comparand, Louis.Fluency.FluentAction? Action)[]! cases) -> T static Louis.Fluency.FluentExtensions.Switch(this T this, TValue value, Louis.Fluency.FluentAction? default, params (TValue Comparand, System.Action? Action)[]! cases) -> T static Louis.Fluency.FluentExtensions.Switch(this T this, TValue value, params (TValue Comparand, Louis.Fluency.FluentAction? Action)[]! cases) -> T