Skip to content

Commit

Permalink
break API: ExecuteIf... methods renamed to If...
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBogomips committed Dec 13, 2024
1 parent a1db684 commit 745ea77
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 87 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ It is a generic type, with `T` representing the type of the value returned by th
* `Match`: Facilitates handling of the operation's result by providing separate paths for the "happy" and "unhappy" flows.
* `RecoverWith`: Provides a way to recover from an error by returning a `Result<T>`
* `Ensure`: Allows asserting a condition on the value returned by the operation.
* `ExecuteIfSuccess`: Executes if the operation succeeds. It is typically used to generate side effects.
* `ExecuteIfFailure`: Executes if the operation fails. It is typically used to generate side effects.
* `IfSuccess`: Executes if the operation succeeds. It is typically used to generate side effects.
* `IfFailure`: Executes if the operation fails. It is typically used to generate side effects.

There are also some unsafe methods intended to support developers who are less familiar with the functional approach
and may need to resort to a procedural style to achieve their goals.
Expand Down Expand Up @@ -95,7 +95,7 @@ public Result<Unit> Publish() {
return ValidateCostComponents() // Note the explicit invocation of the method
.Bind(ValidateTimingComponents)
// ... more binding to validation methods
.ExecuteIfSuccess(() => PublishingStatus = PublishingStatus.Published);
.IfSuccess(() => PublishingStatus = PublishingStatus.Published);
}
```

Expand All @@ -107,7 +107,7 @@ public Result<Unit> Publish() => Result
.Bind(ValidateCostComponents)
.Bind(ValidateTimingComponents)
// ... more binding to validation methods
.ExecuteIfSuccess(() => PublishingStatus = PublishingStatus.Published);
.IfSuccess(() => PublishingStatus = PublishingStatus.Published);
```

## Manipulating `IEnumerable<Maybe<T>>`
Expand Down
2 changes: 1 addition & 1 deletion sample/ConsoleApp/Pipelines/CreateUserPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static void Run(string? username = null!)
.Ensure(u => LookupUser(u).IsNone, new LogicError("User already exists"))
.Map(u => new User(u, "FirstName", "LastName"))
.Bind(CreateUser)
.ExecuteIfSuccess(NotifyCreation)
.IfSuccess(NotifyCreation)
.Match(u => "User created.", e => $"The following error occurred: {e.Message}");

