Clojure Remove item from Vector at a Specified Location

后端 未结 8 832
无人共我
无人共我 2020-12-04 01:35

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 fo

相关标签:
8条回答
  • 2020-12-04 02:10
    (defn vec-remove
      "remove elem in coll"
      [pos coll]
      (vec (concat (subvec coll 0 pos) (subvec coll (inc pos)))))
    
    0 讨论(0)
  • 2020-12-04 02:10

    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]
    
    0 讨论(0)
提交回复
热议问题