mutateSeparated<T> function

void mutateSeparated<T>(
  1. List<T> source,
  2. List<T> separatedList,
  3. T separator
)

Implementation

void mutateSeparated<T>(List<T> source, List<T> separatedList, T separator) {
  int targetIndex = 0; // Position in separatedList we're modifying

  for (int i = 0; i < source.length; i++) {
    // Update or add source element
    if (targetIndex < separatedList.length) {
      separatedList[targetIndex] = source[i];
    } else {
      separatedList.add(source[i]);
    }
    targetIndex++;

    // Update or add separator if not the last element
    if (i < source.length - 1) {
      if (targetIndex < separatedList.length) {
        separatedList[targetIndex] = separator;
      } else {
        separatedList.add(separator);
      }
      targetIndex++;
    }
  }

  // Remove extra elements if separatedList is too long
  if (targetIndex < separatedList.length) {
    separatedList.removeRange(targetIndex, separatedList.length);
  }
}