What is the conj vector method in Clojure?

Overview

A vector is a data storage structure that holds a collection of data items that are sequentially indexed. The (vector) method in Clojure is used to create a vector.

Note: Read this shot to create a vector using the vector method.

The conj() method is a vector method that adds a data item to a vector and returns a new set of vector elements.

Syntax

(conj vec x)

Parameters

We pass two parameters to the conj() method:

vec: The vector set of elements.

x: The data items to be added.

Return value

The conj method returns a new vector with the added element.

Example

(ns clojure.examples.example
(:gen-class))
(defn conjj []
(println (conj (vector 30 29 70) 5)))
(conjj)

Explanation

  • Line 3: We define a function, Conjj.
  • Line 4: We print the new vector returned using println. The conj method adds data element 5 to the vector.
  • Line 5: We call the Conjj function.

Free Resources