Skip to content

Commit 80ab5ad

Browse files
authored
With (Combine) (#1)
Fist stage
1 parent 2a015bd commit 80ab5ad

File tree

7 files changed

+211
-0
lines changed

7 files changed

+211
-0
lines changed

CSharpFunctionalExtensions/CSharpFunctionalExtensions.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
<ItemGroup Condition="'$(TargetFramework)'=='net40'">
3737
<PackageReference Include="Microsoft.Bcl.Async" Version="1.0.168" />
3838
</ItemGroup>
39+
40+
<ItemGroup Condition="$(TargetFramework.StartsWith('net4')) OR $(TargetFramework)=='netstandard2.0'">
41+
<PackageReference Include="System.ValueTuple" Version="4.*" />
42+
</ItemGroup>
3943

4044
<ItemGroup>
4145
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0">
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
3+
namespace CSharpFunctionalExtensions
4+
{
5+
public partial class ResultExtensions
6+
{
7+
public static Result<T, E2> BindError<T, E, E2>(this Result<T, E> self,
8+
Func<E, Result<T, E2>> func)
9+
{
10+
if (self.IsSuccess)
11+
{
12+
return Result.Success<T, E2>(self.Value);
13+
}
14+
15+
return func(self.Error);
16+
}
17+
18+
public static Result<T> BindError<T>(this Result<T> self,
19+
Func<string, Result<T>> func)
20+
{
21+
if (self.IsSuccess)
22+
{
23+
return Result.Success(self.Value);
24+
}
25+
26+
return func(self.Error);
27+
}
28+
}
29+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
4+
namespace CSharpFunctionalExtensions
5+
{
6+
public static partial class ResultExtensions
7+
{
8+
public static Task<Result<TResult>> WithMap<T1, T2, TResult>(this Result<T1> a,
9+
Result<T2> b,
10+
Func<T1, T2, Task<TResult>> func)
11+
{
12+
var mapSuccess =
13+
a.BindError(e1 => b
14+
.MapError(e2 => Errors.Join(e1, e2))
15+
.Bind(_ => Result.Failure<T1>(e1)))
16+
.Bind(x => b
17+
.Map(y => func(x, y))
18+
.MapError(el => el));
19+
20+
return mapSuccess;
21+
}
22+
23+
public static Task<Result<TResult>> WithBind<T1, T2, TResult>(this Result<T1> a,
24+
Result<T2> b,
25+
Func<T1, T2, Task<Result<TResult>>> func)
26+
{
27+
var mapSuccess =
28+
a.BindError(e1 => b
29+
.MapError(e2 => Errors.Join(e1, e2))
30+
.Bind(_ => Result.Failure<T1>(e1)))
31+
.Bind(x => b
32+
.Bind(y => func(x, y))
33+
.MapError(el => el));
34+
35+
return mapSuccess;
36+
}
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
4+
namespace CSharpFunctionalExtensions
5+
{
6+
public static partial class ResultExtensions
7+
{
8+
public static Task<Result<TResult, E>> WithBind<T1, T2, E, TResult>(this Result<T1, E> a,
9+
Result<T2, E> b,
10+
Func<T1, T2, Task<Result<TResult, E>>> map, Func<E, E, E> combineError)
11+
{
12+
var mapSuccess =
13+
a.BindError(e1 => b
14+
.MapError(e2 => combineError(e1, e2))
15+
.Bind(_ => Result.Failure<T1, E>(e1)))
16+
.Bind(x => b
17+
.Bind(y => map(x, y))
18+
.MapError(el => el));
19+
20+
return mapSuccess;
21+
}
22+
23+
public static Task<Result<TResult, E>> WithMap<T1, T2, E, TResult>(this Result<T1, E> a,
24+
Result<T2, E> b,
25+
Func<T1, T2, Task<TResult>> map, Func<E, E, E> combineError)
26+
{
27+
var mapSuccess =
28+
a.BindError(e1 => b
29+
.MapError(e2 => combineError(e1, e2))
30+
.Bind(_ => Result.Failure<T1, E>(e1)))
31+
.Bind(x => b
32+
.Map(y => map(x, y))
33+
.MapError(el => el));
34+
35+
return mapSuccess;
36+
}
37+
}
38+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace CSharpFunctionalExtensions
2+
{
3+
public static class Errors
4+
{
5+
public static string Join(string e1, string e2)
6+
{
7+
return string.Join(Result.ErrorMessagesSeparator, e1, e2);
8+
}
9+
}
10+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
3+
namespace CSharpFunctionalExtensions
4+
{
5+
public static partial class ResultExtensions
6+
{
7+
public static Result<(T1, T2)> With<T1, T2>(Result<T1> a, Result<T2> b)
8+
{
9+
return a.WithMap(b, (x, y) => (x, y));
10+
}
11+
12+
public static Result<TResult> WithMap<T, K, TResult>(
13+
this Result<T> a,
14+
Result<K> b,
15+
Func<T, K, TResult> func)
16+
{
17+
return a
18+
.BindError(e1 => b
19+
.MapError(e2 => Errors.Join(e1, e2))
20+
.Bind(_ => Result.Failure<T>(e1))
21+
).Bind(x => b
22+
.Map(y => func(x, y))
23+
.MapError(e => e));
24+
}
25+
26+
public static Result<TResult> WithBind<T, K, TResult>(
27+
this Result<T> a,
28+
Result<K> b,
29+
Func<T, K, Result<TResult>> func)
30+
{
31+
return a
32+
.BindError(e1 => b
33+
.MapError(e2 => Errors.Join(e1, e2))
34+
.Bind(_ => Result.Failure<T>(e1))
35+
).Bind(x => b
36+
.Bind(y => func(x, y))
37+
.MapError(e => e));
38+
}
39+
}
40+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
3+
namespace CSharpFunctionalExtensions
4+
{
5+
public static partial class ResultExtensions
6+
{
7+
public static Result<T, E> WithBind<T, E>(
8+
this Result<T, E> a,
9+
Result<T, E> b,
10+
Func<T, T, Result<T, E>> func, Func<E, E, E> combineError)
11+
{
12+
return a
13+
.BindError(e1 => b
14+
.MapError(e2 => combineError(e1, e2))
15+
.Bind(_ => Result.Failure<T, E>(e1)))
16+
.Bind(x => b
17+
.Bind(y => func(x, y))
18+
.MapError(el => el));
19+
}
20+
21+
22+
public static Result<R, E> WithMap<T1, T2, E, R>(this Result<T1, E> a,
23+
Result<T2, E> b,
24+
Func<T1, T2, R> func, Func<E, E, E> combineError)
25+
{
26+
var mapSuccess =
27+
a.BindError(e1 => b
28+
.MapError(e2 => combineError(e1, e2))
29+
.Bind(_ => Result.Failure<T1, E>(e1)))
30+
.Bind(x => b
31+
.Map(y => func(x, y))
32+
.MapError(el => el));
33+
34+
return mapSuccess;
35+
}
36+
37+
public static Result<R, E> WithBind<T1, T2, E, R>(this Result<T1, E> a,
38+
Result<T2, E> b,
39+
Func<T1, T2, Result<R, E>> func, Func<E, E, E> combineError)
40+
{
41+
var mapSuccess =
42+
a.BindError(e1 => b
43+
.MapError(e2 => combineError(e1, e2))
44+
.Bind(_ => Result.Failure<T1, E>(e1)))
45+
.Bind(x => b
46+
.Bind(y => func(x, y))
47+
.MapError(el => el));
48+
49+
return mapSuccess;
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)