# Built-in module for array-oriented math functions?

**URL:** <https://es.discourse.group/t/built-in-module-for-array-oriented-math-functions/1121>\
**Category:** 💡 Ideas\
**Created:** [December 12, 2021, 10:23am UTC](https://es.discourse.group/t/built-in-module-for-array-oriented-math-functions/1121 "2021-12-12T10:23:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![claudiameadows](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudiameadows/32/126_2.png) [@claudiameadows](https://es.discourse.group/u/claudiameadows)\
**Post date:** [December 12, 2021, 10:23am UTC](https://es.discourse.group/t/built-in-module-for-array-oriented-math-functions/1121/1 "2021-12-12T10:23:45Z")

</div>

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:
  - `sum` → [`_.sum`](https://lodash.com/docs/4.17.15#sum)
  - `min` → [`_.min`](https://lodash.com/docs/4.17.15#min)
  - `max` → [`_.max`](https://lodash.com/docs/4.17.15#max)
  - `average` → [`_.mean`](https://lodash.com/docs/4.17.15#mean)
  - `count` → [`_.countBy`](https://lodash.com/docs/4.17.15#countBy)

- Underscore has the following:
  - `min` → [`_.min`](https://underscorejs.org/#min)
  - `max` → [`_.max`](https://underscorejs.org/#max)
  - `count` → [`_.countBy`](https://underscorejs.org/#countBy)

- Ramda has the following:
  - `sum` → [`R.sum`](https://ramdajs.com/docs/#sum)
  - `product` → [`R.product`](https://ramdajs.com/docs/#product)
  - `min` → [`R.min`](https://ramdajs.com/docs/#min)
  - `max` → [`R.max`](https://ramdajs.com/docs/#max)
  - `average` → [`R.mean`](https://ramdajs.com/docs/#mean)
  - `median` → [`R.median`](https://ramdajs.com/docs/#median)
  - `count` → [`R.countBy`](https://ramdajs.com/docs/#countBy)

My proposed `count` is similar to [GitHub - tc39/proposal-array-grouping: A proposal to make grouping of array items easier](https://github.com/tc39/proposal-array-grouping), 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.

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [December 12, 2021, 4:50pm UTC](https://es.discourse.group/t/built-in-module-for-array-oriented-math-functions/1121/2 "2021-12-12T16:50:20Z")

</div>

In case they weren’t mentioned upthread (this showed up in my email as a stand-alone email instead of part of a thread of replies, for some reason) there’s a bunch of open proposals for Math methods£

- [https://github.com/rwaldron/proposal-math-extensions](https://github.com/rwaldron/proposal-math-extensions)
- [https://github.com/tc39/proposal-Math.signbit](https://github.com/tc39/proposal-Math.signbit)
- [https://docs.google.com/presentation/d/1Ta\_IbravBUOvu7LUhlN49SvLU-8G8bIQnsS08P3Z4vY/edit?usp=sharing](https://docs.google.com/presentation/d/1Ta_IbravBUOvu7LUhlN49SvLU-8G8bIQnsS08P3Z4vY/edit?usp=sharing)
- [https://github.com/tc39/proposal-seeded-random](https://github.com/tc39/proposal-seeded-random)
- [https://github.com/phoddie/integer-and-modulus-math-proposal](https://github.com/phoddie/integer-and-modulus-math-proposal)
- [https://github.com/tc39/proposal-bigint-math](https://github.com/tc39/proposal-bigint-math)

The subject/title mentions “built-in module”, and altho i think those advancing is unlikely, it’s probably best to not derail the thread discussing where it might live, since that’s not really relevant until stage 2.

---

<div class="post-metadata">

**Author:** ![Jonas\_Wilms](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jonas_wilms/32/189_2.png) [@Jonas\_Wilms](https://es.discourse.group/u/Jonas_Wilms)\
**Post date:** [December 14, 2021, 10:10am UTC](https://es.discourse.group/t/built-in-module-for-array-oriented-math-functions/1121/3 "2021-12-14T10:10:27Z")

</div>

I definitely agree that these utilities would be really useful (especially `average` and `sum`, hate to use `reduce` for that). One comment on:

> [@claudiameadows](#):
>
> 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.

That should be a trivial optimization for the engine, as Math.max is probably an intrinsic anyway. I'd rather say this is a bug / missed opportunity in the engine and not something the language specification should care about. I do however see that the additional arguments (namely the "by") will be very convenient, so I do see good reasons not to use a rest parameter on the new functions.

Instead of "list" I'd rather use the specified "iterable" ...
