Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Respond to review comments
  • Loading branch information
Raf Szalanski committed Mar 14, 2016
commit 88ab7972dd0e76fa838b491814186b79ce347d2c
6 changes: 3 additions & 3 deletions src/clj/clojure/walk.clj
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ the sorting function."}
(defn transform-keys
"Recursively transform all map keys using f as a transformation function."
{:added "1.9"}
[f m]
[m f]
(let [entry-f (fn [[k v]] [(f k) v])]
;; only apply to maps
(postwalk (fn [x] (if (map? x) (into {} (map entry-f x)) x)) m)))
Expand All @@ -102,13 +102,13 @@ the sorting function."}
"Recursively transforms all map keys from strings to keywords."
{:added "1.1"}
[m]
(transform-keys (fn [k] (if (string? k) (keyword k) k)) m))
(transform-keys m (fn [k] (if (string? k) (keyword k) k))))

(defn stringify-keys
"Recursively transforms all map keys from keywords to strings."
{:added "1.1"}
[m]
(transform-keys (fn [k] (if (keyword? k) (name k) k)) m))
(transform-keys m (fn [k] (if (keyword? k) (name k) k))))

(defn prewalk-replace
"Recursively transforms form by replacing keys in smap with their
Expand Down
6 changes: 3 additions & 3 deletions test/clojure/test_clojure/clojure_walk.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
{"a" 1, nil {"b" 2 "c" 3}, "d" 4})))

(deftest t-transform-keys
(is (= (w/transform-keys (fn [k]
(is (= (w/transform-keys {:a 1, nil {:b 2 :c 3}, :d 4, :e 5}
(fn [k]
(if (keyword? k)
(str (name k) "-" (name k))
k))
{:a 1, nil {:b 2 :c 3}, :d 4, :e 5})
k)))
{"a-a" 1, nil {"b-b" 2 "c-c" 3}, "d-d" 4, "e-e" 5})))

(deftest t-prewalk-order
Expand Down