Growing Your Vectors

Now that we’ve seen how to get things out of a vector let’s move on to putting new things in. Again, you can’t actually change an existing vector, but you can make a new, slightly longer vector. One way to get a bigger vector is with the conj—short for conjunction—function:

 (conj novels ​"Carrie"​) ​; Adds "Carrie" to our list of novels.

Evaluate the preceding code and you will get back a new four-element vector:

 [​"Emma"​ ​"Coma"​ ​"War and Peace"​ ​"Carrie"​]

Note that Stephen King’s guide to dealing with high-school conflict has landed at the end of the new vector. If you want to add an item to the front of your vector you can turn to the cons—as in construct—function:

 (cons ​"Carrie"​ novels)

Evaluate the preceding ...

Get Getting Clojure now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.