nth method

  1. @override
Option<T> nth(
  1. int n
)

Returns the nth element of the iterator. Like most indexing operations, the count starts from zero, so nth(0) returns the first value, nth(1) the second, and so on. nth() will return None if n is greater than or equal to the length of the iterator.

Implementation

@override
Option<T> nth(int n) {
  if (n < 0) {
    return None;
  }
  var index = 0;
  for (final element in this) {
    if (index == n) {
      return Some(element);
    }
    index++;
  }
  return None;
}