根据预期的键过滤地图

时间:2014-12-25 21:43:31

标签: clojure hashmap destructuring

在我的Clojure webapp中,我有各种模型命名空间,其中的函数将地图作为一个agrument,并以某种方式将该映射插入到数据库中。在插入之前,我希望能够从地图中取出所需的键。

这方面的一个基本例子是:

(let [msg-keys [:title :body]
      msg {:title "Hello" :body "This is an example" :somekey "asdf" :someotherkey "asdf"}]
  (select-keys msg msg-keys))

;; => {:title "Hello" :body "This is an example"}
当地图有点复杂并且我想选择一组特定的嵌套键时,

select-keys不是一个选项:

(let [person {:name {:first "john" :john "smith"} :age 40 :weight 155 :something {:a "a" :b "b" :c "c" :d "d"}}]
  (some-select-key-fn person [:name [:first] :something [:a :b]]))

;; => {:name {:first "john"} :something {:a "a" :b "b"}}

有没有办法用核心功能做到这一点?有没有办法纯粹用解构来做这件事?

2 个答案:

答案 0 :(得分:3)

此主题为discussed in the Clojure Google Group以及一些解决方案。

解构可能是最接近"核心"功能,如果您的问题是静态的并且地图具有所有预期的键(从而避免nil),则可能是一个很好的解决方案。它可能看起来像:

(let [person {:name {:first "john" :john "smith"} :age 40 :weight 155 :something {:a "a" :b "b" :c "c" :d "d"}}
      {{:keys [first]} :name {:keys [a b]} :something} person]
  {:name {:first first} :something {:a a :b b}})
;; => {:name {:first "john"}, :something {:a "a", :b "b"}}

以下是适用于您的示例地图的Clojure Google Group主题中的解决方案调查。它们各自对如何指定要选择的嵌套键有不同的看法。

以下是Christophe Grand' s solution

(defprotocol Selector
  (-select [s m]))

(defn select [m selectors-coll]
  (reduce conj {} (map #(-select % m) selectors-coll)))

(extend-protocol Selector
  clojure.lang.Keyword
  (-select [k m]
    (find m k))
  clojure.lang.APersistentMap
  (-select [sm m]
    (into {}
          (for [[k s] sm]
            [k (select (get m k) s)]))))

使用它需要稍微修改一下语法:

(let [person {:name {:first "john" :john "smith"} :age 40 :weight 155 :something {:a "a" :b "b" :c "c" :d "d"}}]
  (select person [{:name [:first] :something [:a :b]}]))
;; => {:something {:b "b", :a "a"}, :name {:first "john"}}

这是Moritz Ulrich的解决方案(他警告说,它不能在seqs作为键的地图上工作):

(defn select-in [m keyseq]
  (loop [acc {} [k & ks] (seq keyseq)]
    (if k
      (recur
        (if (sequential? k)
          (let [[k ks] k]
            (assoc acc k
                   (select-in (get m k) ks)))
          (assoc acc k (get m k)))
        ks)
      acc)))

使用它需要另一种略微修改的语法:

(let [person {:name {:first "john" :john "smith"} :age 40 :weight 155 :something {:a "a" :b "b" :c "c" :d "d"}}]
  (select-in person [[:name [:first]] [:something [:a :b]]]))
;; => {:something {:b "b", :a "a"}, :name {:first "john"}}

这是Jay Fields的解决方案:

(defn select-nested-keys [m top-level-keys & {:as pairs}]
  (reduce #(update-in %1 (first %2) select-keys (last %2)) (select-keys m top-level-keys) pairs))

它使用不同的语法:

(let [person {:name {:first "john" :john "smith"} :age 40 :weight 155 :something {:a "a" :b "b" :c "c" :d "d"}}]
  (select-nested-keys person [:name :something] [:name] [:first] [:something] [:a :b]))
;; => {:something {:b "b", :a "a"}, :name {:first "john"}}

这是Baishampayan Ghose的solution

(defprotocol ^:private IExpandable
             (^:private expand [this]))


(extend-protocol IExpandable
  clojure.lang.Keyword
  (expand [k] {k ::all})

  clojure.lang.IPersistentVector
  (expand [v] (if (empty? v)
                {}
                (apply merge (map expand v))))

  clojure.lang.IPersistentMap
  (expand [m]
    (assert (= (count (keys m)) 1) "Number of keys in a selector map can't be more than 1.")
    (let [[k v] (-> m first ((juxt key val)))]
          {k (expand v)}))

  nil
  (expand [_] {}))


(defn ^:private extract* [m selectors expand?]
  (let [sels (if expand? (expand selectors) selectors)]
    (reduce-kv (fn [res k v]
                 (if (= v ::all)
                   (assoc res k (m k))
                   (assoc res k (extract* (m k) v false))))
               {} sels)))

(defn extract
  "Like select-keys, but can select nested keys.

   Examples -

   (extract [{:b {:c [:d]}} :g] {:a 1 :b {:c {:d 1 :e 2}} :g 42 :xxx 11})
   ;=> {:g 42, :b {:c {:d 1}}}

   (extract [:g] {:a 1 :b {:c {:d 1 :e 2}} :g 42 :xxx 11})
   ;=> {:g 42}

   (extract [{:b [:c]} :xxx] {:a 1 :b {:c {:d 1 :e 2}} :g 42 :xxx 11})
   ;=> {:b {:c {:d 1, :e 2}}, :xxx 11}

   Also see - exclude"
  [selectors m]
  (extract* m selectors true))

它使用另一种语法(参数相反):

(let [person {:name {:first "john" :john "smith"} :age 40 :weight 155 :something {:a "a" :b "b" :c "c" :d "d"}}]
  (extract [{:name [:first]} {:something [:a :b]}] person))
;; => {:name {:first "john"}, :something {:a "a", :b "b"}}

答案 1 :(得分:0)

您最好的选择可能是在结构的每个嵌套部分使用选择键。

(-> person
    (select-keys [:name :something])
    (update-in [:name] select-keys [:first])
    (update-in [:something] select-keys [:a :b]))

您当然可以使用上面的通用版本来实现您在函数中建议的语法(使用reduce而不是->形式,最有可能的是,每个嵌套的递归调用选择键)。解构无济于事,它使绑定嵌套数据变得方便,但对于构​​造值并不是那么有用。

以下是我如何使用reduce和递归:

(defn simplify
  [m skel]
  (if-let [kvs (not-empty (partition 2 skel))]
    (reduce (fn [m [k nested]]
              (if nested
                (update-in m [k] simplify nested)
                m))
            (select-keys m (map first kvs))
            kvs)
    m))

请注意,您提出的参数格式不方便,所以我稍微改了一下

user=> (simplify {:name {:first "john" :john "smith"}
                  :age 40
                  :weight 155
                  :something {:a "a" :b "b" :c "c" :d "d"}}
                 [:name [:first nil] :something [:a nil :b nil]])
{:something {:b "b", :a "a"}, :name {:first "john"}}

您提出的语法需要更复杂的实现