Created
August 2, 2012 19:35
-
-
Save kylecbrodie/3239978 to your computer and use it in GitHub Desktop.
Cleans a map to remove nil, empty and false values
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
(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