This file contains 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
(defn kth-largest [k coll] | |
(let [reducer (fn [elems elem] | |
(take k (sort > (conj elems elem))))] | |
(last (apply (partial reduce reducer) (split-at k coll))))) | |
(kth-largest 4 (range 100)) | |
;; => returns 96, the 4th-largest element in the range (0, 1, 2, ..., 99) | |
(kth-largest 100 (take 200 (cycle (range 100)))) | |
;; => returns 50, the 100th-largest element in the collection (0, 0, 1, 1, 2, 2, ..., 99, 99) |