Skip to content

Instantly share code, notes, and snippets.

@quux00
Created December 15, 2012 21:14
Show Gist options
  • Save quux00/4299281 to your computer and use it in GitHub Desktop.
Save quux00/4299281 to your computer and use it in GitHub Desktop.
(ns thornydev.top100.heap
(:require [thornydev.lib.leftist-heap
:refer [pq-first pq-rest pq-insert pq-empty]]))
(defn distance [[x y]]
(+ (java.lang.Math/abs x) (java.lang.Math/abs y)))
(defn dist-lt? [pt1 pt2]
(< (distance pt2) (distance pt1)))
(defn heap-sift [pq pt]
(if (dist-lt? pt (pq-first pq))
pq
(pq-insert dist-lt? (pq-rest dist-lt? pq) pt)))
(defn top-heap [points max-size]
(let [[add-all xs] (split-at max-size points)
init-q (reduce #(pq-insert dist-lt? % %2) pq-empty add-all)]
(reduce heap-sift init-q xs)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment