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 ['a 'b 'c 'd 'e] [0 2 4])
[a c e]
user=> (replace [10 9 8 7 6] [0 2 4])
[10 8 6]
user=> (replace {0 'zero 1 'one 2 'two} '(0 1 2 0))
(zero one two zero)
(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.