[CLJ-997] max-key and min-key to return the first entry in case of several candidates Created: 18/May/12 Updated: 18/May/12
|Affects Version/s:||Release 1.3, Release 1.4|
Consider the following code:
(def values [[1 2] [3 4]  [6 7] ])
Which returns the last max entry [6 7]. Why its not the first max entry [1 2]?
Consider the following example in Scala:
println(List(List(0, 1, 2), List(2, 3, 4), List(1), List(1, 2, 3)).maxBy(_.length))
The very same function in Scala returns the first max entry (by default).
The code from relase 1.4 file "clojure/core.clj#4419-4426" looks is:
I am unsure what is the motivation in returning the last candidate, but
I suggest two following things:
1. Make "max-key" and "min-key" return the first max/min entry if there are several candidates.
This behavior seems more natural/convenient (to me), because in most cases you want to get first "winner".
Line #4424 should have ">=" instead of ">".
2. Make "max-key" and "min-key" make warranty on order, which max/min entry will be returned (either first or last).
Line #4420 should say "Returns the x for which (k x), a number, is greatest. In case of several matches, the first max entry will be returned" or the same doc, but saying "the last max entry will returned".
|Comment by Steve Miner [ 18/May/12 4:03 PM ]|
The current behavior matches the documentation so I wouldn't consider it a "defect". There doesn't seem to be a compelling reason to impose a tie-breaker rule on the implementation.