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"
:static true}
[smap coll]
(if (vector? coll)
(reduce1 (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
No comments for replace. Log in to add a comment.