Search code examples
clojurespecter

Removing nested values with Specter in Clojure


Suppose that I have a Clojure map like this:

(def mymap {:a [1 2 3] :b {:c [] :d [1 2 3]}})

I would like a function remove-empties that produces a new map in which entries from (:b mymap) that have an empty sequence as a value are removed. So (remove-empties mymap) would give the value:

{:a [1 2 3] :b {:d [1 2 3]}}

Is there a way to write a function to do this using Specter?


Solution

  • (update my-map :b (fn [b]
                        (apply dissoc b 
                               (map key (filter (comp empty? val) b)))))