Created
May 30, 2020 12:56
-
-
Save jotanavarro/06f23218e2e6ad9ffb2b651f1292c6a5 to your computer and use it in GitHub Desktop.
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
;; The core function assoc-in | |
(defn my-assoc-in | |
[m [& ks] v] | |
(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 v)] | |
(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