-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Math.{minIndex, maxIndex} #3
Comments
In mathematics, inf, sup and argmax have very precise definitions (which don't match your test cases above).
|
// 1 year.. still no Math.maxIndex in sight..
const maxI = a => { const x=Math.max(...a); return a.indexOf(x) };
// or const maxI = a => a.reduce((i, _, j) => a[j]>a[i]?j:i, 0);
// I leave this comment for people in 2025 :p |
I'd (still) love to have this functionality as well, but as a small nit, it seems somewhat of a constraint to me to define this directly on the [4, 2, 4, 2].minIndex() === 1
[4, 2, 4, 2].reverse().minIndex() === 0 I often use |
These implementations make a single pass through the array function indexOfMax(array) {
return array.reduce((a, v, i) => 1 in a && v <= a[1] ? a : [i, v], [-1])[0];
}
function indexOfMin(array) {
return array.reduce((a, v, i) => 1 in a && v >= a[1] ? a : [i, v], [-1])[0];
}
function lastIndexOfMax(array) {
return array.reduceRight((a, v, i) => 1 in a && v <= a[1] ? a : [i, v], [-1])[0];
}
function lastIndexOfMin(array) {
return array.reduceRight((a, v, i) => 1 in a && v >= a[1] ? a : [i, v], [-1])[0];
} Examples indexOfMax( [] ); // -1
indexOfMax( [ 4, 2, 4, 2 ] ); // 0
indexOfMax( [ 1n, 1n, 4n ] ); // 2
indexOfMax( [ "a", "b", "c" ] ); // 2
indexOfMin( [] ); // -1
indexOfMin( [ 4, 2, 4, 2 ] ); // 1
indexOfMin( [ 4n, 1n, 4n ] ); // 1
indexOfMin( [ "x", "a", "z" ] ); // 1 I'm not convinced it is worth these being built-in; they could easily be made available in a JavaScript library. If they are to be built-in, I suggest they be methods on the |
Why should they be methods on Array.prototype, when Math.max/Math.min already establishes the precedent for a Math function that takes a list of arguments? |
Hi @ljharb! I think of it as a function that perhaps should work on more than just let names = ["a", "d", "c", "b"].maxIndex(); // 1 ("d"), as its code point is U+0044 Rust's use itertools::Itertools;
["a", "d", "b", "c"].iter().position_max() // Some(1)
Next to that, I personally find it more ergonomic and easier on the eyes when chaining, e.g: let values = [ { re: 3, im: 4 }, { re: 8, im: 0 }, { re: 6, im: 8 } ];
const maxAbsIndex = (values) =>
Math.maxIndex(values.map(({ re, im }) => Math.hypot(re, im)));
const maxAbsIndex = (values) =>
values.map(({ re, im }) => Math.hypot(re, im)).maxIndex(); I'm not a big fan of the extra level of nesting that comes with |
Rust appears to implement it on iterators, not arrays (based on your example) It’s a good point that being indexes implies it’s for arrays and nothing else (even generic iterators don’t really have indexes). |
Something I often need is to get the index of a minimum in an array, not especially its value
The text was updated successfully, but these errors were encountered: