Skip to content

Commit

Permalink
ToResult: support for async
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBogomips committed Dec 9, 2024
1 parent dba4cd3 commit 7d3cd7e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
3 changes: 1 addition & 2 deletions src/Monads/Maybe/Maybe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ public Maybe(TValue? value)
public Maybe<TNewValue> Map<TNewValue>(Func<TValue, TNewValue?> map) where TNewValue : class
=> Value is not null ? new Maybe<TNewValue>(map(Value)) : Maybe<TNewValue>.None;

public Result<TValue> MapToResult() =>
Value is not null ? Result.Success(Value) : Result.Failure<TValue>(MaybeNoneError.Default);


/// <inheritdoc cref="M:Bogoware.Monads.Maybe`1.Map``1(System.Func{`0,``0})"/>
public async Task<Maybe<TNewValue>> Map<TNewValue>(Func<TValue, Task<TNewValue?>> map) where TNewValue : class
Expand Down
14 changes: 12 additions & 2 deletions src/Monads/Maybe/MaybeAsyncExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,25 @@ public static Task<bool> Satisfy<TValue>(this Task<Maybe<TValue>> maybe, Func<TV
where TValue : class
=> maybe.Match(predicate, false);

/// <inheritdoc cref="M:Bogoware.Monads.MaybeExtensions.ToResult``1(Bogoware.Monads.Maybe{``0},System.Func{Bogoware.Monads.Error})"/>

/// <inheritdoc cref="M:Bogoware.Monads.MaybeExtensions.MapToResult``1(Bogoware.Monads.Maybe{``0})"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<Result<TValue>> ToResult<TValue>(this Task<Maybe<TValue>> maybeTask) where TValue : class
{
var maybe = await maybeTask;
return maybe.MapToResult();
}

/// <inheritdoc cref="M:Bogoware.Monads.MaybeExtensions.MapToResult``1(Bogoware.Monads.Maybe{``0},System.Func{Bogoware.Monads.Error})"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<Result<TValue>> ToResult<TValue>(this Task<Maybe<TValue>> maybeTask, Func<Error> errorFunc) where TValue : class
{
var maybe = await maybeTask;
return maybe.MapToResult(errorFunc);
}

/// <inheritdoc cref="M:Bogoware.Monads.MaybeExtensions.ToResult``1(Bogoware.Monads.Maybe{``0},System.Func{Bogoware.Monads.Error})"/>

/// <inheritdoc cref="M:Bogoware.Monads.MaybeExtensions.MapToResult``1(Bogoware.Monads.Maybe{``0},System.Func{Bogoware.Monads.Error})"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<Result<TValue>> ToResult<TValue>(this Task<Maybe<TValue>> maybeTask, Func<Task<Error>> errorFunc) where TValue : class
{
Expand Down
1 change: 0 additions & 1 deletion src/Monads/Maybe/MaybeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ Func<Task<TNewValue>> value
/// <summary>
/// Convert a <see cref="Maybe{T}"/> to a <see cref="Result{TValue}"/> with a default error in case of <c>None</c>.
/// </summary>
/// <param name="maybe"></param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Result<TValue> MapToResult<TValue>(this Maybe<TValue> maybe)
where TValue : class
Expand Down

0 comments on commit 7d3cd7e

Please sign in to comment.