Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extensions to asyncmaybe #115

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Threading.Tasks;
using Lemonad.ErrorHandling.Extensions.Maybe.Task;

namespace Lemonad.ErrorHandling.Extensions.AsyncMaybe.Task {
public static partial class Index {
public static IAsyncMaybe<T> ToAsyncMaybe<T>(this Task<IAsyncMaybe<T>> source)
=> Resolve(source).ToAsyncMaybe();

private static async Task<IMaybe<T>> Resolve<T>(Task<IAsyncMaybe<T>> source) {
if (source is null) throw new ArgumentNullException(nameof(source));
var awaitedSource = await source.ConfigureAwait(false);
if (awaitedSource is null) throw new ArgumentNullException(nameof(awaitedSource));
return await awaitedSource.HasValue.ConfigureAwait(false)
? ErrorHandling.Maybe.Value(awaitedSource.Value)
: ErrorHandling.Maybe.None<T>();
}
}
}
19 changes: 19 additions & 0 deletions src/Lemonad.ErrorHandling/Extensions/AsyncMaybe/ToEnumerable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Lemonad.ErrorHandling.Extensions.Maybe;
using Lemonad.ErrorHandling.Internal;

namespace Lemonad.ErrorHandling.Extensions.AsyncMaybe {
public static partial class Index {
/// <summary>
/// Treat <typeparamref name="TSource" /> as enumerable with 0-1 elements in.
/// This is handy when combining <see cref="Maybe{T}" /> with LINQ's API.
/// </summary>
public static async Task<IEnumerable<TSource>> ToEnumerable<TSource>(this IAsyncMaybe<TSource> source) {
if (source is null) throw new ArgumentNullException(nameof(source));
var maybe = await source;
return maybe.ToEnumerable();
}
}
}
2 changes: 1 addition & 1 deletion src/Lemonad.ErrorHandling/Internal/AsyncMaybe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public IAsyncMaybe<T> FilterAsync(Func<T, Task<bool>> predicate) => predicate is

public IAsyncMaybe<TResult> FlatMap<TResult>(Func<T, IMaybe<TResult>> selector) => selector is null
? throw new ArgumentNullException(nameof(selector))
: _asyncResult.FlatMap(x => Index.ToResult(selector(x), Unit.Selector)).ToAsyncMaybe();
: _asyncResult.FlatMap(x => Extensions.Maybe.Index.ToResult(selector(x), Unit.Selector)).ToAsyncMaybe();

public IAsyncMaybe<TResult> FlatMap<TResult>(Func<T, TResult?> selector) where TResult : struct =>
selector is null
Expand Down
7 changes: 3 additions & 4 deletions src/Lemonad.ErrorHandling/Internal/Maybe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Lemonad.ErrorHandling.Extensions.AsyncResult;
using Lemonad.ErrorHandling.Extensions.Maybe;
using Lemonad.ErrorHandling.Extensions.Result;
using Index = Lemonad.ErrorHandling.Extensions.Maybe.Index;

namespace Lemonad.ErrorHandling.Internal {
internal readonly struct Maybe<T> : IMaybe<T> {
Expand Down Expand Up @@ -74,7 +73,7 @@ public IAsyncMaybe<T> FilterAsync(Func<T, Task<bool>> predicate) => predicate is
public IMaybe<TResult> FlatMap<TResult>(Func<T, IMaybe<TResult>> selector) => selector is null
? throw new ArgumentNullException(nameof(selector))
: _result
.FlatMap(x => Index.ToResult(selector(x), Unit.Selector)).ToMaybe();
.FlatMap(x => Extensions.Maybe.Index.ToResult(selector(x), Unit.Selector)).ToMaybe();

public IAsyncMaybe<TResult> FlatMapAsync<TResult>(Func<T, IAsyncMaybe<TResult>> selector)
=> _result.FlatMapAsync(
Expand Down Expand Up @@ -133,8 +132,8 @@ public IMaybe<T> IsNoneWhen(Func<T, bool> predicate) => predicate is null

public IMaybe<T> Flatten<TResult>(Func<T, IMaybe<TResult>> selector) => selector is null
? throw new ArgumentNullException(nameof(selector))
: _result.Flatten(x =>
Index.ToResult(selector(x), Unit.Selector)).ToMaybe();
: _result.Flatten(x => Lemonad.ErrorHandling.Extensions.Maybe.Index.ToResult(selector(x), Unit.Selector))
.ToMaybe();

public IMaybe<TResult> FlatMap<TSelector, TResult>(
Func<T, TSelector?> selector,
Expand Down