使用Clojure中的Specter删除嵌套值

使用Clojure中的Specter删除嵌套值,clojure,specter,Clojure,Specter,假设我有这样一张Clojure地图: (def mymap {:a [1 2 3] :b {:c [] :d [1 2 3]}}) 我想要一个函数removeepties,它生成一个新的映射,其中(:b mymap)中具有空序列作为值的条目被删除。因此(删除清空mymap)将给出以下值: {:a [1 2 3] :b {:d [1 2 3]}} 有没有办法用Specter编写一个函数来实现这一点?到目前为止,我还没有找到一种使用Specter的过滤器的方法,因为当我测试过滤器时,它们似乎会两

假设我有这样一张Clojure地图:

(def mymap {:a [1 2 3] :b {:c [] :d [1 2 3]}})
我想要一个函数removeepties,它生成一个新的映射,其中(:b mymap)中具有空序列作为值的条目被删除。因此(删除清空mymap)将给出以下值:

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

有没有办法用Specter编写一个函数来实现这一点?

到目前为止,我还没有找到一种使用Specter的
过滤器的方法,因为当我测试过滤器时,它们似乎会两次接收每个映射条目(一次作为映射条目,一次作为两个长度向量),并且在这两个条目之间给出不同的结果似乎会导致问题。然而,我们不应该在任何可能出现的地方删除空序列,而应该将它们作为值的条目映射到其中

不过,我似乎确实得到了一种你可能仍然感兴趣的
clojure.walk
方法

(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]}}

虽然我对Specter不是很熟悉,但是除了
postwalk
解决方案之外,您还可以使用
tupelo.forest
解决这个问题。您确实需要将数据重新排列为Hiccup或Enlive格式,这样就很容易识别没有子节点的任何节点:

(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]]]])
  )))

我也不知道斯佩克特,但这在普通的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))

警告:对于非常嵌套的数据结构,它可能会堆栈溢出。

这是specter解决方案:

(update my-map :b (fn [b]
                    (apply dissoc b 
                           (map key (filter (comp empty? val) b)))))
(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]}}
以下是如何使用:


在英语中,这表示“在
:b
下,查找
为空的所有映射值?
。将它们设置为
,即删除它们。”

假设我们只需要深入一层,而不需要像接受的答案那样递归搜索:

(setval [:b MAP-VALS empty?] NONE mymap)
一种完全递归的解决方案,可在任何级别删除映射中的空值

(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"}}}}

请参考上的wiki。

Specter在香草解决方案中的强大威力
(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"}}}}