Given a map of replacement pairs and a vector/collection, returns a
vector/seq with any elements = a key in smap replaced with the
corresponding val in smap
user=> (replace [:zeroth :first :second :third :fourth] [0 2 4 0]) [:zeroth :second :fourth :zeroth] user=> (replace [10 9 8 7 6] [0 2 4]) [10 8 6]
user=> (replace '{0 ZERO, 1 ONE, 2 TWO} '(This is the code — 0 1 2 0))
(This is the code — ZERO ONE TWO ZERO)
user=> (replace {2 :two, 4 :four} [4 2 3 4 5 6 2])
[:four :two 3 :four 5 6 :two]
(defn replace
"Given a map of replacement pairs and a vector/collection, returns a
vector/seq with any elements = a key in smap replaced with the
corresponding val in smap"
{:added "1.0"}
[smap coll]
(if (vector? coll)
(reduce (fn [v i]
(if-let [e (find smap (nth v i))]
(assoc v i (val e))
v))
coll (range (count coll)))
(map #(if-let [e (find smap %)] (val e) %) coll)))
Comments top
1 comment(s) for replace.
The behaviour for vectors was a little strange for me. I'd say replace "selects" from "smap" the indexes which are in "coll"