-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #540 from PNZeml/master
Add BindZip to return the Tuple of merged Binds
- Loading branch information
Showing
7 changed files
with
861 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
CSharpFunctionalExtensions.Examples/ResultExtensions/Methods/BindZipExamples.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace CSharpFunctionalExtensions.Examples.ResultExtensions.Methods | ||
{ | ||
public class BindZipExamples | ||
{ | ||
public async Task BindZipAsyncSample() | ||
{ | ||
await FetchFirstEntity() | ||
.BindZip(FetchSecondEntity) | ||
.Map(x => $"{x.First}, {x.Second}!"); | ||
|
||
return; | ||
|
||
Task<Result<string>> FetchFirstEntity() => | ||
Task.FromResult(Result.Success("Hello")); | ||
|
||
Result<string> FetchSecondEntity(string _) => | ||
"World"; | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
CSharpFunctionalExtensions.Tests/ResultTests/Extensions/BindZipTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System.Runtime.CompilerServices; | ||
using FluentAssertions; | ||
using FluentAssertions.Execution; | ||
using Xunit; | ||
|
||
namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions; | ||
|
||
public class BindZipTests : BindTestsBase | ||
{ | ||
[Fact] | ||
public void BindZip_T_with_Bind_K_returns_success() | ||
{ | ||
Result<(T, K)> output = Success_T(T.Value).BindZip(_ => Success_K()); | ||
|
||
using (var _ = new AssertionScope()) | ||
{ | ||
AssertSuccess(output); | ||
|
||
(var _, var _, (T t, K k)) = output; | ||
t.Should().Be(T.Value); | ||
k.Should().Be(K.Value); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void BindZip_T_With_Bind_K_eight_times_returns_success() | ||
{ | ||
Result<(T, K, K, K, K, K, K, K)> output = Success_T(T.Value) | ||
.BindZip(_ => Success_K()) | ||
.BindZip((_, _) => Success_K()) | ||
.BindZip((_, _, _) => Success_K()) | ||
.BindZip((_, _, _, _) => Success_K()) | ||
.BindZip((_, _, _, _, _) => Success_K()) | ||
.BindZip((_, _, _, _, _, _) => Success_K()) | ||
.BindZip((_, _, _, _, _, _, _) => Success_K()); | ||
|
||
((ITuple)output.Value).Length.Should().Be(8); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
CSharpFunctionalExtensions/Result/Methods/Extensions/BindZip.Task.Left.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
#if (NETSTANDARD || NETCORE || NET5_0_OR_GREATER) | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace CSharpFunctionalExtensions | ||
{ | ||
public static partial class ResultExtensions | ||
{ | ||
public static Task<Result<(T First, K Second)>> BindZip<T, K>( | ||
this Result<T> result, Func<T, Task<Result<K>>> func | ||
) { | ||
return result.IsFailure | ||
? Result.Failure<(T, K)>(result.Error).AsCompletedTask() | ||
: result.AsCompletedTask().BindZip(func); | ||
} | ||
|
||
public static Task<Result<(T First, K Second), E>> BindZip<T, K, E>( | ||
this Result<T, E> result, Func<T, Task<Result<K, E>>> func | ||
) { | ||
return result.IsFailure | ||
? Result.Failure<(T, K), E>(result.Error).AsCompletedTask() | ||
: result.AsCompletedTask().BindZip(func); | ||
} | ||
|
||
public static Task<Result<(T1 First, T2 Second, K Third)>> BindZip<T1, T2, K>( | ||
this Result<(T1, T2)> result, Func<T1, T2, Task<Result<K>>> func | ||
) { | ||
return result.IsFailure | ||
? Result.Failure<(T1, T2, K)>(result.Error).AsCompletedTask() | ||
: result.AsCompletedTask().BindZip(func); | ||
} | ||
|
||
public static Task<Result<(T1 First, T2 Second, K Third), E>> BindZip<T1, T2, K, E>( | ||
this Result<(T1, T2), E> result, Func<T1, T2, Task<Result<K, E>>> func | ||
) { | ||
return result.IsFailure | ||
? Result.Failure<(T1, T2, K), E>(result.Error).AsCompletedTask() | ||
: result.AsCompletedTask().BindZip(func); | ||
} | ||
|
||
public static Task<Result<(T1, T2, T3, K)>> BindZip<T1, T2, T3, K>( | ||
this Result<(T1, T2, T3)> result, Func<T1, T2, T3, Task<Result<K>>> func | ||
) { | ||
return result.IsFailure | ||
? Result.Failure<(T1, T2, T3, K)>(result.Error).AsCompletedTask() | ||
: result.AsCompletedTask().BindZip(func); | ||
} | ||
|
||
public static Task<Result<(T1, T2, T3, K), E>> BindZip<T1, T2, T3, K, E>( | ||
this Result<(T1, T2, T3), E> result, Func<T1, T2, T3, Task<Result<K, E>>> func | ||
) { | ||
return result.IsFailure | ||
? Result.Failure<(T1, T2, T3, K), E>(result.Error).AsCompletedTask() | ||
: result.AsCompletedTask().BindZip(func); | ||
} | ||
|
||
public static Task<Result<(T1, T2, T3, T4, K)>> BindZip<T1, T2, T3, T4, K>( | ||
this Result<(T1, T2, T3, T4)> result, Func<T1, T2, T3, T4, Task<Result<K>>> func | ||
) { | ||
return result.IsFailure | ||
? Result.Failure<(T1, T2, T3, T4, K)>(result.Error).AsCompletedTask() | ||
: result.AsCompletedTask().BindZip(func); | ||
} | ||
|
||
public static Task<Result<(T1, T2, T3, T4, K), E>> BindZip<T1, T2, T3, T4, K, E>( | ||
this Result<(T1, T2, T3, T4), E> result, Func<T1, T2, T3, T4, Task<Result<K, E>>> func | ||
) { | ||
return result.IsFailure | ||
? Result.Failure<(T1, T2, T3, T4, K), E>(result.Error).AsCompletedTask() | ||
: result.AsCompletedTask().BindZip(func); | ||
} | ||
|
||
public static Task<Result<(T1, T2, T3, T4, T5, K)>> BindZip<T1, T2, T3, T4, T5, K>( | ||
this Result<(T1, T2, T3, T4, T5)> result, Func<T1, T2, T3, T4, T5, Task<Result<K>>> func | ||
) { | ||
return result.IsFailure | ||
? Result.Failure<(T1, T2, T3, T4, T5, K)>(result.Error).AsCompletedTask() | ||
: result.AsCompletedTask().BindZip(func); | ||
} | ||
|
||
public static Task<Result<(T1, T2, T3, T4, T5, K), E>> BindZip<T1, T2, T3, T4, T5, K, E>( | ||
this Result<(T1, T2, T3, T4, T5), E> result, Func<T1, T2, T3, T4, T5, Task<Result<K, E>>> func | ||
) { | ||
return result.IsFailure | ||
? Result.Failure<(T1, T2, T3, T4, T5, K), E>(result.Error).AsCompletedTask() | ||
: result.AsCompletedTask().BindZip(func); | ||
} | ||
|
||
public static Task<Result<(T1, T2, T3, T4, T5, T6, K)>> BindZip<T1, T2, T3, T4, T5, T6, K>( | ||
this Result<(T1, T2, T3, T4, T5, T6)> result, Func<T1, T2, T3, T4, T5, T6, Task<Result<K>>> func | ||
) { | ||
return result.IsFailure | ||
? Result.Failure<(T1, T2, T3, T4, T5, T6, K)>(result.Error).AsCompletedTask() | ||
: result.AsCompletedTask().BindZip(func); | ||
} | ||
|
||
public static Task<Result<(T1, T2, T3, T4, T5, T6, K), E>> BindZip<T1, T2, T3, T4, T5, T6, K, E>( | ||
this Result<(T1, T2, T3, T4, T5, T6), E> result, Func<T1, T2, T3, T4, T5, T6, Task<Result<K, E>>> func | ||
) { | ||
return result.IsFailure | ||
? Result.Failure<(T1, T2, T3, T4, T5, T6, K), E>(result.Error).AsCompletedTask() | ||
: result.AsCompletedTask().BindZip(func); | ||
} | ||
|
||
public static Task<Result<(T1, T2, T3, T4, T5, T6, T7, K)>> BindZip<T1, T2, T3, T4, T5, T6, T7, K>( | ||
this Result<(T1, T2, T3, T4, T5, T6, T7)> result, | ||
Func<T1, T2, T3, T4, T5, T6, T7, Task<Result<K>>> func | ||
) { | ||
return result.IsFailure | ||
? Result.Failure<(T1, T2, T3, T4, T5, T6, T7, K)>(result.Error).AsCompletedTask() | ||
: result.AsCompletedTask().BindZip(func); | ||
} | ||
|
||
public static Task<Result<(T1, T2, T3, T4, T5, T6, T7, K), E>> BindZip<T1, T2, T3, T4, T5, T6, T7, K, E>( | ||
this Result<(T1, T2, T3, T4, T5, T6, T7), E> result, | ||
Func<T1, T2, T3, T4, T5, T6, T7, Task<Result<K, E>>> func | ||
) { | ||
return result.IsFailure | ||
? Result.Failure<(T1, T2, T3, T4, T5, T6, T7, K), E>(result.Error).AsCompletedTask() | ||
: result.AsCompletedTask().BindZip(func); | ||
} | ||
} | ||
} | ||
#endif |
148 changes: 148 additions & 0 deletions
148
CSharpFunctionalExtensions/Result/Methods/Extensions/BindZip.Task.Right.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
#if (NETSTANDARD || NETCORE || NET5_0_OR_GREATER) | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace CSharpFunctionalExtensions | ||
{ | ||
public static partial class ResultExtensions | ||
{ | ||
public static async Task<Result<(T First, K Second)>> BindZip<T, K>( | ||
this Task<Result<T>> resultTask, Func<T, Result<K>> func | ||
) { | ||
var result = await resultTask.DefaultAwait(); | ||
|
||
return result.IsFailure ? Result.Failure<(T, K)>(result.Error) : result.BindZip(func); | ||
} | ||
|
||
public static async Task<Result<(T First, K Second), E>> BindZip<T, K, E>( | ||
this Task<Result<T, E>> resultTask, Func<T, Result<K, E>> func | ||
) { | ||
var result = await resultTask.DefaultAwait(); | ||
|
||
return result.IsFailure ? Result.Failure<(T, K), E>(result.Error) : result.BindZip(func); | ||
} | ||
|
||
public static async Task<Result<(T1 First, T2 Second, K Third)>> BindZip<T1, T2, K>( | ||
this Task<Result<(T1, T2)>> resultTask, Func<T1, T2, Result<K>> func | ||
) { | ||
var result = await resultTask.DefaultAwait(); | ||
|
||
return result.IsFailure | ||
? Result.Failure<(T1, T2, K)>(result.Error) : result.BindZip(func); | ||
} | ||
|
||
public static async Task<Result<(T1 First, T2 Second, K Third), E>> BindZip<T1, T2, K, E>( | ||
this Task<Result<(T1, T2), E>> resultTask, Func<T1, T2, Result<K, E>> func | ||
) { | ||
var result = await resultTask.DefaultAwait(); | ||
|
||
return result.IsFailure | ||
? Result.Failure<(T1, T2, K), E>(result.Error) : result.BindZip(func); | ||
} | ||
|
||
public static async Task<Result<(T1, T2, T3, K)>> BindZip<T1, T2, T3, K>( | ||
this Task<Result<(T1, T2, T3)>> resultTask, Func<T1, T2, T3, Result<K>> func | ||
) { | ||
var result = await resultTask.DefaultAwait(); | ||
|
||
return result.IsFailure | ||
? Result.Failure<(T1, T2, T3, K)>(result.Error) | ||
: result.BindZip(func); | ||
} | ||
|
||
public static async Task<Result<(T1, T2, T3, K), E>> BindZip<T1, T2, T3, K, E>( | ||
this Task<Result<(T1, T2, T3), E>> resultTask, Func<T1, T2, T3, Result<K, E>> func | ||
) { | ||
var result = await resultTask.DefaultAwait(); | ||
|
||
return result.IsFailure | ||
? Result.Failure<(T1, T2, T3, K), E>(result.Error) | ||
: result.BindZip(func); | ||
} | ||
|
||
public static async Task<Result<(T1, T2, T3, T4, K)>> BindZip<T1, T2, T3, T4, K>( | ||
this Task<Result<(T1, T2, T3, T4)>> resultTask, Func<T1, T2, T3, T4, Result<K>> func | ||
) { | ||
var result = await resultTask.DefaultAwait(); | ||
|
||
return result.IsFailure | ||
? Result.Failure<(T1, T2, T3, T4, K)>(result.Error) | ||
: result.BindZip(func); | ||
} | ||
|
||
public static async Task<Result<(T1, T2, T3, T4, K), E>> BindZip<T1, T2, T3, T4, K, E>( | ||
this Task<Result<(T1, T2, T3, T4), E>> resultTask, Func<T1, T2, T3, T4, Result<K, E>> func | ||
) { | ||
var result = await resultTask.DefaultAwait(); | ||
|
||
return result.IsFailure | ||
? Result.Failure<(T1, T2, T3, T4, K), E>(result.Error) | ||
: result.BindZip(func); | ||
} | ||
|
||
public static async Task<Result<(T1, T2, T3, T4, T5, K)>> BindZip<T1, T2, T3, T4, T5, K>( | ||
this Task<Result<(T1, T2, T3, T4, T5)>> resultTask, Func<T1, T2, T3, T4, T5, Result<K>> func | ||
) { | ||
var result = await resultTask.DefaultAwait(); | ||
|
||
return result.IsFailure | ||
? Result.Failure<(T1, T2, T3, T4, T5, K)>(result.Error) | ||
: result.BindZip(func); | ||
} | ||
|
||
public static async Task<Result<(T1, T2, T3, T4, T5, K), E>> BindZip<T1, T2, T3, T4, T5, K, E>( | ||
this Task<Result<(T1, T2, T3, T4, T5), E>> resultTask, Func<T1, T2, T3, T4, T5, Result<K, E>> func | ||
) { | ||
var result = await resultTask.DefaultAwait(); | ||
|
||
return result.IsFailure | ||
? Result.Failure<(T1, T2, T3, T4, T5, K), E>(result.Error) | ||
: result.BindZip(func); | ||
} | ||
|
||
public static async Task<Result<(T1, T2, T3, T4, T5, T6, K)>> BindZip<T1, T2, T3, T4, T5, T6, K>( | ||
this Task<Result<(T1, T2, T3, T4, T5, T6)>> resultTask, | ||
Func<T1, T2, T3, T4, T5, T6, Result<K>> func | ||
) { | ||
var result = await resultTask.DefaultAwait(); | ||
|
||
return result.IsFailure | ||
? Result.Failure<(T1, T2, T3, T4, T5, T6, K)>(result.Error) | ||
: result.BindZip(func); | ||
} | ||
|
||
public static async Task<Result<(T1, T2, T3, T4, T5, T6, K), E>> BindZip<T1, T2, T3, T4, T5, T6, K, E>( | ||
this Task<Result<(T1, T2, T3, T4, T5, T6), E>> resultTask, | ||
Func<T1, T2, T3, T4, T5, T6, Result<K, E>> func | ||
) { | ||
var result = await resultTask.DefaultAwait(); | ||
|
||
return result.IsFailure | ||
? Result.Failure<(T1, T2, T3, T4, T5, T6, K), E>(result.Error) | ||
: result.BindZip(func); | ||
} | ||
|
||
public static async Task<Result<(T1, T2, T3, T4, T5, T6, T7, K)>> BindZip<T1, T2, T3, T4, T5, T6, T7, K>( | ||
this Task<Result<(T1, T2, T3, T4, T5, T6, T7)>> resultTask, | ||
Func<T1, T2, T3, T4, T5, T6, T7, Result<K>> func | ||
) { | ||
var result = await resultTask.DefaultAwait(); | ||
|
||
return result.IsFailure | ||
? Result.Failure<(T1, T2, T3, T4, T5, T6, T7, K)>(result.Error) | ||
: result.BindZip(func); | ||
} | ||
|
||
public static async Task<Result<(T1, T2, T3, T4, T5, T6, T7, K), E>> BindZip<T1, T2, T3, T4, T5, T6, T7, K, E>( | ||
this Task<Result<(T1, T2, T3, T4, T5, T6, T7), E>> resultTask, | ||
Func<T1, T2, T3, T4, T5, T6, T7, Result<K, E>> func | ||
) { | ||
var result = await resultTask.DefaultAwait(); | ||
|
||
return result.IsFailure | ||
? Result.Failure<(T1, T2, T3, T4, T5, T6, T7, K), E>(result.Error) | ||
: result.BindZip(func); | ||
} | ||
} | ||
} | ||
#endif |
Oops, something went wrong.