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:

  1. vec: This is the vector set of the element.
  2. 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 println to print the element returned. Then we use the get method to get the data value at index position 3 from the vector.
  • Line 5: We print the element returned. The get method is used to get the data value at index position 1 from the vector.
  • Line 6: We call the gett function.

Free Resources