Created
May 30, 2020 14:15
-
-
Save jotanavarro/fe044fa87f692756dd04c65539acd593 to your computer and use it in GitHub Desktop.
update-in Clojure function
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
| ;; My implementation of Clojure update-in function | |
| (defn my-update-in | |
| [m ks f & args] | |
| (let [sorted-keys (reverse ks)] | |
| (loop [curr-key (first sorted-keys) | |
| rem-keys (rest sorted-keys) | |
| curr-map (get-in m (reverse rem-keys)) | |
| acc-val (assoc curr-map curr-key (apply f (get-in m ks) args))] | |
| (if (empty? rem-keys) | |
| acc-val | |
| (let [next-key (first rem-keys) | |
| next-rem (rest rem-keys) | |
| next-map (get-in m next-rem) | |
| next-val (assoc next-map next-key acc-val)] | |
| (recur next-key next-rem next-map next-val)))))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment