Skip to content
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

Added lastMaxBy function to stdlib #3092

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

acefalobi
Copy link

@acefalobi acefalobi commented Feb 17, 2020

This is a fix to the problem brought up in this issue

This commit adds a new standard library function lastMaxBy that replicates the functionality of maxBy, but returns the last maximum element instead of the last. It also adds new tests and samples for this function

@acefalobi acefalobi requested review from ilya-g and qurbonzoda and removed request for ilya-g February 17, 2020 10:50
@ilya-g
Copy link
Member

ilya-g commented Feb 17, 2020

We had a discussion about the similar problem here: https://discuss.kotlinlang.org/t/should-max-maxby-maxwith-return-the-last-largest-element/2139/11
However we haven't found so far a use case motivating enough to change max function behavior or introduce overloads like lastMax(By).

@acefalobi
Copy link
Author

acefalobi commented Feb 17, 2020

I think a motivating use case would be like the one mentioned in the issue where you have a list sorted by a predicate and wanted to get the maximum element of the list by another predicate.

If I had a list [(C, 2), (A, 1), (B, 2)] and then sorted by the first parameter, I would get [(A, 1), (B, 2), (C, 2)]. Running maxBy on the new sorted list with the second parameter would return (B, 2), even though the desired result would be to keep the previous order and return the maximum element in respect to the previous sort which would be (C, 2).

@ilya-g
Copy link
Member

ilya-g commented Feb 18, 2020

Sorry, I don't get how lastMaxBy would help here. The first sort could have reordered elements in an arbitrary way, so if (C, 2) is the desired result, it could be either the first maximum, or the last one, or neither of those in that sorted list.

Also using the elements in the motivating example like (A, 1) doesn't help either. Is your real use case about sorting pairs of letters and numbers?

I suggest to move the discussion about use cases to that forum thread, since it isn't related to the code in this PR directly, and in order to get more opinions there.

@acefalobi
Copy link
Author

Moving to the discussion thread then

@acefalobi
Copy link
Author

@ilya-g seems like the discussion is inconclusive. Should I close the PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants