Last active
December 20, 2015 07:59
-
-
Save devn/6096924 to your computer and use it in GitHub Desktop.
I wish these were in clojure.walk.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns clojure.walk.useful | |
(:require [clojure.walk :refer (postwalk)])) | |
(defn transform-keys | |
"Recursively transforms all keys in map `m` which return true for | |
a function `pred` by applying a function `f` to them." | |
[m pred f] | |
(let [func (fn [[k v]] (if (pred k) [(f k) v] [k v]))] | |
(postwalk (fn [x] (if (map? x) (into {} (map func x)) x)) m))) | |
(defn transform-vals | |
"Recursively transforms all values in map `m` which return true for | |
a function `pred` by applying a function `f` to them." | |
[m pred f] | |
(let [func (fn [[k v]] (if (pred v) [k (f v)] [k v]))] | |
(postwalk (fn [x] (if (map? x) (into {} (map func x)) x)) m))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment