Skip to content

argmax and argmin (or max_by and min_by) #7430

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

Closed
DartBot opened this issue Dec 15, 2012 · 4 comments
Closed

argmax and argmin (or max_by and min_by) #7430

DartBot opened this issue Dec 15, 2012 · 4 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. closed-duplicate Closed in favor of an existing report type-enhancement A request for a change that isn't a bug

Comments

@DartBot
Copy link

DartBot commented Dec 15, 2012

This issue was originally filed by [email protected]


Link to StackOverflow question:
http://stackoverflow.com/questions/13884666/any-max-and-argmax

I would like Collection#argmax and argmin. Examples of where these might be useful:
final animalNames = ['cat', 'turtle', 'sheep'];
final longest = animalNames.argmax((name) => name.length);
// longest == 'turtle'

final leastHealth = monsters.argmin((monster) => monster.health);

These are the same as Ruby max_by and min_by:
http://ruby-doc.org/core-1.9.3/Enumerable.html#method-i-max_by
They are also very commonly used in mathematics:
http://en.wikipedia.org/wiki/Arg_max

@iposva-google
Copy link
Contributor

Removed Type-Defect label.
Added Type-Enhancement, Area-Library, Triaged labels.

@DartBot
Copy link
Author

DartBot commented Dec 17, 2012

This comment was originally written by @seaneagan


this is very similar to issue #1319

@DartBot
Copy link
Author

DartBot commented Dec 17, 2012

This comment was originally written by [email protected]


I think you might be right. They should be merged. The lack of examples and my misunderstanding of what happens when max has no parameters confused me. Are the following examples correct:
[1, 2, 3].max(); // -> 3
[1, 2, 3].max((e) => (e == 2) ? 4 : e); // -> 2
['cat', 'monkey', 'sheep'].max(); // -> sheep
['cat', 'monkey', 'sheep'].max((e) => e.length); // -> 'monkey'
monsters.max() // -> depends on what > is overloaded to mean
monsters.max((e) => e.health); // -> monster with most health

Is it that [1, 2, 3].max() returns a value (3) but [1, 2, 3].max((e) => (e == 2) ? 4 : e) returns the argument that mapped to the highest value?

@lrhn
Copy link
Member

lrhn commented Dec 18, 2012

I don't think we want a specific argmin/argsort that works direcly given a projection function, but I do think we should have min/max on Iterable, and possibly a helper for creating comparison functions from projections so you can write:

  list.min(Comparable.compareBy((m) => m.health))

which would be equivalent to

  list.min((a, b) => a.health.compareTo(b.health))

which would find the element in list with the lowest health.


Added Duplicate label.
Marked as being merged into #1319.

@DartBot DartBot added Type-Enhancement area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. closed-duplicate Closed in favor of an existing report labels Dec 18, 2012
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed type-enhancement labels Mar 1, 2016
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. closed-duplicate Closed in favor of an existing report type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants