Last active
March 26, 2020 15:59
-
-
Save bsless/dabb84a1deadf42a06f397309891ef65 to your computer and use it in GitHub Desktop.
probably the simplest way to implement dissoc-in
This file contains hidden or 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
(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