Clojure JIRAhttp://dev.clojure.org/jira
This file is an XML representation of an issueen-us4.464925-07-2011[CLJ-997] max-key and min-key to return the first entry in case of several candidateshttp://dev.clojure.org/jira/browse/CLJ-997
Clojure<p>Consider the following code:</p>
<p>(def values [<span class="error">&#91;1 2&#93;</span> <span class="error">&#91;3 4&#93;</span> <span class="error">&#91;5&#93;</span> <span class="error">&#91;6 7&#93;</span> <span class="error">&#91;8&#93;</span>])<br/>
(apply max-key count values)<br/>
; =&gt; <span class="error">&#91;6 7&#93;</span></p>
<p>Which returns the <b>last</b> max entry <span class="error">&#91;6 7&#93;</span>. Why its not the first max entry <span class="error">&#91;1 2&#93;</span>?<br/>
Well, truth is "max-key" gives no warranty on which max value will be returned.</p>
<p>Consider the following example in Scala:</p>
<p>println(List(List(0, 1, 2), List(2, 3, 4), List(1), List(1, 2, 3)).maxBy(_.length))<br/>
&gt; List(0, 1, 2)</p>
<p>The very same function in Scala returns the <b>first</b> max entry (by default).</p>
<p>The code from relase 1.4 file "clojure/core.clj#4419-4426" looks is:<br/>
=======================<br/>
4419: (defn max-key<br/>
4420: "Returns the x for which (k x), a number, is greatest."<br/>
4421: {:added "1.0"<br/>
4422: :static true}<br/>
4423: (<span class="error">&#91;k x&#93;</span> x)<br/>
4424: (<span class="error">&#91;k x y&#93;</span> (if (&gt; (k x) (k y)) x y))<br/>
4425: (<span class="error">&#91;k x y &amp; more&#93;</span><br/>
4426: (reduce1 #(max-key k %1 %2) (max-key k x y) more)))<br/>
=======================</p>
<p>I am unsure what is the motivation in returning the last candidate, but</p>
<p>I suggest two following things:</p>
<p>1. Make "max-key" and "min-key" return the <b>first</b> max/min entry if there are several candidates.</p>
<p> This behavior seems more natural/convenient (to me), because in most cases you want to get first "winner".<br/>
(e.g. find the first biggest vector in a sequence) and less often you need to get the last entry &#8211; in those cases<br/>
you can do "reverse" before feeding a sequence to "max-key", thus it seems having "return first max" behavior more useful.</p>
<p> Line #4424 should have "&gt;=" instead of "&gt;".</p>
<p>2. Make "max-key" and "min-key" make warranty on order, which max/min entry will be returned (either first or last).</p>
<p> 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".</p>CLJ-997max-key and min-key to return the first entry in case of several candidatesDefectMinorOpenUnresolvedUnassignedOleksandr ShyshkoFri, 18 May 2012 06:03:15 -0500Fri, 18 May 2012 16:03:44 -0500Release 1.3Release 1.401<p>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.</p>Global Rank