philipbohun.com

Glojure by Example: Vectors

A vector is an indexed, ordered collection. It covers the jobs of both a Go array and a Go slice for ordinary Clojure data. Literal vectors use square brackets.

(def nums [10 20 30])
(println nums)
(println (count nums))
(println (nth nums 1))
(println (nums 2))

conj adds at the end and returns a new vector. The original binding is unchanged. assoc replaces the value at an index. subvec returns a view of a range, end excluded.

(println (conj nums 40))
(println nums)
(println (assoc nums 0 11))
(println (subvec nums 1 3))

Calling a vector with an index looks up that index.

$ glj vectors.glj
[10 20 30]
3
20
30
[10 20 30 40]
[10 20 30]
[11 20 30]
[20 30]

Next example: Lists.