Table of Contents

EitherAsyncExtensions Class

Definition

Namespace
Bodu.Functional
Assembly
Bodu.Core.dll
Package
Bodu.Core 1.0.1
Source
EitherAsyncExtensions.MapLeftAsync.cs

Provides Task-based asynchronous companions to the Either<TLeft, TRight> combinators.

public static class EitherAsyncExtensions
Inheritance
EitherAsyncExtensions
Inherited Members

Remarks

Each operation mirrors its synchronous counterpart - MapLeftAsync pairs with MapLeft<TResult>(Func<TLeft, TResult>), MapRightAsync with MapRight<TResult>(Func<TRight, TResult>), and MatchAsync with Match<TResult>(Func<TLeft, TResult>, Func<TRight, TResult>) - so an asynchronous pipeline over Task<Either<TLeft, TRight>> composes exactly like its synchronous equivalent.

Argument validation runs synchronously, so a null source task or delegate faults at the call site rather than on the returned task.

Every combinator preserves the uninitialized-instance contract of the underlying type: operating on default(Either<TLeft, TRight>) throws InvalidOperationException, matching the synchronous combinators.

The combinators accept no CancellationToken: they perform no I/O of their own, so cancellation belongs to the caller's delegates, which can close over their own tokens.

Methods

MapLeftAsync<TLeft, TRight, TResult>(Either<TLeft, TRight>, Func<TLeft, Task<TResult>>)

Asynchronously projects the left value using the specified asynchronous selector, passing a right value through untouched.

public static Task<Either<TResult, TRight>> MapLeftAsync<TLeft, TRight, TResult>(this Either<TLeft, TRight> source, Func<TLeft, Task<TResult>> selector)

Parameters

source Either<TLeft, TRight>

The either to project.

selector Func<TLeft, Task<TResult>>

The asynchronous projection applied to the left value when the left side is active.

Returns

Task<Either<TResult, TRight>>

A task that completes with Left(await selector(value)) when the left side is active; otherwise a right-carrying either with the original right value.

Type Parameters

TLeft

The type of the value carried on the left side.

TRight

The type of the value carried on the right side.

TResult

The left type produced by the selector.

Remarks

The selector is not invoked when the right side is active. The lift is strict: a null awaited projection result is rejected by the Left(TLeft) factory. Argument validation runs synchronously, so a null selector faults at the call site rather than on the returned task.

Exceptions

ArgumentNullException

selector is null, or the awaited projection produced null.

InvalidOperationException

The either is default(Either<TLeft, TRight>) and carries neither side.

MapLeftAsync<TLeft, TRight, TResult>(Task<Either<TLeft, TRight>>, Func<TLeft, Task<TResult>>)

Asynchronously projects the left value of the awaited either using the specified asynchronous selector, passing a right value through untouched.

public static Task<Either<TResult, TRight>> MapLeftAsync<TLeft, TRight, TResult>(this Task<Either<TLeft, TRight>> source, Func<TLeft, Task<TResult>> selector)

Parameters

source Task<Either<TLeft, TRight>>

The task producing the either to project. Must not be null.

selector Func<TLeft, Task<TResult>>

The asynchronous projection applied to the left value when the left side is active.

Returns

Task<Either<TResult, TRight>>

A task that completes with Left(await selector(value)) when the left side is active; otherwise a right-carrying either with the original right value.

Type Parameters

TLeft

The type of the value carried on the left side.

TRight

The type of the value carried on the right side.

TResult

The left type produced by the selector.

Remarks

The selector is not invoked when the right side is active. The lift is strict: a null awaited projection result is rejected by the Left(TLeft) factory. Argument validation runs synchronously, so a null argument faults at the call site rather than on the returned task.

Exceptions

ArgumentNullException

source or selector is null, or the awaited projection produced null.

InvalidOperationException

The awaited either is default(Either<TLeft, TRight>) and carries neither side.

MapLeftAsync<TLeft, TRight, TResult>(Task<Either<TLeft, TRight>>, Func<TLeft, TResult>)

Asynchronously projects the left value of the awaited either using the specified selector, passing a right value through untouched.

public static Task<Either<TResult, TRight>> MapLeftAsync<TLeft, TRight, TResult>(this Task<Either<TLeft, TRight>> source, Func<TLeft, TResult> selector)

Parameters

