Skip to content

Instantly share code, notes, and snippets.

View jotanavarro's full-sized avatar
🔷
Go-ing

Jose Navarro jotanavarro

🔷
Go-ing
View GitHub Profile
@jotanavarro
jotanavarro / update-in.clj
Created May 30, 2020 14:15
update-in Clojure function
;; 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)
;; 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
;; The clojure comp cre function
;; Recursive
(defn my-comp
([] my-comp)
([f] f)
([f & fs]
(fn [& args]
(f (apply (apply my-comp fs) args)))))