Skip to content

Instantly share code, notes, and snippets.

@bsless
Last active March 26, 2020 15:59
Show Gist options
  • Save bsless/dabb84a1deadf42a06f397309891ef65 to your computer and use it in GitHub Desktop.
Save bsless/dabb84a1deadf42a06f397309891ef65 to your computer and use it in GitHub Desktop.
probably the simplest way to implement dissoc-in
(defn dissoc-in
[m ks]
(let [[k & ks] ks]
(if ks
(let [found (find m k)]
(if found
(assoc m k (dissoc-in (val found) ks))
m))
(dissoc m k))))
;;; with vector support for terminal associative value:
(defn dissocv
[v n]
(vec (concat (subvec v 0 n) (subvec v (inc n)))))
(defn dissoc*
[m k]
(if (map? m)
(dissoc m k)
(dissocv m k)))
(defn dissoc-in
[m ks]
(let [[k & ks] ks]
(if ks
(let [found (find m k)]
(if found
(assoc m k (dissoc-in (val found) ks))
m))
(dissoc* m k))))
(dissoc-in {} [:a :b])
;; => {}
(dissoc-in {:a {}} [:a :b])
;; => {:a {}}
(dissoc-in {:a {:b 1}} [:a :b])
;; => {:a {}}
(dissoc-in {:a [{:b 1}]} [:a 0 :b])
;; => {:a [{}]}
(dissoc-in {:a [{:b [:c :d]}]} [:a 0 :b 0]);; => {:a [{:b [:d]}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment