quantileSorted function Summarizing data

num? quantileSorted(
  1. Iterable<num?> values,
  2. num p
)

Similar to quantile, but expects the input to be sorted.

Implementation

num? quantileSorted(Iterable<num?> values, num p) {
  var n = values.length;
  if (n == 0 || p.isNaN) return null;
  if (p <= 0 || n < 2) return values.elementAt(0);
  if (p >= 1) return values.elementAt(n - 1);
  var i = (n - 1) * p,
      i0 = i.floor(),
      value0 = nanIfNull(values.elementAt(i0)),
      value1 = nanIfNull(values.elementAt(i0 + 1));
  return value0 + (value1 - value0) * (i - i0);
}