Clojure: Finding the highest first element of a second element of a tuple and returning the first element of the found tuple

Viewed 72

Finding the highest first element of a second element of a tuple and returning the first element of the found tuple.

(defn test [cards]
                                        
  (->>
   (apply max (map #(get % 1) cards))
   (.indexOf (map #(get % 1) cards) )
   (get cards)
   (first)
   )
  )

(test [[1 14] [3 24] [2 12] [4 24])

=> 3

This works perfectly, but is there a more idiomatic way to do this?

1 Answers

A very basic way, using reduce and destructuring:

(->> cards
     (reduce (fn [[_ v0 :as t0] [_ v :as t]]
                 (if (> v v0) t t0)))
     first)
Related