Attempt - Kalkwst/Risotto GitHub Wiki

Definition

Asserts that all elements of the sequence are handled properly, otherwise throws an Exception

In this page

Overloads

Overload Description
Attempt(Func<TSource, bool> fn) Asserts that all elements of the sequence are handled properly, otherwise throws an InvalidOperationException
Attempt(Func<TSource, bool> fn, Func<TSource, Exception> errorHandler) Asserts that all elements of the sequence are handled properly, otherwise throws an Exception provided by the errorHandler

Attempt(Func<TSource, bool> fn)

Asserts that all elements of the sequence are handled properly, otherwise throws an InvalidOperationException

public static IEnumerable<TSource> Attempt<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> fn);

Type Parameters

TSource
The type of the elements of source.

Parameters

source IEnumerable <TSource>
A sequence of values to determine if they are all valid.

fn Func <TSource, bool>
A predicate function that asserts an element of the source sequence for a condition.

Returns

IEnumerable <TSource>

the original sequence.

Exceptions

InvalidOperationException

One or more elements in the sequence do not pass the fn validation

Example

The following code example demonstrates how to use Attempt(Func<TSource, bool> fn) to determine whether all of the elements in a sequence are handled properly.

var source = new int[] { 3, 9, 27, 81, 243 };
var asserted = source.Attempt(x => x % 3 == 0);
//=> int[]{ 3, 9, 27, 81, 243 }
var source = new int[] { 2, 4, 6, 7, 8 };
source.Attempt(x => x % 2 == 0).Purge();
//=> throws InvalidOperationException

Remarks

Enumeration is terminated as soon as an invalid element is found.

Attempt(Func<TSource, bool> fn, Func<TSource, Exception> errorHandler)

Asserts that all elements of the sequence are handled properly, otherwise throws an Exception provided by the errorHandler.

Attempt(Func<TSource, bool> fn, Func<TSource, Exception> errorHandler);

Type Parameters

TSource
The type of the elements of source.

Parameters

source IEnumerable <TSource>
A sequence of values to determine if they are all valid.

fn Func <TSource, bool>
A predicate function that asserts an element of the source sequence for a condition.

errorHandler Func <TSource, Exception>
A predicate function that returns the Exception to be thrown.

Returns

IEnumerable <TSource>

the original sequence.

Exceptions

Exception

One or more elements in the sequence do not pass the fn validation

Example

The following code example demonstrates how to use Attempt(Func<TSource, bool> fn, Func<TSource, Exception> errorHandler) to determine whether all of the elements in a sequence are handled properly.

var sequence = new int[] { 2, 4, 6, 7, 8 };
source.Attempt(x => x % 2 == 0, invalid => new InvalidValueException(invalid)).Purge();
//=> throws InvalidValueException
⚠️ **GitHub.com Fallback** ⚠️