withoutLast method

Iterable<T> withoutLast()

Lazily returns all values without the last one.

Example:

[1, 2, 3].withoutLast(); // [1, 2]
[].withoutLast(); // [];

Implementation

Iterable<T> withoutLast() sync* {
  final hasFirst = iterator.moveNext();
  if (!hasFirst) return;
  while (true) {
    final value = iterator.current;
    final isLastOne = !iterator.moveNext();
    if (!isLastOne) {
      yield value;
    } else {
      break;
    }
  }
}