A clj snippet #2

Today's snippet is taken from the plumbing library, a clojure utility library. The same function (logicwise) can also be found in other libraries.

(defn dissoc-in
  "Dissociate this keyseq from m, removing any empty maps created as a result
   (including at the top-level)."
  [m [k & ks]]
  (when m
    (if-let [res (and ks (dissoc-in (get m k) ks))]
      (assoc m k res)
      (let [res (dissoc m k)]
        (when-not (empty? res)

This function should actually be in clojure.core. At least a couple of times I found myself wanting to dissoc something nested deeply in a re-frame db. While writing this post, I actually realized that there is an effort to bring it to the core library, but looking at the last time somebody touched that file, one wonders if that is going to happen.

(def db {:users {0 {:name "Alan Turing"}
                 1 {:name "Lady Gaga"}
                 2 {:name "Claude Shannon"}}})
(dissoc-in db [:users 1])
Map {:users: Map}

Edit: I actually realized that the Why Not A Function series already covered dissoc-in, so I should better check what has already been discussed elsewhere.

Runtimes (1)