Table of Contents

Class TraverseExtensions

Namespace
FunctionalDdd
Assembly
FunctionalDdd.RailwayOrientedProgramming.dll

Transforms a collection of items into a Result containing a collection, short-circuiting on the first failure. Useful for processing collections where each item can fail independently.

public static class TraverseExtensions
Inheritance
TraverseExtensions
Inherited Members

Examples

// Validate all items in a collection
var items = new[] { "item1", "item2", "item3" };
var result = items.Traverse(item => ValidateItem(item));

// Process collection asynchronously with cancellation
var orderIds = new[] { "order1", "order2", "order3" };
var orders = await orderIds.TraverseAsync(
    async (id, ct) => await FetchOrderAsync(id, ct),
    cancellationToken
);

// If any item fails, the entire operation fails with that error
// If all succeed, returns Success with IEnumerable of all results

Methods

TraverseAsync<TIn, TOut>(IEnumerable<TIn>, Func<TIn, CancellationToken, Task<Result<TOut>>>, CancellationToken)

Asynchronously transforms a collection of items into a Result containing all transformed items. Short-circuits on the first failure. Supports cancellation.

public static Task<Result<IEnumerable<TOut>>> TraverseAsync<TIn, TOut>(this IEnumerable<TIn> source, Func<TIn, CancellationToken, Task<Result<TOut>>> selector, CancellationToken cancellationToken = default)

Parameters

source IEnumerable<TIn>

Source collection to transform.

selector Func<TIn, CancellationToken, Task<Result<TOut>>>

Async transformation function with cancellation support.

cancellationToken CancellationToken

Cancellation token to observe.

Returns

Task<Result<IEnumerable<TOut>>>

Task producing Success with all items if all succeed; otherwise the first failure.

Type Parameters

TIn

Type of input items.

TOut

Type of output items.

Exceptions

ArgumentNullException

Thrown when source or selector is null.

TraverseAsync<TIn, TOut>(IEnumerable<TIn>, Func<TIn, CancellationToken, ValueTask<Result<TOut>>>, CancellationToken)

Asynchronously transforms a collection of items using ValueTask with cancellation support. Short-circuits on the first failure.

public static ValueTask<Result<IEnumerable<TOut>>> TraverseAsync<TIn, TOut>(this IEnumerable<TIn> source, Func<TIn, CancellationToken, ValueTask<Result<TOut>>> selector, CancellationToken cancellationToken = default)

Parameters

source IEnumerable<TIn>

Source collection to transform.

selector Func<TIn, CancellationToken, ValueTask<Result<TOut>>>

Async transformation function with cancellation support.

cancellationToken CancellationToken

Cancellation token to observe.

Returns

ValueTask<Result<IEnumerable<TOut>>>

ValueTask producing Success with all items if all succeed; otherwise the first failure.

Type Parameters

TIn

Type of input items.

TOut

Type of output items.

Exceptions

ArgumentNullException

Thrown when source or selector is null.

TraverseAsync<TIn, TOut>(IEnumerable<TIn>, Func<TIn, Task<Result<TOut>>>)

Asynchronously transforms a collection of items into a Result containing all transformed items. Short-circuits on the first failure.

public static Task<Result<IEnumerable<TOut>>> TraverseAsync<TIn, TOut>(this IEnumerable<TIn> source, Func<TIn, Task<Result<TOut>>> selector)

Parameters

source IEnumerable<TIn>

Source collection to transform.

selector Func<TIn, Task<Result<TOut>>>

Async transformation function returning a Result.

Returns

Task<Result<IEnumerable<TOut>>>

Task producing Success with all items if all succeed; otherwise the first failure.

Type Parameters

TIn

Type of input items.

TOut

Type of output items.

Exceptions

ArgumentNullException

Thrown when source or selector is null.

TraverseAsync<TIn, TOut>(IEnumerable<TIn>, Func<TIn, ValueTask<Result<TOut>>>)

Asynchronously transforms a collection of items using ValueTask for zero-allocation scenarios. Short-circuits on the first failure.

public static ValueTask<Result<IEnumerable<TOut>>> TraverseAsync<TIn, TOut>(this IEnumerable<TIn> source, Func<TIn, ValueTask<Result<TOut>>> selector)

Parameters

source IEnumerable<TIn>

Source collection to transform.

selector Func<TIn, ValueTask<Result<TOut>>>

Async transformation function returning a Result.

Returns

ValueTask<Result<IEnumerable<TOut>>>

ValueTask producing Success with all items if all succeed; otherwise the first failure.

Type Parameters

TIn

Type of input items.

TOut

Type of output items.

Exceptions

ArgumentNullException

Thrown when source or selector is null.

Traverse<TIn, TOut>(IEnumerable<TIn>, Func<TIn, Result<TOut>>)

Transforms a collection of items into a Result containing all transformed items. Short-circuits on the first failure.

public static Result<IEnumerable<TOut>> Traverse<TIn, TOut>(this IEnumerable<TIn> source, Func<TIn, Result<TOut>> selector)

Parameters

source IEnumerable<TIn>

Source collection to transform.

selector Func<TIn, Result<TOut>>

Transformation function returning a Result.

Returns

Result<IEnumerable<TOut>>

Success with all items if all succeed; otherwise the first failure.

Type Parameters

TIn

Type of input items.

TOut

Type of output items.

Exceptions

ArgumentNullException

Thrown when source or selector is null.