Lookup
1 2 3 |
(defn lookup [i m] "This function looks up a value, i, in map m and returns the result if it exists, and otherwise returns i." (get m i i)) |
Usage example:
1 2 3 4 |
user=> (lookup 'b '{a 1 b 2}) 2 user=> (lookup 'c '{a 1 b 2}) c |
Substitute
1 2 |
(defn substitute [m l] (map (fn [i] (lookup i m)) l)) |
1 2 |
user=> (substitute '(a c b d) '{a 1, b 2}) (1 c 2 d) |
1 2 |
(defn substitute [l m] (map #(lookup % m) l)) |
Deep Substitute
1 2 3 4 5 6 |
(defn deep-substitute [l m] (map (fn [i] (if (seq? i) (deep-substitute i m) (lookup i m))) l)) |
1 2 |
user=> (deep-substitute '(a (b a) (c) d) '{a 1, b 2}) (1 (2 1) (c) d) |