Skip to content

Instantly share code, notes, and snippets.

@kylecbrodie
Created August 2, 2012 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylecbrodie/3239978 to your computer and use it in GitHub Desktop.
Save kylecbrodie/3239978 to your computer and use it in GitHub Desktop.
Cleans a map to remove nil, empty and false values
(defn filter-map
"Returns a map with only the pairs that (pred key value) returned true"
[pred m]
(select-keys m (filter #(pred % (m %)) (keys m))))
(defn clean-map
"Takes a map and removes all nil's, empty data structs, and
falses as these can be implied by them not being there."
[m]
(filter-map (fn [key val] (if (or (true? val)
(and val
(or (string? val) (seq? val) (vector? val) (map? val) (set? val))
(not (empty? val)))))) m))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment