The Wire · Showcase
ELIXIR FIXES ENUM SORTER BUG THAT BROKE CUSTOM COMPARATORS
By RepoJournal · Filed · About Elixir & Phoenix
Enum.min and Enum.max were silently returning wrong results when you passed a custom sorter on ranges, and the core team just caught it.
The bug [1] is subtle but brutal: range comparisons only looked at endpoints, which works fine for natural ordering but fails completely with custom sorters. Pass `Enum.max(1..10, fn a, b -> rem(a, 5) >= rem(b, 5) end)` and you'd get 1 instead of 4. The fix now applies the custom sorter properly across the full range. Separately, three smaller but important fixes landed overnight. Duration specs [2] were incorrectly typed to forbid negative microsecond values, even though the module's own doctests produce them. Map.from_struct [4] had a typecheck issue that's now resolved. IEx completion [3] was crashing when maps contained non-atom keys, a common pattern that should never blow up the REPL.
Action items
- → Review any code using Enum.min/max with custom sorters on ranges - results may have been wrong elixir-lang/elixir [plan]
- → Upgrade Elixir when the next patch lands to get the Duration spec fix and IEx stability improvements elixir-lang/elixir [monitor]
References
- [1] Fix `Enum.min/2,3` and `Enum.max/2,3` with a custom sorter on ranges ↗ elixir-lang/elixir
- [2] Fix `Duration` specs to allow negative microsecond values (#15611) elixir-lang/elixir
- [3] Fix IEx completion crash on maps with non-atom keys (#15612) elixir-lang/elixir
- [4] Fix `Map.from_struct/1` typecheck ↗ elixir-lang/elixir