// In a real example the final Match would return a specific model, for example, an IActionResult
Expand Down
6 changes: 3 additions & 3 deletions src/Monads/Maybe/Maybe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ public Task<TResult> Match<TResult>(Func<TValue, TResult> mapValue, Func<Task<TR
/// <summary>
/// Execute the action if the <see cref="Maybe{T}"/> is <c>Some</c>.
/// </summary>
public Maybe<TValue> ExecuteIfSome(Action<TValue> action)
public Maybe<TValue> IfSome(Action<TValue> action)
{
if (Value is not null) action(Value);
return this;
}

/// <inheritdoc cref="ExecuteIfSome(System.Action{TValue})"/>
public async Task<Maybe<TValue>> ExecuteIfSome(Func<TValue, Task> action)
/// <inheritdoc cref="M:Bogoware.Monads.Maybe`1.IfSome(System.Action{`0})"/>
public async Task<Maybe<TValue>> IfSome(Func<TValue, Task> action)
{
if (Value is not null) await action(Value);
return this;
Expand Down
24 changes: 12 additions & 12 deletions src/Monads/Maybe/MaybeAsyncExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,40 +129,40 @@ public static async Task<TResult> Match<TValue, TResult>(
=> await (await maybeTask).Match(value, none);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<Maybe<TValue>> ExecuteIfSome<TValue>(
public static async Task<Maybe<TValue>> IfSome<TValue>(
this Task<Maybe<TValue>> maybeTask,
Action action) where TValue : class
=> (await maybeTask).ExecuteIfSome(action);
=> (await maybeTask).IfSome(action);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<Maybe<TValue>> ExecuteIfSome<TValue>(
public static async Task<Maybe<TValue>> IfSome<TValue>(
this Task<Maybe<TValue>> maybeTask,
Action<TValue> action) where TValue : class
=> (await maybeTask).ExecuteIfSome(action);
=> (await maybeTask).IfSome(action);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<Maybe<TValue>> ExecuteIfSome<TValue>(
public static async Task<Maybe<TValue>> IfSome<TValue>(
this Task<Maybe<TValue>> maybeTask,
Func<Task> action) where TValue : class
=> await (await maybeTask).ExecuteIfSome(action);
=> await (await maybeTask).IfSome(action);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<Maybe<TValue>> ExecuteIfSome<TValue>(
public static async Task<Maybe<TValue>> IfSome<TValue>(
this Task<Maybe<TValue>> maybeTask,
Func<TValue, Task> action) where TValue : class
=> await (await maybeTask).ExecuteIfSome(action);
=> await (await maybeTask).IfSome(action);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<Maybe<TValue>> ExecuteIfNone<TValue>(
public static async Task<Maybe<TValue>> IfNone<TValue>(
this Task<Maybe<TValue>> maybeTask,
Action action) where TValue : class
=> (await maybeTask).ExecuteIfNone(action);
=> (await maybeTask).IfNone(action);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<Maybe<TValue>> ExecuteIfNone<TValue>(
public static async Task<Maybe<TValue>> IfNone<TValue>(
this Task<Maybe<TValue>> maybeTask,
Func<Task> action) where TValue : class
=> await (await maybeTask).ExecuteIfNone(action);
=> await (await maybeTask).IfNone(action);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<Maybe<TValue>> Execute<TValue>(
Expand Down
12 changes: 6 additions & 6 deletions src/Monads/Maybe/MaybeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,16 @@ public static async Task<Maybe<TValue>> Execute<TValue>(this Maybe<TValue> maybe
/// Execute the action if the <see cref="Maybe{T}"/> is <c>Some</c>.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref readonly Maybe<TValue> ExecuteIfSome<TValue>(in this Maybe<TValue> maybe, Action action)
public static ref readonly Maybe<TValue> IfSome<TValue>(in this Maybe<TValue> maybe, Action action)
where TValue : class
{
if (maybe.IsSome) action();
return ref maybe;
}

/// <inheritdoc cref="M:Bogoware.Monads.MaybeExtensions.ExecuteIfSome``1(Bogoware.Monads.Maybe{``0}@,System.Action)"/>
/// <inheritdoc cref="M:Bogoware.Monads.MaybeExtensions.IfSome``1(Bogoware.Monads.Maybe{``0}@,System.Action)"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<Maybe<TNewValue>> ExecuteIfSome<TNewValue>(this Maybe<TNewValue> maybe, Func<Task> action)
public static async Task<Maybe<TNewValue>> IfSome<TNewValue>(this Maybe<TNewValue> maybe, Func<Task> action)
where TNewValue : class
{
if (maybe.IsSome) await action();
Expand All @@ -189,16 +189,16 @@ public static async Task<Maybe<TNewValue>> ExecuteIfSome<TNewValue>(this Maybe<T
/// Execute the action if the <see cref="Maybe{T}"/> is <c>None</c>.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref readonly Maybe<TNewValue> ExecuteIfNone<TNewValue>(in this Maybe<TNewValue> maybe, Action action)
public static ref readonly Maybe<TNewValue> IfNone<TNewValue>(in this Maybe<TNewValue> maybe, Action action)
where TNewValue : class
{
if (maybe.IsNone) action();
return ref maybe;
}

/// <inheritdoc cref="M:Bogoware.Monads.MaybeExtensions.ExecuteIfNone``1(Bogoware.Monads.Maybe{``0}@,System.Action)"/>
/// <inheritdoc cref="M:Bogoware.Monads.MaybeExtensions.IfNone``1(Bogoware.Monads.Maybe{``0}@,System.Action)"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<Maybe<TNewValue>> ExecuteIfNone<TNewValue>(this Maybe<TNewValue> maybe, Func<Task> action)
public static async Task<Maybe<TNewValue>> IfNone<TNewValue>(this Maybe<TNewValue> maybe, Func<Task> action)
where TNewValue : class
{
if (maybe.IsNone) await action();
Expand Down
12 changes: 6 additions & 6 deletions src/Monads/Result/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,14 +370,14 @@ public async Task<Result<TValue>> Ensure(Func<TValue, Task<bool>> predicate, Fun
/// <summary>
/// Execute the action if the <see cref="Result{TValue}"/>.<see cref="IsSuccess"/> is true.
/// </summary>
public Result<TValue> ExecuteIfSuccess(Action<TValue> action)
public Result<TValue> IfSuccess(Action<TValue> action)
{
if (IsSuccess) action(Value!);
return this;
}

/// <inheritdoc cref="ExecuteIfSuccess(System.Action{TValue})"/>
public async Task<Result<TValue>> ExecuteIfSuccess(Func<TValue, Task> action)
/// <inheritdoc cref="M:Bogoware.Monads.Maybe`1.IfSome(System.Action{`0})"/>
public async Task<Result<TValue>> IfSuccess(Func<TValue, Task> action)
{
if (IsSuccess) await action(Value!);
return this;
Expand All @@ -386,15 +386,15 @@ public async Task<Result<TValue>> ExecuteIfSuccess(Func<TValue, Task> action)
/// <summary>
/// Execute the action if the <see cref="Result{TValue}"/>.<see cref="IsFailure"/> is true.
/// </summary>
public Result<TValue> ExecuteIfFailure(Action<Error> action)
public Result<TValue> IfFailure(Action<Error> action)
{
if (IsFailure) action(Error!);
return this;
}


/// <inheritdoc cref="ExecuteIfFailure(System.Action{Monads.Error})"/>
public async Task<Result<TValue>> ExecuteIfFailure(Func<Error, Task> action)
/// <inheritdoc cref="M:Bogoware.Monads.Result`1.IfFailure(System.Action{Bogoware.Monads.Error})"/>
public async Task<Result<TValue>> IfFailure(Func<Error, Task> action)
{
if (IsFailure) await action(Error!);
return this;
Expand Down
40 changes: 20 additions & 20 deletions src/Monads/Result/ResultExecuteExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,31 @@ public static class ResultExecuteExtensions
#region Functional Closure Extensions

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Result<TValue> ExecuteIfSuccess<TValue>(
public static Result<TValue> IfSuccess<TValue>(
this Result<TValue> result, Action action)
{
if (result.IsSuccess) action();
return result;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<Result<TValue>> ExecuteIfSuccess<TValue>(
public static async Task<Result<TValue>> IfSuccess<TValue>(
this Result<TValue> result, Func<Task> action)
{
if (result.IsSuccess) await action();
return result;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Result<TValue> ExecuteIfFailure<TValue>(
public static Result<TValue> IfFailure<TValue>(
this Result<TValue> result, Action action)
{
if (result.IsFailure) action();
return result;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<Result<TValue>> ExecuteIfFailure<TValue>(
public static async Task<Result<TValue>> IfFailure<TValue>(
this Result<TValue> result, Func<Task> action)
{
if (result.IsFailure) await action();
Expand Down Expand Up @@ -78,44 +78,44 @@ public static async Task<Result<TValue>> Execute<TValue>(
#region Left Async Extensions

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<Result<TValue>> ExecuteIfSuccess<TValue>(
public static async Task<Result<TValue>> IfSuccess<TValue>(
this Task<Result<TValue>> result, Action<TValue> action)
=> (await result).ExecuteIfSuccess(action);
=> (await result).IfSuccess(action);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<Result<TValue>> ExecuteIfSuccess<TValue>(
public static async Task<Result<TValue>> IfSuccess<TValue>(
this Task<Result<TValue>> result, Func<TValue, Task> action)
=> await (await result).ExecuteIfSuccess(action);
=> await (await result).IfSuccess(action);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<Result<TValue>> ExecuteIfSuccess<TValue>(
public static async Task<Result<TValue>> IfSuccess<TValue>(
this Task<Result<TValue>> result, Action action)
=> (await result).ExecuteIfSuccess(action);
=> (await result).IfSuccess(action);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<Result<TValue>> ExecuteIfSuccess<TValue>(
public static async Task<Result<TValue>> IfSuccess<TValue>(
this Task<Result<TValue>> result, Func<Task> action)
=> await (await result).ExecuteIfSuccess(action);
=> await (await result).IfSuccess(action);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<Result<TValue>> ExecuteIfFailure<TValue>(
public static async Task<Result<TValue>> IfFailure<TValue>(
this Task<Result<TValue>> result, Action<Error> action)
=> (await result).ExecuteIfFailure(action);
=> (await result).IfFailure(action);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<Result<TValue>> ExecuteIfFailure<TValue>(
public static async Task<Result<TValue>> IfFailure<TValue>(
this Task<Result<TValue>> result, Func<Error, Task> action)
=> await (await result).ExecuteIfFailure(action);
=> await (await result).IfFailure(action);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<Result<TValue>> ExecuteIfFailure<TValue>(
public static async Task<Result<TValue>> IfFailure<TValue>(
this Task<Result<TValue>> result, Action action)
=> (await result).ExecuteIfFailure(action);
=> (await result).IfFailure(action);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<Result<TValue>> ExecuteIfFailure<TValue>(
public static async Task<Result<TValue>> IfFailure<TValue>(
this Task<Result<TValue>> result, Func<Task> action)
=> await (await result).ExecuteIfFailure(action);
=> await (await result).IfFailure(action);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<Result<TValue>> Execute<TValue>(
Expand Down
12 changes: 6 additions & 6 deletions test/Monads.UnitTests/MaybeTests/MaybeAsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public async Task IfSome_action()
{
var inspector = new Mock<ICallInspector>();
var sut = Task.FromResult(Maybe.Some(new Value(0)));
await sut.ExecuteIfSome(inspector.Object.MethodVoid);
await sut.IfSome(inspector.Object.MethodVoid);
inspector.Verify(c => c.MethodVoid());
}

Expand All @@ -265,7 +265,7 @@ public async Task IfSome_action_with_arg()
{
var inspector = new Mock<ICallInspector>();
var sut = Task.FromResult(Maybe.Some(new Value(0)));
await sut.ExecuteIfSome(inspector.Object.MethodWithValueArg);
await sut.IfSome(inspector.Object.MethodWithValueArg);
inspector.Verify(c => c.MethodWithValueArg(It.IsAny<Value>()));
}

Expand All @@ -274,7 +274,7 @@ public async Task IfSome_asyncAction()
{
var inspector = new Mock<ICallInspector>();
var sut = Task.FromResult(Maybe.Some(new Value(0)));
await sut.ExecuteIfSome(inspector.Object.MethodVoidAsync);
await sut.IfSome(inspector.Object.MethodVoidAsync);
inspector.Verify(c => c.MethodVoidAsync());
}

Expand All @@ -283,7 +283,7 @@ public async Task IfSome_asyncAction_with_arg()
{
var inspector = new Mock<ICallInspector>();
var sut = Task.FromResult(Maybe.Some(new Value(0)));
await sut.ExecuteIfSome(inspector.Object.MethodWithValueArgAsync);
await sut.IfSome(inspector.Object.MethodWithValueArgAsync);
inspector.Verify(c => c.MethodWithValueArgAsync(It.IsAny<Value>()));
}

Expand All @@ -292,15 +292,15 @@ public async Task IfNone_action()
{
var inspector = new Mock<ICallInspector>();
var sut = Task.FromResult(Maybe.None<Value>());
await sut.ExecuteIfNone(inspector.Object.MethodVoid);
await sut.IfNone(inspector.Object.MethodVoid);
inspector.Verify(c => c.MethodVoid());
}
[Fact]
public async Task IfNone_actionAsync()
{
var inspector = new Mock<ICallInspector>();
var sut = Task.FromResult(Maybe.None<Value>());
await sut.ExecuteIfNone(inspector.Object.MethodVoidAsync);
await sut.IfNone(inspector.Object.MethodVoidAsync);
inspector.Verify(c => c.MethodVoidAsync());
}

Expand Down
Loading

0 comments on commit 745ea77

Please sign in to comment.