Fix Enum.min/2,3 and Enum.max/2,3 with a custom sorter on ranges - #15603
Merged
Conversation
josevalim
reviewed
Jul 14, 2026
The range clause of `aggregate/3` only compared the two endpoints of the
range, which is correct solely for sorters consistent with the natural
integer order. Any other comparator returned a different result than
the identical call on the equivalent list or stream:
sorter = fn a, b -> rem(a, 5) >= rem(b, 5) end
Enum.max(1..10, sorter) #=> 1, expected 4
Enum.max(Enum.to_list(1..10), sorter) #=> 4
Once the range is known to be non-empty, take the endpoint shortcut
only when the sorter is one of the defaults (`&>=/2` or `&<=/2`, both
order-consistent); any other sorter traverses the elements through
`reduce_range/5`, seeded with the first element.
pnezis
force-pushed
the
fix-enum-min-max-range-sorter
branch
from
July 14, 2026 09:03
93e10a8 to
b70b46f
Compare
Member
|
💚 💙 💜 💛 ❤️ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The range clause of
aggregate/3only compared the two endpoints of the range, which is correct solely for sorters consistent with the natural integer order. Any other comparator returned a different result than the identical call on the equivalent list or stream:Only take the endpoint shortcut when the sorter is the default one (
&>=/2for max,&<=/2for min); any custom sorter now reduces over the range elements like lists and streams do.