weavejester / medley

A lightweight library of useful Clojure functions
Eclipse Public License 1.0
865 stars 66 forks source link

Suggested addition: replace-subvec #82

Open jramosg opened 10 months ago

jramosg commented 10 months ago

When you need to put a vector into another vector, from start until the end of the vector to insert.

(defn replace-subvec [v v2 index]
  {:pre [(vector? v)
         (vector? v2)
         (integer? index)]}
  (let [v-count (count v)
        index (if (<= index v-count)
                index v-count)]
    (persistent!
      (reduce-kv
        (fn [acc i el]
          (assoc! acc (+ i index) el))
        (transient v) v2))))
weavejester commented 10 months ago

This function might be a little too specific for Medley.