updated method

  1. @override
IList<A> updated(
  1. int index,
  2. A elem
)
override

Returns a new collection with the element at index replaced by elem.

Throws RangeError if index is out of bounds.

Implementation

@override
IList<A> updated(int index, A elem) {
  var i = 0;
  var current = this;
  final prefix = builder<A>();

  while (i < index && current.nonEmpty) {
    i += 1;
    prefix.addOne(current.head);
    current = current.tail;
  }

  if (i == index && current.nonEmpty) {
    return prefix.prependToList(Cons(elem, current.tail));
  } else {
    throw RangeError('$index is out of bounds (min 0, max ${length - 1})');
  }
}