Nested Vector Update

Say I have the following vector

(def x [[1 2 3] [4 5 6] [7 8]])

and I want to add the number 9 to the last vector (I don't know the index of the vector)

(conj (vec (butlast x)) (conj (last x) 9))
 #=> [[1 2 3] [4 5 6] [7 8 9]]

Is there a better / clearer way to do this?

+4
source share
1 answer

Use effective tail access features

(conj (pop x) (conj (peek x) 9))

But you can also

(update-in x [(dec (count x))] conj 9)
+8
source

All Articles