Skip to content

Commit e29a3e6

Browse files
michalmarczykstuarthalloway
authored andcommitted
CLJ-2247: restore last match semantics of {min,max}-key
The CLJ-99 patch (d10a9d3) makes two changes to the behaviour of {min,max}-key: 1. it ensures that the key function is only called once on each argument; 2. it causes both functions to return the first match, whereas previously they returned the last one. This preserves 1. and reverts 2. Signed-off-by: Stuart Halloway <stu@cognitect.com>
1 parent bdb3261 commit e29a3e6

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

src/clj/clojure/core.clj

+2-2
Original file line numberDiff line numberDiff line change
@@ -4944,7 +4944,7 @@
49444944
(if more
49454945
(let [w (first more)
49464946
kw (k w)]
4947-
(if (> kw kv)
4947+
(if (>= kw kv)
49484948
(recur w kw (next more))
49494949
(recur v kv (next more))))
49504950
v)))))
@@ -4962,7 +4962,7 @@
49624962
(if more
49634963
(let [w (first more)
49644964
kw (k w)]
4965-
(if (< kw kv)
4965+
(if (<= kw kv)
49664966
(recur w kw (next more))
49674967
(recur v kv (next more))))
49684968
v)))))

test/clojure/test_clojure/other_functions.clj

+4-1
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,10 @@
335335
count ["longest" "a" "xy" "foo" "bar"] "a" "longest"
336336
- [5 10 15 20 25] 25 5
337337
#(if (neg? %) (- %) %) [-2 -1 0 1 2 3 4] 0 4
338-
{nil 1 false -1 true 0} [true true false nil] false nil))
338+
{nil 1 false -1 true 0} [true true false nil] false nil)
339+
(are [f k coll expected] (= expected (apply f k coll))
340+
min-key :x [{:x 1000} {:x 1001} {:x 1002} {:x 1000 :second true}] {:x 1000 :second true}
341+
max-key :x [{:x 1000} {:x 999} {:x 998} {:x 1000 :second true}] {:x 1000 :second true}))
339342

340343

341344
; Printing

0 commit comments

Comments
 (0)