Skip to content

Commit

Permalink
Merge pull request #540 from PNZeml/master
Browse files Browse the repository at this point in the history
Add BindZip to return the Tuple of merged Binds
  • Loading branch information
vkhorikov authored Apr 29, 2024
2 parents 919185f + 2c97ae7 commit 20e7682
Show file tree
Hide file tree
Showing 7 changed files with 861 additions and 0 deletions.
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";
}
}
}
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);
}
}
9 changes: 9 additions & 0 deletions CSharpFunctionalExtensions/CSharpFunctionalExtensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,15 @@
<Compile Update="Result\Methods\Extensions\TapIfTry.Task.Right.cs">
<DependentUpon>TapIfTry.Task.cs</DependentUpon>
</Compile>
<Compile Update="Result\Methods\Extensions\BindZip.Task.cs">
<DependentUpon>BindZip.cs</DependentUpon>
</Compile>
<Compile Update="Result\Methods\Extensions\BindZip.Task.Left.cs">
<DependentUpon>BindZip.Task.cs</DependentUpon>
</Compile>
<Compile Update="Result\Methods\Extensions\BindZip.Task.Right.cs">
<DependentUpon>BindZip.Task.cs</DependentUpon>
</Compile>
</ItemGroup>

<ItemGroup>
Expand Down
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
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
Loading

0 comments on commit 20e7682

Please sign in to comment.