I rarely use Math.max
as nearly all of my use cases are for lists and spreading potentially hundreds to tens of thousands of arguments (depending on the data set) is obviously not a good idea. (It's also slower) Additionally, there are a few statistical functions that come up a little more than the rest.
Here's a list of things I'd like to see:
sum(list, by?): number
product(list, by?): number
min(list, by?): value
max(list, by?): value
average(list, by?): number
quantile(q, list, by?): number
median(list, by?): number
count(list): Map<value, number>
Most of these have precedent in standard utility libraries, specifically with lists:
- Lodash has the following:
- Underscore has the following:
- Ramda has the following:
My proposed count
is similar to GitHub - tc39/proposal-array-grouping: A proposal to make grouping of array items easier, but instead of returning an object of lists, it's a map of pure lengths (which is much cheaper to calculate). mode
isn't provided as it's literally as simple as max(count(list), ([k, v]) => v)[0]
.
The only one of those that isn't listed above, quantile
, is probably the most common statistic I've seen in practice after the above, and outside numerical computing contexts, is about the only one I've ever needed of the major statistical functions as it comes in handy with benchmarks and metrics as a more pragmatically useful alternative to max
(q=1
) and min
(q=0
). While technically median
(q=0.5
) could be implemented in terms of quantile
as proposed here (and really, specified in terms of it), I'm providing it as a separate function.
Yes, there's been some recent noise about this already, but I would like to hold back from actually specifying where they'd go, because it's a bit early for that.