2

在我的 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"}}

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

4

2 回答 2

3

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解决方案

(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 的解决方案

(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"}}
于 2014-12-26T00:02:04.577 回答
0

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

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

您当然可以使用上述的通用版本来实现您在函数中建议的语法(最有可能使用 areduce而不是->表单,并且对每个嵌套的键选择进行递归调用)。解构并没有多大帮助,它使绑定嵌套数据很方便,但对于构​​造值并没有那么有用。

以下是我将如何使用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"}}

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

于 2014-12-25T22:10:35.873 回答