Comments (7)
In mathematics, inf, sup and argmax have very precise definitions (which don't match your test cases above).
Math.minIndex
and Math.maxIndex
are much more analogous to Math.min
and Math.max
. This naming is also consistent with Array.prototype.find
and Array.prototype.findIndex
.
from proposal-math-extensions.
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).
from proposal-math-extensions.
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 Math
object? Wouldn't defining it on arrays make it more versatile?
[4, 2, 4, 2].minIndex() === 1
[4, 2, 4, 2].reverse().minIndex() === 0
I often use position_min()
and position_max()
in Rust, which are defined in a similar manner and are part of the popular Itertools
crate.
from proposal-math-extensions.
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 Array.prototype
object rather than functions on the Math
object.
from proposal-math-extensions.
Why should they be methods on Array.prototype
Hi @ljharb! I think of it as a function that perhaps should work on more than just Number
and on any value that can be compared to another:
let names = ["a", "d", "c", "b"].maxIndex(); // 1 ("d"), as its code point is U+0044
Rust's Itertools
, as precedent, implements it in a similar manner:
use itertools::Itertools;
["a", "d", "b", "c"].iter().position_max() // Some(1)
Array.prototype
is also the first place I would look for a method that returns an index, akin to:
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 Math.maxIndex()
and think it would work better for the imperative and functional approach, especially with larger chains in case of the latter.
from proposal-math-extensions.
// 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
from proposal-math-extensions.
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?
from proposal-math-extensions.
Related Issues (20)
- [Suggestion] Methods to be added HOT 8
- [Suggestion] Math.mean and Math.roundBy HOT 2
- [Suggestion] Constants to be added HOT 1
- Is it still active? HOT 7
- [Suggestion] Math.lerp and Math.normalize HOT 2
- alternatives to Math.radians and Math.degrees HOT 5
- Math.exp2
- modulo function HOT 8
- Math.pow should accept Bigints, modulo and pow=-1 HOT 12
- Proposal Math.clamp separately HOT 1
- In the spec draft missed conversion of arguments to numbers
- Repository should be transferred to tc39 organization HOT 2
- Should `factorial` be defined in all or some domains? HOT 2
- Dot product?
- New rounding modes HOT 1
- Bitwise methods HOT 4
- Tuple methods
- `Number.prototype.toByteSize` / `Intl.SizeFormat` HOT 2
- `Math.copysign`? HOT 1
- `Math.MAX_RANDOM_BITS`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from proposal-math-extensions.