replaceAllList<T> function

List<T> replaceAllList<T>(
  1. Iterable<T> it,
  2. T from, [
  3. T? to
])

Delete all occurrences, or replace with to.

Implementation

List<T> replaceAllList<T>(Iterable<T> it, T from, [T? to]) {
  if (it.isEmpty) {
    return [];
  }
  List<T> result = List.from(it);
  List<int> indicesToReplace = [];

  for (int i in nums(result.length)) {
    if (deepEquals(result[i], from)) {
      indicesToReplace.add(i);
    }
  }

  for (int i in backwardsList(indicesToReplace)) {
    to == null ? result.removeAt(i) : result[i] = to;
  }

  return result;
}