source Task<Either<TLeft, TRight>>

The task producing the either to project. Must not be null.

selector Func<TLeft, TResult>

The projection applied to the left value when the left side is active.

Returns

Task<Either<TResult, TRight>>

A task that completes with Left(selector(value)) when the left side is active; otherwise a right-carrying either with the original right value.

Type Parameters

TLeft

The type of the value carried on the left side.

TRight

The type of the value carried on the right side.

TResult

The left type produced by the selector.

Remarks

The selector is not invoked when the right side is active. The lift is strict, matching MapLeft<TResult>(Func<TLeft, TResult>). Argument validation runs synchronously, so a null argument faults at the call site rather than on the returned task.

Exceptions

ArgumentNullException

source or selector is null, or the projection returned null.

InvalidOperationException

The awaited either is default(Either<TLeft, TRight>) and carries neither side.

MapRightAsync<TLeft, TRight, TResult>(Either<TLeft, TRight>, Func<TRight, Task<TResult>>)

Asynchronously projects the right value using the specified asynchronous selector, passing a left value through untouched.

public static Task<Either<TLeft, TResult>> MapRightAsync<TLeft, TRight, TResult>(this Either<TLeft, TRight> source, Func<TRight, Task<TResult>> selector)

Parameters

source Either<TLeft, TRight>

The either to project.

selector Func<TRight, Task<TResult>>

The asynchronous projection applied to the right value when the right side is active.

Returns

Task<Either<TLeft, TResult>>

A task that completes with Right(await selector(value)) when the right side is active; otherwise a left-carrying either with the original left value.

Type Parameters

TLeft

The type of the value carried on the left side.

TRight

The type of the value carried on the right side.

TResult

The right type produced by the selector.

Remarks

The selector is not invoked when the left side is active. The lift is strict: a null awaited projection result is rejected by the Right(TRight) factory. Argument validation runs synchronously, so a null selector faults at the call site rather than on the returned task.

Exceptions

ArgumentNullException

selector is null, or the awaited projection produced null.

InvalidOperationException

The either is default(Either<TLeft, TRight>) and carries neither side.

MapRightAsync<TLeft, TRight, TResult>(Task<Either<TLeft, TRight>>, Func<TRight, Task<TResult>>)

Asynchronously projects the right value of the awaited either using the specified asynchronous selector, passing a left value through untouched.

public static Task<Either<TLeft, TResult>> MapRightAsync<TLeft, TRight, TResult>(this Task<Either<TLeft, TRight>> source, Func<TRight, Task<TResult>> selector)

Parameters

source Task<Either<TLeft, TRight>>

The task producing the either to project. Must not be null.

selector Func<TRight, Task<TResult>>

The asynchronous projection applied to the right value when the right side is active.

Returns

Task<Either<TLeft, TResult>>

A task that completes with Right(await selector(value)) when the right side is active; otherwise a left-carrying either with the original left value.

Type Parameters

TLeft

The type of the value carried on the left side.

TRight

The type of the value carried on the right side.

TResult

The right type produced by the selector.

Remarks

The selector is not invoked when the left side is active. The lift is strict: a null awaited projection result is rejected by the Right(TRight) factory. Argument validation runs synchronously, so a null argument faults at the call site rather than on the returned task.

Exceptions

ArgumentNullException

source or selector is null, or the awaited projection produced null.

InvalidOperationException

The awaited either is default(Either<TLeft, TRight>) and carries neither side.

MapRightAsync<TLeft, TRight, TResult>(Task<Either<TLeft, TRight>>, Func<TRight, TResult>)

Asynchronously projects the right value of the awaited either using the specified selector, passing a left value through untouched.

public static Task<Either<TLeft, TResult>> MapRightAsync<TLeft, TRight, TResult>(this Task<Either<TLeft, TRight>> source, Func<TRight, TResult> selector)

Parameters

source Task<Either<TLeft, TRight>>

The task producing the either to project. Must not be null.

selector Func<TRight, TResult>

The projection applied to the right value when the right side is active.

Returns

Task<Either<TLeft, TResult>>

A task that completes with Right(selector(value)) when the right side is active; otherwise a left-carrying either with the original left value.

Type Parameters

TLeft

The type of the value carried on the left side.

TRight

The type of the value carried on the right side.

TResult

The right type produced by the selector.

