singleOrOption method

Option<T> singleOrOption()

Returns the single element of an iterator, None if this is empty or has more than one element.

Implementation

@pragma('vm:prefer-inline')
Option<T> singleOrOption() {
  final firstTwo = take(2).iterator;
  if (!firstTwo.moveNext()) {
    return None;
  }
  final first = firstTwo.current;
  if (!firstTwo.moveNext()) {
    return Some(first);
  }
  return None;
}