genericJoin<T> function Null safety

List<T> genericJoin<T>(
  1. List<T> list,
  2. T seperator
)

Method which joins seperator in between the items in list.

Does the same as the Iterable.join() method but instead of returning a string, retuns the list with seperators in between.

Implementation

List<T> genericJoin<T>(List<T> list, T seperator) {
  var out = <T>[];
  var iterator = list.iterator;

  if (!iterator.moveNext()) return out;

  out.add(iterator.current);
  while (iterator.moveNext()) {
    out.add(seperator);
    out.add(iterator.current);
  }
  return out;
}