Code Monkey home page Code Monkey logo

Comments (7)

 avatar commented on July 20, 2024 2

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.

ljharb avatar ljharb commented on July 20, 2024 2

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.

imjasonmiller avatar imjasonmiller commented on July 20, 2024 1

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.

Andrew-Cottrell avatar Andrew-Cottrell commented on July 20, 2024 1

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.

imjasonmiller avatar imjasonmiller commented on July 20, 2024 1

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.

caub avatar caub commented on July 20, 2024
// 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.

ljharb avatar ljharb commented on July 20, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.