Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
';jQuery';用于操作clojure映射的类型函数_Clojure_Clojurescript_Noir - Fatal编程技术网

';jQuery';用于操作clojure映射的类型函数

';jQuery';用于操作clojure映射的类型函数,clojure,clojurescript,noir,Clojure,Clojurescript,Noir,是否有jQuery类型的函数来解决遍历嵌套映射的问题 例如,如果我的配置如下所示: (def fig {:config {:example {:a "a" :b "b" :c "c"} :more {:a "a" :b "b" :c "c"}}}) (-> fig ($ [:config :example :a] #(str % "a

是否有jQuery类型的函数来解决遍历嵌套映射的问题

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

  (def fig
    {:config
      {:example
        {:a "a"
         :b "b"
         :c "c"}
       :more
        {:a "a"
         :b "b"
         :c "c"}}})
  (->  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"}}}
我还没有找到一种使用assoc和dissoc操作嵌套持久数据结构的好方法。但是,如果有一种
jquery
风格的方法来操作地图,那么我可以编写如下代码:

  (def fig
    {:config
      {:example
        {:a "a"
         :b "b"
         :c "c"}
       :more
        {:a "a"
         :b "b"
         :c "c"}}})
  (->  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 DOM


提前谢谢

中的更新
是更新嵌套地图的强大功能

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))))))))
(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-select {:a {:b 1} :b 2} [:b])
(clj-query-update {:c {:a {:b 1} :b 2}} [:c :b] #(* % 2))
它应该产生:

(1 2)
{:c {:a {:b 2} :b 4}}
更新/替换:

(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))))))))
(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-select {:a {:b 1} :b 2} [:b])
(clj-query-update {:c {:a {:b 1} :b 2}} [:c :b] #(* % 2))
它应该产生:

(1 2)
{:c {:a {:b 2} :b 4}}

但是我没有彻底测试它。

+1有没有办法更改更新以使其更像jquery?(原因是我试图操纵hiccup结构)为了完整性:还有
进入
。谢谢!这将给我一个很好的模板玩!这段代码的问题是每次调用它时都会创建一个新的结构。我不认为updatein会这样做,而是返回originalIn通用Clojure函数的修改版本,该函数不修改传递的对象。它的实现方式使得克隆clojure数据结构非常快。除非你是指(into{}…部分?是的,
into{}
部分就是我刚才提到的,现在应该只替换更改的子映射。