大佬教程收集整理的这篇文章主要介绍了‘jQuery’类型函数用于操作clojure贴图,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
(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.
提前致谢!
否则:如果你想做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}}
我没有彻底测试它.
以上是大佬教程为你收集整理的‘jQuery’类型函数用于操作clojure贴图全部内容,希望文章能够帮你解决‘jQuery’类型函数用于操作clojure贴图所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。