Skip to content

Instantly share code, notes, and snippets.

@jotanavarro
Created May 30, 2020 12:56
Show Gist options
  • Save jotanavarro/06f23218e2e6ad9ffb2b651f1292c6a5 to your computer and use it in GitHub Desktop.
Save jotanavarro/06f23218e2e6ad9ffb2b651f1292c6a5 to your computer and use it in GitHub Desktop.
;; 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