Is there a way to remove an item from a vector based on index as of now i am using subvec to split the vector and recreate it again. I am looking for the reverse of assoc for vectors?
subvec
is probably the best way. The Clojure docs say subvec
is "O(1) and very fast, as the resulting vector shares structure with the original and no trimming is done". The alternative would be walking the vector and building a new one while skipping certain elements, which would be slower.
Removing elements from the middle of a vector isn't something vectors are necessarily good at. If you have to do this often, consider using a hash-map so you can use dissoc
.
See:
clojuredocs.org
clojure.github.io
, where the official website points to.(defn vec-remove "remove elem in coll" [pos coll] (into (subvec coll 0 pos) (subvec coll (inc pos))))
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