Clojure Remove item from Vector at a Specified Location

20,348

Solution 1

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:

  • subvec at clojuredocs.org
  • subvec at clojure.github.io, where the official website points to.

Solution 2

(defn vec-remove
  "remove elem in coll"
  [pos coll]
  (into (subvec coll 0 pos) (subvec coll (inc pos))))

Solution 3

user=> (def a [1 2 3 4 5])
user=> (time (dotimes [n 100000] (vec (concat (take 2 a) (drop 3 a)))))
"Elapsed time: 1185.539413 msecs"
user=> (time (dotimes [n 100000] (vec (concat (subvec a 0 2) (subvec a 3 5)))))
"Elapsed time: 760.072048 msecs"

Yup - subvec is fastest

Solution 4

The vector library clojure.core.rrb-vector provides logarithmic time concatenation and slicing. Assuming you need persistence, and considering what you're asking for, a logarithmic time solution is as fast as theoretically possible. In particular, it is much faster than any solution using clojure's native subvec, as the concat step puts any such solution into linear time.

(require '[clojure.core.rrb-vector :as fv])
(let [s (vec [0 1 2 3 4])]
  (fv/catvec (fv/subvec s 0 2) (fv/subvec s 3 5)))
; => [0 1 3 4]

Solution 5

Here is a solution iv found to be nice:

(defn index-exclude [r ex] 
   "Take all indices execpted ex" 
    (filter #(not (ex %)) (range r))) 


(defn dissoc-idx [v & ds]
   (map v (index-exclude (count v) (into #{} ds))))

(dissoc-idx [1 2 3] 1 2)


'(1)
Share:
20,348
Hamza Yerlikaya
Author by

Hamza Yerlikaya

Updated on May 26, 2021

Comments

  • Hamza Yerlikaya
    Hamza Yerlikaya almost 3 years

    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?

  • Torbjørn
    Torbjørn over 11 years
    For removing a single item this is slower than using subvec, but for removing multiple indexes this is pretty nice.
  • Ryan Wilson
    Ryan Wilson almost 9 years
    What clojure version did you use for the subvec w/ transients version? Going back 4ish years now, there's been an open bug for subvec being incompatible with transient, because APersistentVector$SubVector does not implement IEditableCollection. I assume that's why the example wraps the subvec call in (vec (subvec ...)) though in my tests with clojure 1.7, vec in that case will still return a clojure.lang.APersistentVector$SubVector, presumably as an optimization since, but I haven't dug into the clojure source for that.
  • omiel
    omiel almost 9 years
    1.6.0 I think. It was released a few months before this answer was written.
  • Ahmed Fasih
    Ahmed Fasih almost 8 years
    Would it be possible for you to update for Clojure 1.8? As @RyanWilson mentioned, your transient example doesn't work with 1.8.
  • Ahmed Fasih
    Ahmed Fasih almost 8 years
    Replacing vec in your last example with into [] works with Clojure 1.8 but that absolutely kills the performance :(. The fastest I can get is (into (subvec v 0 i) (subvec v j)), which is about 15% faster than your using subvec middle version on my computer…