quantileIndex, medianIndex by Fil · Pull Request #159 · d3/d3-array
It feels a little unusual to me that quantileIndex (and by extension medianIndex) returns a two-element array of indexes rather than a single index. I understand why that is, but I think another reasonable interpretation would be to return only the first index, and then quantileIndex would be consistent with the other index methods. And secondly, knowing both indexes isn’t enough to compute the value returned by median or quantile using the R-7 method since you’d still need to the fractional component (i - i0). So how would you feel about just returning the first index?
Something like:
function quantileIndex(values, p, valueof) { values = Float64Array.from(numbers(values, valueof)); if (!(n = values.length)) return; if ((p = +p) <= 0 || n < 2) return minIndex(values); if (p >= 1) return maxIndex(values); var n, i = (n - 1) * p, i0 = Math.floor(i), order = (i, j) => ascending(values[i], values[j]); return max(quickselect(Uint32Array.from(values, (_, i) => i), i0, 0, n - 1, order).subarray(0, i0 + 1), order); }