Skip to content

Instantly share code, notes, and snippets.

@orther
Created October 22, 2015 14:40
Show Gist options
  • Save orther/60fecbe93c1c32ce8639 to your computer and use it in GitHub Desktop.
Save orther/60fecbe93c1c32ce8639 to your computer and use it in GitHub Desktop.
Playing with getting top two numbers w/ O(n)
(require '[clojure.core.reducers :as r])
(defn top-two
[[big1 big2 :as acc] x]
(cond
(> x big1) [x big1]
(> x big2) [big1 x]
:else acc))
(defn top-two+
([] [0 0])
([acc x] (top-two acc x)))
(time (def nums (doall (take 1000000 (range)))))
(time (second (reduce top-two [0 0] nums)))
(time (second (r/reduce top-two+ nums)))
(time (second (r/fold top-two+ nums)))
@orther
Copy link
Author

orther commented Oct 22, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment