Skip to content

Instantly share code, notes, and snippets.

@devn
Last active December 20, 2015 07:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devn/6096924 to your computer and use it in GitHub Desktop.
Save devn/6096924 to your computer and use it in GitHub Desktop.
I wish these were in clojure.walk.
(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