Search code examples
functionclojure

Function for the maximum index of vector in clojure


Hi I'd like to ask how to get the maximum value of index from Vector in clojure?

(def dummyvector [190 260 300 310 250])

I have dummyvector here, index 0 = 190, 1 = 260 ... I know the maximum is index 4 = 250. Does clojure have function to get the index value of 4?


Solution

  • Please see the list of documentation in this template project, especially Getting Clojure, Brave Clojure, and the Clojure CheatSheet.

    As for your particular question, just type something like:

    > (count [5 4 3 2 1])
    5
    

    You can then use the dec function (decrement by 1) to get 4.

    > (dec (count [5 4 3 2 1]))
    4
    

    If you are instead trying to search for an arbitrary value in the list, you may wish to use Java interop like:

    (.indexOf  [0 2 4 1 3 5]  4) => 2