Codebase list kitchensink-clojure / 7d9e9d8
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
2 changed file(s) with 9 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
360360 (let [res (dissoc m k)]
361361 (when-not (empty? res)
362362 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))
363368
364369 (defn merge-with-key
365370 "Returns a map that consists of the rest of the maps conj-ed onto
255255 (testing "should remove the empty map"
256256 (is (= {:a {:b 1}} (dissoc-in testmap [:a :c :d]))))))
257257
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
258262 (deftest merge-with-key-test
259263 (let [m1 {:a 1 :b 2}
260264 m2 {:a 3 :b 4}