stretchListToLength<T> function

List<T> stretchListToLength<T>(
  1. List<T> list,
  2. int length
)

Implementation

List<T> stretchListToLength<T>(List<T> list, int length) {
  if (list.isEmpty) {
    return list;
  }

  var currentLength = list.length;

  if (currentLength > length) {
    throw 'Trying to stretch an array to a length shorter than its own';
  }

  var indices =
      range(end: length).map((e) => e.toDouble() / length * currentLength);

  return [for (var i in indices) list[i.toInt()]];
}