intercalateList<T> function

List<T> intercalateList<T>(
  1. Iterable<T> sub,
  2. Iterable<Iterable<T>> it
)

Insert element between other elements and concatenate.

Implementation

List<T> intercalateList<T>(Iterable<T> sub, Iterable<Iterable<T>> it) {
  List<List<T>> original = [];
  for (var v in it) {
    original.add(List.from(v));
  }

  if (original.isEmpty) {
    return [];
  }
  if (original.length == 1) {
    return original.first;
  }

  List<T> input = List.from(sub);

  List<T> result = [];
  for (int i in nums(original.length - 1)) {
    result = result + original[i] + input;
  }
  result += original.last;

  return result;
}