Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the index of the smallest member of this vector in Clojure?

Tags:

clojure

reduce

I have used the following expression to retrieve the index of the smallest number in a vector. However, I would like to avoid the use of .indexOf (for efficiency reasons and maybe numeric precision, although I guess the numbers are implicitly converted to strings).

(.indexOf [1 2 3 4 0 5]
  (reduce #(if (< %1 %2) %1 %2) [1 2 3 4 0 5] ))

Would it be possible to do it differently using reduce?

like image 863
kostas Avatar asked Jan 11 '12 07:01

kostas


1 Answers

user=> (first (apply min-key second (map-indexed vector [1 2 4 0 5])))
3
like image 129
Alex Taggart Avatar answered Sep 30 '22 22:09

Alex Taggart