Removing nested values with Specter in Clojure

牧云@^-^@ 提交于 2019-12-12 17:14:26

问题


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?


回答1:


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



回答2:


Here's how to do it with Specter:

(use 'com.rpl.specter)

(setval [:b MAP-VALS empty?] NONE my-map)
=> {:a [1 2 3], :b {:d [1 2 3]}}

In English, this says "Under :b, find all the map values that are empty?. Set them to NONE, i.e. remove them."




回答3:


This is the specter solution:

(ns myns.core
  (:require
   [com.rpl.specter :as spc]))

(def my-map
  {:a [1 2 3]
   :b {:c []
       :d [1 2 3]}})

(defn my-function
  [path data]
  (let [pred #(and (vector? %) (empty? %))]
    (spc/setval [path spc/MAP-VALS pred] spc/NONE data)))

;; (my-function [:b] my-map) => {:a [1 2 3]
;;                               :b {:d [1 2 3]}}



回答4:


I don't know specter either, but this is pretty simple to do in plain clojure.

(defn remove-empties [m]
  (reduce-kv (fn [acc k v]
               (cond (map? v) (let [new-v (remove-empties v)]
                                (if (seq new-v)
                                  (assoc acc k new-v)
                                  acc))
                     (empty? v) acc
                     :else (assoc acc k v)))
             (empty m), m))

Caveat: For extremely nested data structures it might stack overflow.




回答5:


So far I haven't found an approach with specter's filterer, because when I test filterers they seem to receive each map entry twice (once as a map entry and once as a 2-length vector) and giving different results between those seems to cause issues. However, we shouldn't be removing empty sequences anywhere they might appear, just map entries where they're the value.

I did seem to get a clojure.walk approach working that might still interest you, though.

(ns nested-remove
  (:require [com.rpl.specter :as s]          
            [clojure.walk :refer [postwalk]]))


(defn empty-seq-entry? [entry]
  (and (map-entry? entry) (sequential? (val entry)) (empty? (val entry))))

(defn remove-empties [root]
  (postwalk #(if (map? %) (into (empty %) (remove empty-seq-entry? %)) %) root))

(remove-empties mymap) ;;=> {:a [1 2 3], :b {:d [1 2 3]}}



回答6:


Assuming we only need to go one level deep and not search recursively like the accepted answer:

(setval [:b MAP-VALS empty?] NONE mymap)

A fully recursive solution that removes empty values in a map at any level

(def my-complex-map {:a [1] :b {:c [] :d [1 2 3] :e {:f "foo" :g []}}})

; declare recursive path that traverses map values
(declarepath DEEP-MAP-VALS)
(providepath DEEP-MAP-VALS (if-path map? [MAP-VALS DEEP-MAP-VALS] STAY))

(setval [DEEP-MAP-VALS empty?] NONE my-complex-map)
; => {:a [1], :b {:d [1 2 3, :e {:f "foo"}}}}

Reference the wiki on using specter recursively.




回答7:


While I am not very familiar with Specter, in addition to the postwalk solution, you can solve this using tupelo.forest from the Tupelo library. You do need to rearrange the data a bit into Hiccup or Enlive format, then it's easy to identify any nodes with no child nodes:

(ns tst.clj.core
  (:use clj.core tupelo.test)
  (:require
    [tupelo.core :as t]
    [tupelo.forest :as tf] ))
(t/refer-tupelo)

(defn hid->enlive [hid]
  (tf/hiccup->enlive (tf/hid->hiccup hid)))

(defn empty-kids?
  [path]
  (let [hid     (last path)
        result  (and (tf/node-hid? hid)
                  (empty? (grab :kids (tf/hid->tree hid))))]
       result))

; delete any nodes without children
(dotest
  (tf/with-forest (tf/new-forest)
    (let [e0          {:tag     :root
                       :attrs   {}
                       :content [{:tag     :a
                                  :attrs   {}
                                  :content [1 2 3]}
                                 {:tag     :b
                                  :attrs   {}
                                  :content [{:tag     :c
                                             :attrs   {}
                                             :content []}
                                            {:tag     :d
                                             :attrs   {}
                                             :content [1 2 3]}
                                            ]}]}
          root-hid    (tf/add-tree-enlive e0)
          empty-paths (tf/find-paths-with root-hid [:** :*] empty-kids?)
          empty-hids  (mapv last empty-paths)]
       (is= (hid->enlive root-hid) ; This is the original tree structure (Enlive format)
         {:tag   :root,
          :attrs {},
          :content
                 [{:tag   :a,
                   :attrs {},
                   :content
                          [{:tag :tupelo.forest/raw, :attrs {}, :content [1]}
                           {:tag :tupelo.forest/raw, :attrs {}, :content [2]}
                           {:tag :tupelo.forest/raw, :attrs {}, :content [3]}]}
                  {:tag   :b,
                   :attrs {},
                   :content
                          [{:tag :c, :attrs {}, :content []}
                           {:tag   :d,
                            :attrs {},
                            :content
                                   [{:tag :tupelo.forest/raw, :attrs {}, :content [1]}
                                    {:tag :tupelo.forest/raw, :attrs {}, :content [2]}
                                    {:tag :tupelo.forest/raw, :attrs {}, :content [3]}]}]}]})
      (apply tf/remove-hid empty-hids) ; remove the nodes with no child nodes
      (is= (hid->enlive root-hid) ; this is the result (Enlive format)
        {:tag   :root,
         :attrs {},
         :content
                [{:tag   :a,
                  :attrs {},
                  :content
                         [{:tag :tupelo.forest/raw, :attrs {}, :content [1]}
                          {:tag :tupelo.forest/raw, :attrs {}, :content [2]}
                          {:tag :tupelo.forest/raw, :attrs {}, :content [3]}]}
                 {:tag   :b,
                  :attrs {},
                  :content
                         [{:tag   :d,
                           :attrs {},
                           :content
                                  [{:tag :tupelo.forest/raw, :attrs {}, :content [1]}
                                   {:tag :tupelo.forest/raw, :attrs {}, :content [2]}
                                   {:tag :tupelo.forest/raw, :attrs {}, :content [3]}]}]}]})
      (is= (tf/hid->hiccup root-hid) ; same result in Hiccup format
        [:root
         [:a
          [:tupelo.forest/raw 1]
          [:tupelo.forest/raw 2]
          [:tupelo.forest/raw 3]]
         [:b
          [:d
           [:tupelo.forest/raw 1]
           [:tupelo.forest/raw 2]
           [:tupelo.forest/raw 3]]]])
  )))


来源:https://stackoverflow.com/questions/44750837/removing-nested-values-with-specter-in-clojure

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!