I know that clojure's clojure.lang.IPersistentVector
implements assoc
, as in (assoc [0 1 2 3] 0 -1) ; => [-1 1 2 3]
. I have also heard (as in this answer) that clojure's vector doesn't implement dissoc
, as in (dissoc [0 1 2 3] 0) ; => [1 2 3]
. If this functionality is so easily reproducible using subvec
, is there any real reason why it shouldn't be implemented in clojure.lang
, clojure.core
, or even contrib? If not, is there any reasoning behind that?
Dissoc doesn't make much sense for vectors for two reasons:
dissoc
is "remove a key". You can't remove a key from a vector without causing other side effects (e.g. moving all future values)dissoc
would perform relatively badly on vectors if it had to move all subsequent keys - roughly O(n) with quite a lot of GC. Clojure core generally avoids implementing operations that aren't efficient / don't make sense for a particular data structure.Basically, if you find yourself wanting to do dissoc
on a vector, you are probably using the wrong data structure. A persistent hashmap or set is probably a better choice.
If you want a data structure which works as a vector but supports cutting out and inserting elements or subsequences efficiently, then it is worth checking out RRB trees: https://github.com/clojure/core.rrb-vector
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With