orther
10/22/2015 - 2:40 PM

Playing with getting top two numbers w/ O(n)

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)))