Reputation: 877
I want to find the position of the last occurence of an element in a sequence. Testing some solutions I came up with,
user> (def numbers (repeatedly 100 #(rand-int 100)))
#'user/numbers
user> (time ((zipmap numbers (range)) 22))
"Elapsed time: 0.865193 msecs"
90
user> (time (last (keep-indexed #(if (= 22 %2) %1) numbers)))
"Elapsed time: 1.600483 msecs"
90
Is there a more concise built-in that does this?
Upvotes: 3
Views: 886
Reputation: 20944
You could use lastIndexOf
after you cast the object to the correct type:
(.lastIndexOf numbers (int 22))
The method is documented here: http://docs.oracle.com/javase/6/docs/api/java/util/List.html#lastIndexOf(java.lang.Object)
Upvotes: 1