average<T> static method

double average<T>(
  1. Iterable<T> list, {
  2. num by(
    1. T
    )?,
})

Returns the arithmetic mean of list, or 0 when empty.

Implementation

static double average<T>(Iterable<T> list, {num Function(T)? by}) {
  if (list.isEmpty) return 0;
  var total = 0.0;
  var count = 0;
  for (final v in list) {
    total += by != null ? by(v) : (v as num);
    count++;
  }
  return total / count;
}