addAround<V> function

List<V> addAround<V>(
  1. List<V> list,
  2. Generator<V> generator
)

Returns new list with items generated around list

final list = ['😁','😒','😊'];
final newList = addAround(list, (index,previous,next) => '🍨');
print(newList);//['🍨','😁','🍨','😒','🍨','😊','🍨']

Implementation

List<V> addAround<V>(List<V> list, Generator<V> generator) {
  if (list.isEmpty) return <V>[];
  final int length = list.length * 2 + 2;
  final List<V> newList = <V>[];
  for (int i = 1, j = 0; i < length; i++) {
    if (i.isOdd) {
      newList.add(generator(i, previous<V>(list, j), next<V>(list, j)));
      continue;
    }
    newList.add(list[j++]);
  }
  return newList;
}