Why is clocjure not using decoc for vectors?

I know that clojure clojure.lang.IPersistentVector implements assoc , as in (assoc [0 1 2 3] 0 -1) ; => [-1 1 2 3] (assoc [0 1 2 3] 0 -1) ; => [-1 1 2 3] . I also heard (as in this answer) that the clojure vector does not implement dissoc , as in (dissoc [0 1 2 3] 0) ; => [1 2 3] (dissoc [0 1 2 3] 0) ; => [1 2 3] . If this functionality is so easily reproduced using subvec , is there any real reason why it should not be implemented in clojure.lang , clojure.core or even contributed? If not, are there any thoughts on this?

+6
source share
1 answer

Disoc doesn't make much sense for vectors for two reasons:

  • A dissoc value means "delete key." You cannot remove a key from a vector without causing other side effects (for example, moving all future values).
  • dissoc will perform relatively poorly on vectors if it had to move all subsequent keys - roughly O (n) with a fairly large amount of GC. The Clojure core usually avoids performing operations that are inefficient / meaningless for a particular data structure.

Basically, if you want to make dissoc on a vector, you are probably using the wrong data structure. The likely choice of a hash map or set is the best choice.

If you need a data structure that works like a vector, but supports cutting and pasting elements or subsequences, then you should check the RRB trees: https://github.com/clojure/core.rrb-vector

+8
source

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


All Articles