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:
(defn vec-remove
"remove elem in coll"
[pos coll]
(into (subvec coll 0 pos) (subvec coll (inc pos))))
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
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]
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)
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
.
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 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 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"
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)))
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)))
© 2022 - 2024 — McMap. All rights reserved.