有没有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。
提前感谢!
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"
首先,你应该看看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}}
不过我没有彻底测试它。