Clojure:search/replace 动态嵌套 map/seq 中的值

Clojure: search/replace values in a dynamic nested map/seq

我有一个动态创建的地图数据结构,稍后将被解析为 JSON。因此嵌套级别等是未知的并且取决于数据。

如果一个键有多个值,它们将表示为序列中的映射。

这是一个例子:

{:key "value"
:anotherKey "anotherValue"
:foo "test"
:others [ {:foo "test2"} {:foo "test3"} ]
:deeper {:nesting {:foo "test4"} }
}

我现在想搜索键 :foo 并将 "/bar" 附加到值。

结果应该return修改后的图:

{:key "value"
:anotherKey "anotherValue"
:foo "test/bar"
:others [ {:foo "test2/bar"} {:foo "test3/bar"} ]
:deeper {:nesting {:foo "test4/bar"} }
}

实现该目标的简洁明了的方法是什么?

我尝试了一种递归方法,但除了大型数据结构的内存问题之外,我还在为 return 附加值而苦苦挣扎。

可能还有比这更简单的:

(clojure.walk/prewalk 
  (fn [m] 
    (if (and (map? m) (:foo m)) 
      (update-in m [:foo] #(str % "/bar")) 
      m)) 
  {:key "value"
   :anotherKey "anotherValue"
   :foo "test"
   :others [{:foo "test2"} {:foo "test3"}]
   :deeper {:nesting {:foo "test4"}}})

=>
{:anotherKey "anotherValue", 
 :key "value", 
 :deeper {:nesting {:foo "test4/bar"}}, 
 :foo "test/bar", 
 :others [{:foo "test2/bar"} {:foo "test3/bar"}]}