firstE method
Returns the first element in the enumerable, optionally matching a specified condition.
Begins iteration of the enumerable, but then returns the first element
found that matches the specified condition
. If condition
is omitted,
firstE will return the first element of the enumerable.
If the enumerable is empty, an EmptyEnumerableError will be thrown. If
condition
is provided but iteration reaches the end of the enumerable
before an element is found, an ElementNotFoundError will be thrown.
If condition
is provided, the firstE method short-circuits the first
time condition
returns true
and will not iterate further over the
enumerable. In the worst case, it will iterate over the entire enumerable.
If condition
is omitted, the firstE method will always only visit the
first element in the enumerable, making it run in constant time
regardless of iteration length.
Implementation
T firstE({Condition<T> condition}) {
final iterator = this.iterator;
if (!iterator.moveNext()) throw EmptyEnumerableError();
if (condition == null) return iterator.current;
do {
if (condition(iterator.current)) return iterator.current;
} while (iterator.moveNext());
throw ElementNotFoundError();
}