Clojure vector-dependent behavior
Clojure assoc
applied to vector seems to have inconsistent behavior
When the index is present in the vector, replace the value
(assoc [1 2 3 4 5] 3 42) => [1 2 3 42 5]
When the index is close to the last, the vector grows ( conj
equivalent)
(assoc [1 2 3 4 5] 5 42) => [1 2 3 4 5 42])
Otherwise IndexOutOfBoundsExcpetion
selected
although this is useful in some cases, for example reduce assoc
it can lead to subtle bugs in the program
Is there expected behavior or perhaps a bug in the associated for the vector?
Expected. See Docstring for assoc
, especially the last note on argument index
.
This is described at the top of the page. 101 Clojure programs.