Skip to content

Instantly share code, notes, and snippets.

@w01fe
Created July 10, 2013 06:58
Show Gist options
  • Save w01fe/5964022 to your computer and use it in GitHub Desktop.
Save w01fe/5964022 to your computer and use it in GitHub Desktop.
top_k.clj
(defn top-k
"Return the top k elements of xs according to score-fn"
[k score-fn xs]
(let [n (count xs)
scores (dbl/amake [i n] (score-fn (nth xs i)))
^ints idxs (dbl/amax-indices scores k)]
(seq (hiphip.array/amake Object [i k] (nth xs (aget idxs (- n i 1)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment