I'm trying to write a function that finds the largest two numbers from a list of positive integers in Clojure.
I've toyed around with Scheme in the past, but I'm very new to Clojure. This is what I came up with:
; Accumulator function for reduce later (defn top-two [acc x] (let [big1 (first acc) big2 (second acc)] (cond (> x big1) [x big1] (> x big2) [big1 x] :else acc))) (defn top-two-list [coll] (reduce top-two [0 0] coll))
This works, but I'm wondering if there is a way to simplify the
top-two function, or to make this more readable or idiomatic.
This could be done much simpler if I was willing to sort the entire list, but I'm looking for an O(n) solution.