insertBetween method

Iterable<T> insertBetween(
  1. T element
)

Inserts the specified element between each element of the iterable.

If the iterable has only one element, or is empty, it will return the iterable unchanged.

Example:

final list = [1, 2, 3];
final result = list.insertBetween(0); // [1, 0, 2, 0, 3]

Implementation

Iterable<T> insertBetween(T element) sync* {
  final iterator = this.iterator;

  if (!iterator.moveNext()) return;

  yield iterator.current;

  while (iterator.moveNext()) {
    yield element;
    yield iterator.current;
  }
}