Browse Appendices

A.3.2 Vectors

Explore Clojure's vectors as indexed collections enabling efficient random access and modification with examples.

Efficient and Portable Indexed Collections

In Clojure, vectors are a fundamental data structure that represents indexed collections. They are particularly favored for their efficiency in random access and updates. This makes vectors highly suitable for various computational tasks where order and quick, indexed mutations are needed.

Creating Vectors

Vectors in Clojure can be created using two primary methods: literal syntax and the vector function. Both of these approaches allow for easy initialization of vectors:

;; Using literal syntax
(def my-vector [10 20 30 40])

;; Using the vector function
(def my-vector (vector 10 20 30 40))

Accessing Elements

Retrieving elements from a vector can be accomplished with the get function or by using the vector itself as a function:

;; Using get function
(get my-vector 2) ;=> 30

;; Direct function invocation
(my-vector 2) ;=> 30

Modifying Vectors

Vectors are immutable, meaning modifications don’t alter the original vector but return a new one. The assoc function is employed to modify elements at specific indexes, while the conj function adds elements to the end:

;; Modify an element with assoc
(def updated-vector (assoc my-vector 1 25))
;; => [10 25 30 40]

;; Add an element with conj
(def extended-vector (conj my-vector 50))
;; => [10 20 30 40 50]

Common Use Cases

Vectors are heavily used in Clojure programs due to their flexibility and performance characteristics. They are commonly employed for:

  • Organizing ordered collections
  • Managing indexed data for algorithms requiring random access
  • Implementing stacks (push operations)

Vectors represent a key aspect of Clojure’s approach to handling collections through immutability and provide developers with robust mechanisms for list-like operations within the landscape of functional programming.


### How can you create a vector in Clojure? - [x] Using the vector function - [x] Using literal syntax - [ ] Using curly braces - [ ] Using parentheses > **Explanation:** Vectors in Clojure can be created using the `vector` function or using literal syntax with square brackets. Curly braces and parentheses are used for sets and lists/maps, respectively. ### Which function provides efficient element update in a vector? - [ ] conj - [x] assoc - [ ] merge - [ ] apply > **Explanation:** `assoc` is used to update a vector by creating a new vector with the modified value. `conj` is for appended additions, not index-specific mutations. ### What does `conj` do when used with vectors? - [x] Adds an element to the end - [ ] Removes the first element - [ ] Sorts the elements - [ ] Inserts at the beginning > **Explanation:** `conj` is used to append elements to the end of a vector in Clojure, maintaining its functional nature of immutability. ### If you use `get` with a `my-vector` and an index of 4, but your vector only has 4 elements starting from 0, what happens? - [x] Returns nil - [ ] Throws an IndexOutOfBoundsException - [ ] Returns the last element - [ ] Triggers an infinite loop > **Explanation:** The `get` function returns `nil` when accessing an index that doesn't exist in a vector, avoiding Java’s IndexOutOfBoundsException. ### Can you use vectors as functions to access their elements by index? - [x] Yes - [ ] No > **Explanation:** Vectors can be directly used as functions to access their elements by index value in Clojure, demonstrating their utility. ### What is a characteristic of vectors in Clojure? - [x] Immutable data structure - [ ] Mutable data structure - [ ] Unordered sequence - [ ] Fixed size > **Explanation:** Vectors in Clojure are immutable, meaning updating a vector in functional programming results in a new vector rather than modifying the original.
Saturday, October 5, 2024