average property

num average

Returns the average (mean) of all values in this.

this is only enumerated once.

Implementation

num get average {
  var count = 0;
  num runningSum = 0;
  for (var value in this) {
    count++;
    runningSum += value;
  }

  if (count == 0) {
    throw ArgumentError(
      'Cannot calculate the average of an empty collection.',
    );
  }

  return runningSum / count;
}