What is the get vector method in Clojure?
What is a vector in Clojure?
A vector is a Clojure data structure that stores sequentially indexed data.
What is a get vector method in Clojure?
A get method returns the element of a vector by its index position. We would see this in the following example.
Syntax
(get vec index)
Parameter(s)
The get method receives two parameters:
vec: This is the vector set of the element.index: This is the position of the element index.
Return value
The method returns an element from the vector at the index position.
Example
(ns clojure.examples.example(:gen-class))(defn gett [](println (get (vector 5 4 3 2 1) 3))(println (get (vector 5 4 3 2 1) 1)))(gett)
Explanation
From the code above:
- Line 3: We define a function
gett. - Line 4: We use
printlnto print the element returned. Then we use thegetmethod to get the data value at index position3from thevector.
- Line 5: We print the element returned. The
getmethod is used to get the data value at index position1from thevector.
- Line 6: We call the
gettfunction.