Remarks

The selector is not invoked when the left side is active. The lift is strict, matching MapRight<TResult>(Func<TRight, TResult>). Argument validation runs synchronously, so a null argument faults at the call site rather than on the returned task.

Exceptions

ArgumentNullException

source or selector is null, or the projection returned null.

InvalidOperationException

The awaited either is default(Either<TLeft, TRight>) and carries neither side.

MatchAsync<TLeft, TRight, TResult>(Either<TLeft, TRight>, Func<TLeft, Task<TResult>>, Func<TRight, Task<TResult>>)

Asynchronously collapses the either to a single value by invoking and awaiting the asynchronous branch for the active side.

public static Task<TResult> MatchAsync<TLeft, TRight, TResult>(this Either<TLeft, TRight> source, Func<TLeft, Task<TResult>> onLeft, Func<TRight, Task<TResult>> onRight)

Parameters

source Either<TLeft, TRight>

The either to collapse.

onLeft Func<TLeft, Task<TResult>>

The asynchronous projection invoked with the left value when the left side is active.

onRight Func<TRight, Task<TResult>>

The asynchronous projection invoked with the right value when the right side is active.

Returns

Task<TResult>

A task that completes with the value produced by the awaited branch.

Type Parameters

TLeft

The type of the value carried on the left side.

TRight

The type of the value carried on the right side.

TResult

The type produced by both branches.

Remarks

Exactly one branch is invoked. Argument validation runs synchronously, so a null argument faults at the call site rather than on the returned task.

Exceptions

ArgumentNullException

onLeft or onRight is null.

InvalidOperationException

The either is default(Either<TLeft, TRight>) and carries neither side.

MatchAsync<TLeft, TRight, TResult>(Task<Either<TLeft, TRight>>, Func<TLeft, Task<TResult>>, Func<TRight, Task<TResult>>)

Asynchronously collapses the awaited either to a single value by invoking and awaiting the asynchronous branch for the active side.

public static Task<TResult> MatchAsync<TLeft, TRight, TResult>(this Task<Either<TLeft, TRight>> source, Func<TLeft, Task<TResult>> onLeft, Func<TRight, Task<TResult>> onRight)

Parameters

source Task<Either<TLeft, TRight>>

The task producing the either to collapse. Must not be null.

onLeft Func<TLeft, Task<TResult>>

The asynchronous projection invoked with the left value when the left side is active.

onRight Func<TRight, Task<TResult>>

The asynchronous projection invoked with the right value when the right side is active.

Returns

Task<TResult>

A task that completes with the value produced by the awaited branch.

Type Parameters

TLeft

The type of the value carried on the left side.

TRight

The type of the value carried on the right side.

TResult

The type produced by both branches.

Remarks

Exactly one branch is invoked. Argument validation runs synchronously, so a null argument faults at the call site rather than on the returned task.

Exceptions

ArgumentNullException

source, onLeft, or onRight is null.

InvalidOperationException

The awaited either is default(Either<TLeft, TRight>) and carries neither side.

MatchAsync<TLeft, TRight, TResult>(Task<Either<TLeft, TRight>>, Func<TLeft, TResult>, Func<TRight, TResult>)

Asynchronously collapses the awaited either to a single value by invoking the branch for the active side.

public static Task<TResult> MatchAsync<TLeft, TRight, TResult>(this Task<Either<TLeft, TRight>> source, Func<TLeft, TResult> onLeft, Func<TRight, TResult> onRight)

Parameters

source Task<Either<TLeft, TRight>>

The task producing the either to collapse. Must not be null.

onLeft Func<TLeft, TResult>

The projection invoked with the left value when the left side is active.

onRight Func<TRight, TResult>

The projection invoked with the right value when the right side is active.

Returns

Task<TResult>

A task that completes with the value produced by the invoked branch.

Type Parameters

TLeft

The type of the value carried on the left side.

TRight

The type of the value carried on the right side.

TResult

The type produced by both branches.

Remarks

Exactly one branch is invoked. Argument validation runs synchronously, so a null argument faults at the call site rather than on the returned task.

Exceptions

ArgumentNullException

source, onLeft, or onRight is null.

InvalidOperationException

The awaited either is default(Either<TLeft, TRight>) and carries neither side.

Applies to

ProductVersions
.NET8, 10