Is team behavior compatible?

assoc can throw an IndexOutOfBoundsException when the index is, well, out of bounds, as in:

 user=> (assoc [] 1 nil) IndexOutOfBoundsException clojure.lang.PersistentVector.assocN(PersistentVector.java:137) 

Why not throw the same exception if I try to set the value at index 0?

 user=> (assoc [] 0 nil) [nil] 

It seems to me that in both cases the index goes beyond ...

thanks

+4
source share
1 answer

Assoc-in uses assoc to change the item on the supplied key (index in your case). Assoc-in (and assoc) tries to create the keys (or indexes) that you need.

Special handling for vectors is mentioned in the associated dock string:

When applied to a vector, a new vector is returned that contains the value of val in the index. Note. Index must be <= (vector counter) .

Update: Just to clarify: A missing exception is consistent, because when 0 is a valid index for a new element in an empty vector, 1 is not.

+3
source

Source: https://habr.com/ru/post/1480328/


All Articles