insert2 method

Iterable<T> insert2(
  1. int index,
  2. T v1,
  3. T v2
)

Inserts two elements into the iterable at the specified index.

Takes the specified elements and inserts them into the iterable at the position index.

If index is less than zero, an ArgumentError is thrown.

If iteration of this iterable is exausted before the position index is reached, a RangeError is thrown.

Implementation

Iterable<T> insert2(int index, T v1, T v2) sync* {
  if (index == 0) {
    yield* prepend2(v1, v2);
    return;
  }
  if (index < 0) {
    throw ArgumentError('Parameter "index" must be greater than zero.');
  }

  final iterator = this.iterator;
  var i = 0;

  while (iterator.moveNext()) {
    if (i == index) {
      yield v1;
      yield v2;
    }
    yield iterator.current;
    i++;
  }

  if (index >= i) {
    throw RangeError.index(index, this, null, null, i);
  }
}