Skip to content

Instantly share code, notes, and snippets.

@dominem
Created March 22, 2020 15:36
Show Gist options
  • Save dominem/84a79fb07b96ba4ff72c2ffe6aa5f89d to your computer and use it in GitHub Desktop.
Save dominem/84a79fb07b96ba4ff72c2ffe6aa5f89d to your computer and use it in GitHub Desktop.
(defn my-assoc-in
[m [k & ks] v]
(assoc m k
(reduce (fn [v-in k-in] (assoc {} k-in v-in))
v
(reverse ks))))
(comment
(my-assoc-in {} [:a] 1)
;; => {:a 1}
(my-assoc-in {:a 1} [:b] 2)
;; => {:a 1, :b 2}
(my-assoc-in {:a 1} [:a] 2)
;; => {:a 2}
(my-assoc-in {} [:a :b :c] 4)
;; => {:a {:b {:c 4}}}
(my-assoc-in {:a {:b 3}} [:a :b] {:c 123})
;; => {:a {:b {:c 123}}}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment