I'm trying to fill a Clojure vector with values from a map. I have another vector of specific keys in the map the store the values I need. I need to iterate the key-vec, get the values from the map and store them in another vector.
I've tried using loop+recur:
(let [keys-vec (:keys-vec item)
my-map (:my-map item)]
(loop [newV []
i 0]
(if (< i (count keys-vec))
(recur
(conj newV (get my-map (get keys-vec i)))
(inc i))
newV)))
And it worked. But I know Clojure is known for it's minimalistic/efficient code writing style and I wanted to know whether there's a better way.
Any ideas?
(def new-vector existing-vector)solve the problem? - superkonduktrloopand elements' indices, as Clojure's collections are immutable and persistent. Instead, you are encouraged to use reducing functions. The idiomatic way to fulfil the task would simply be(vals (select-keys my-map keys-vec)). - superkonduktr