Attempt - Kalkwst/Risotto GitHub Wiki
Asserts that all elements of the sequence are handled properly, otherwise throws an Exception
In this page
- Definition
- Overloads
- Attempt(Func<TSource, bool> fn)
- Attempt(Func<TSource, bool> fn, Func<TSource, Exception> errorHandler)
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
|
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);
TSource
The type of the elements of source
.
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.
IEnumerable <TSource>
the original sequence.
One or more elements in the sequence do not pass the fn
validation
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
Enumeration is terminated as soon as an invalid element is found.
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);
TSource
The type of the elements of source
.
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.
IEnumerable <TSource>
the original sequence.
One or more elements in the sequence do not pass the fn
validation
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