在Clojure中使用Specter删除嵌套值

时间:2017-06-25 21:32:35

标签: clojure specter

假设我有一个像这样的Clojure地图:

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

我想要一个函数remove-empties,它会生成一个新的映射,其中删除了具有空序列作为值的(:b mymap)条目。所以(remove-empties mymap)会给出值:

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

有没有办法使用Spectre编写一个函数来执行此操作?

7 个答案:

答案 0 :(得分:3)

以下是Specter

的使用方法
(use 'com.rpl.specter)

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

在英语中,这表示"在:b下,找到empty?的所有地图值。将它们设置为NONE,即删除它们。"

答案 1 :(得分:2)

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

答案 2 :(得分:1)

这是幽灵解决方案:

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

答案 3 :(得分:0)

到目前为止,我还没有找到幽灵filterer的方法,因为当我测试过滤器时,他们似乎每次接收两个地图条目(一次作为地图条目,一次作为一个双长度矢量)并给予这些之间的不同结果似乎会引起问题。但是,我们不应该删除可能出现的任何位置的空序列,只需映射它们是值的条目。

我似乎确实采用了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]}}

答案 4 :(得分:0)

我也不知道幽灵,但在普通的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))

警告:对于极其嵌套的数据结构,它可能会堆叠溢出。

答案 5 :(得分:0)

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

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

参考using specter recursively上的Wiki。

答案 6 :(得分:-1)

虽然我对Spectre不是很熟悉,但除postwalk解决方案外,您还可以使用tupelo.forest from the Tupelo library来解决此问题。您需要将数据重新排列成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]]]])
  )))