Use reduce
:
(defn assoc-all
[v ks value]
(reduce #(assoc %1 %2 value) v ks))
Example:
(assoc-all [1 2 3 4] [0 1] 2)
;; => [2 2 3 4]
Alternatively, create the key/value pairs and use apply
:
(defn assoc-all
[v ks value]
(->> (interleave ks (repeat value))
(apply assoc v)))
If assoc
was using transients internally this might actually be more efficient than the reduce
version. Since it isn't, the lazy sequence overhead is probably eating it all up.
So, finally, a transient version:
(defn assoc-all
[v ks value]
(persistent!
(reduce
#(assoc! %1 %2 value)
(transient v)
ks)))