Method SingleOrNone
- Namespace
- SodaFlow.Functional
- Assembly
- SodaFlow.Functional.dll
SingleOrNone<T>(IEnumerable<T>?)
Returns the only element of a sequence, if it has one.
[Pure]
public static Maybe<T> SingleOrNone<T>(this IEnumerable<T>? source)
Parameters
sourceIEnumerable<T>The sequence to read. A null sequence counts as empty.
Returns
- Maybe<T>
A Maybe<T> containing the single element, and one containing no value if the sequence is empty.
Type Parameters
TThe type of the values in the sequence.
Remarks
This throws where SingleOrDefault<TSource>(IEnumerable<TSource>) throws, and for the same cause: a sequence with two elements did not fail to give an answer. It contradicts the assumption that there was only one to give. A result with no value hides that. Where the caller needs more than one, and simply does not wanted, filter first, or use FirstOrNone<T>(IEnumerable<T>?). Reads at most two elements of the source.
Exceptions
- InvalidOperationException
The sequence contains more than one element.
SingleOrNone<T>(IEnumerable<T>?, Func<T, bool>)
Returns the only element of a sequence which satisfies a predicate, if there is one.
[Pure]
public static Maybe<T> SingleOrNone<T>(this IEnumerable<T>? source, Func<T, bool> predicate)
Parameters
sourceIEnumerable<T>The sequence to search. A null sequence counts as empty.
predicateFunc<T, bool>The condition the element must satisfy.
Returns
- Maybe<T>
A Maybe<T> containing the single matching element, and one containing no value if there is none.
Type Parameters
TThe type of the values in the sequence.
Exceptions
- InvalidOperationException
More than one element satisfies
predicate.