Add walk-leaves function.
Add a walk-leaves function to the core namespace which applies
a function to all leaves of a map.
Alex Dreyer
9 years ago
360 | 360 |
(let [res (dissoc m k)]
|
361 | 361 |
(when-not (empty? res)
|
362 | 362 |
res)))))
|
|
363 |
|
|
364 |
(defn walk-leaves
|
|
365 |
"Walk a map applying a function to all leaf nodes"
|
|
366 |
[m f]
|
|
367 |
(mapvals #(if (map? %) (walk-leaves % f) (f %)) m))
|
363 | 368 |
|
364 | 369 |
(defn merge-with-key
|
365 | 370 |
"Returns a map that consists of the rest of the maps conj-ed onto
|
255 | 255 |
(testing "should remove the empty map"
|
256 | 256 |
(is (= {:a {:b 1}} (dissoc-in testmap [:a :c :d]))))))
|
257 | 257 |
|
|
258 |
(deftest walk-leaves-test
|
|
259 |
(testing "should apply a function to all of the leaves"
|
|
260 |
(is (= {:a 2 :b {:c 5}} (walk-leaves {:a 1 :b {:c 4}} inc)))))
|
|
261 |
|
258 | 262 |
(deftest merge-with-key-test
|
259 | 263 |
(let [m1 {:a 1 :b 2}
|
260 | 264 |
m2 {:a 3 :b 4}
|