replace

clojure.core

  • (replace smap coll)
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

2 Examples top

  • 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)
    
  • user=> (replace {2 :a, 4 :b} [4 2 3 4 5 6 2])
    [:b :a 3 :b 5 6 :a]
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:4430 top

(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)))
Vars in clojure.core/replace:
Used in 0 other vars

Comments top

No comments for replace. Log in to add a comment.