first method

T first([
  1. bool predicate(
    1. T
    )?
])

Returns first element.

Use predicate to return the first element matching the given predicate

@throws NoSuchElementException if the collection is empty.

Implementation

T first([bool Function(T)? predicate]) {
  if (predicate == null) {
    final i = iterator();
    if (!i.hasNext()) {
      throw const NoSuchElementException("Collection is empty");
    }
    return i.next();
  } else {
    for (final element in iter) {
      if (predicate(element)) return element;
    }
    throw const NoSuchElementException(
        "Collection contains no element matching the predicate.");
  }
}