Last active
October 12, 2020 07:29
-
-
Save bfollington/f98991fbf3cd05a0fea2988078608c87 to your computer and use it in GitHub Desktop.
https://stackoverflow.com/questions/20136617/clojure-idiomatic-update-a-maps-value-if-the-key-exists
This file contains hidden or 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 contains-in? | |
[m ks] | |
(not= ::absent (get-in m ks ::absent))) | |
(defn update-in-if-contains | |
[m ks f & args] | |
(if (contains-in? m ks) | |
(apply (partial update-in m ks f) args) | |
m)) | |
;; (update-in-if-contains map [:key] map-fn) | |
(defn helpme [m] | |
(cond-> m | |
(:r m) (assoc :r []) | |
(:g m) (assoc :g []))) | |
;; will only replace :r if :r exists, concise way to do option.map |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment