Hamza Yerlikaya
Hamza Yerlikaya

Reputation: 49329

Clojure Remove item from Vector at a Specified Location

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?

Upvotes: 40

Views: 25518

Answers (9)

roboli
roboli

Reputation: 1478

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)))

Upvotes: 0

zenna
zenna

Reputation: 9176

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

Upvotes: 34

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"

Upvotes: 3

Brian Carper
Brian Carper

Reputation: 72926

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.

Upvotes: 35

djhaskin987
djhaskin987

Reputation: 10057

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]

Upvotes: 9

omiel
omiel

Reputation: 1593

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.

Upvotes: 4

BillRobertson42
BillRobertson42

Reputation: 12883

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)))

Upvotes: 2

narkisr
narkisr

Reputation: 311

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)

Upvotes: 5

Timothy Pratley
Timothy Pratley

Reputation: 10662

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

Upvotes: 18

Related Questions