4

有没有 jQuery 类型的函数来解决遍历嵌套地图的问题?

例如,如果我的配置如下所示:

  (def fig
    {:config
      {:example
        {:a "a"
         :b "b"
         :c "c"}
       :more
        {:a "a"
         :b "b"
         :c "c"}}})

我还没有想出用 assoc 和 dissoc 操作嵌套持久数据结构的好方法。但是,如果有一种jquery风格的方式来操作地图,那么我可以编写如下代码:

  (->  fig
    ($ [:config :example :a] #(str % "a"))
    ($ [:config :b] #(str % "b")))

  Giving this output:

  {:config
    {:example
      {:a "aa"
       :b "bb"
       :c "c"}
     :more
      {:a "a"
       :b "bb"
       :c "c"}}}

对于选择器来说是这样的:

($ fig [:config :example :a])
  ;=> "a"

($ fig [:config :b])
  ;=> {[:config :example :b] "b", 
  ;    [:config :more :b] "b"}

所以本质上,我正在寻找一种jayq用于操作 clojure 对象而不是 html doms 的实现。

提前致谢!

4

2 回答 2

6

update-in是更新嵌套地图的绝佳功能。

user> (def data {:config
{:example  {:a "a" :b "b" :c "c"}}
 :more {:a "a" :b "b" :c "c"}})

user> (pprint (update-in data [:config :example] assoc :d 4))

{:config {:example {:a "a", :c "c", :b "b", :d 4}},
 :more {:a "a", :c "c", :b "b"}}

assoc-in可能更接近你想要的

user> (pprint (assoc-in data [:config :example :d] 4))
{:config {:example {:a "a", :c "c", :b "b", :d 4}},
 :more {:a "a", :c "c", :b "b"}}

为了在不更改值的情况下读取值,您可以使用关键字在地图中查找自己的事实来编写比 jquery 表单更紧凑的表单

user> (-> data :config :example :a)
"a"
于 2012-06-29T19:23:43.417 回答
4

首先,您应该查看 Enlive。

否则:如果你想做 jQuery 所做的事情(当然非常简化)——而不是仅仅调用 update-in:

选择:

(defn clj-query-select [obj path]
  (if (empty? path)
    (list obj)
    (when (map? obj)
      (apply concat
        (remove nil? 
          (for [[key value] obj]
            (clj-query-select
              value 
              (if (= key (first path)) (rest path) path))))))))

来电:

(clj-query-select {:a {:b 1} :b 2} [:b])

它应该产生:

(1 2)

更新/替换:

(defn clj-query-update [obj path fn]
  (if (empty? path)
    (fn obj)
    (if (map? obj)
      (into obj
        (remove nil?
          (for [[key value] obj]
            (let [res (clj-query-update 
                        value 
                        (if (= key (first path)) (rest path) path)
                        fn)]
          (when (not= res value) [key res])))))
      obj)))

来电:

(clj-query-update {:c {:a {:b 1} :b 2}} [:c :b] #(* % 2))

它应该产生:

{:c {:a {:b 2} :b 4}}

虽然我没有彻底测试它。

于 2012-06-29T20:47:12.570 回答