Clojure Remove item from Vector at a Specified Location
Asked Answered
R

9

40

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?

Roasting answered 8/9, 2009 at 16:40 Comment(0)
A
35

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.
Aquarius answered 8/9, 2009 at 17:37 Comment(0)
C
33
(defn vec-remove
  "remove elem in coll"
  [pos coll]
  (into (subvec coll 0 pos) (subvec coll (inc pos))))
Coze answered 19/8, 2013 at 17:33 Comment(1)
This has better performance than all the other suggestions except the mapv selecting indices you want. But I think this is simpler and performance as I benchmarked it was only about a 2 percent slower.Rover
S
18
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

Status answered 11/9, 2009 at 7:55 Comment(0)
C
9

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]
Crampton answered 29/8, 2014 at 2:45 Comment(0)
L
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)
Lenticular answered 28/8, 2012 at 16:20 Comment(1)
For removing a single item this is slower than using subvec, but for removing multiple indexes this is pretty nice.Secretin
J
4

subvec is fast ; combined with transients it gives even better results.

Using criterium to benchmark:

user=> (def len 5)
user=> (def v (vec (range 0 5))
user=> (def i (quot len 2))
user=> (def j (inc i))

; using take/drop
user=> (bench
         (vec (concat (take i v) (drop j v))))
;              Execution time mean : 817,618757 ns
;     Execution time std-deviation : 9,371922 ns

; using subvec
user=> (bench
         (vec (concat (subvec v 0 i) (subvec v j len))))
;              Execution time mean : 604,501041 ns
;     Execution time std-deviation : 8,163552 ns

; using subvec and transients
user=> (bench
         (persistent!
          (reduce conj! (transient (vec (subvec v 0 i))) (subvec v j len))))
;              Execution time mean : 307,819500 ns
;     Execution time std-deviation : 4,359432 ns

The speedup is even greater at greater lengths ; the same bench with a len equal to 10000 gives means: 1,368250 ms, 953,565863 µs, 314,387437 µs.

Jeddy answered 27/6, 2014 at 19:17 Comment(4)
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.Monkhmer
1.6.0 I think. It was released a few months before this answer was written.Jeddy
Would it be possible for you to update for Clojure 1.8? As @RyanWilson mentioned, your transient example doesn't work with 1.8.Leonardaleonardi
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…Leonardaleonardi
H
3

It may be faster to get the indexes you want.

(def a [1 2 3 4 5])

(def indexes [0 1 3 4])

(time (dotimes [n 100000] (vec (concat (subvec a 0 2) (subvec a 3 5)))))
"Elapsed time: 69.401787 msecs"

(time (dotimes [n 100000] (mapv #(a %) indexes)))
"Elapsed time: 28.18766 msecs"
Hackle answered 10/5, 2020 at 16:18 Comment(0)
E
2

Yet another possibility which ought to work with any sequence and not bomb if the index was out of range...

(defn drop-index [col idx]
  (filter identity (map-indexed #(if (not= %1 idx) %2) col)))
Eugenol answered 27/6, 2014 at 1:58 Comment(0)
W
0

Maybe not the fastest but maybe the most idiomatic?

(defn remove-n [n coll]
  (let [indexed (map-indexed vector coll)
        cleaned (remove #(= n (first %)) indexed)]
    (map second cleaned)))
Whiskey answered 13/11, 2023 at 12:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.