使用拉鍊。 在REPL:
user> coll
[{:res [{:a [{:thekey ["the value I am looking for"]}]} {:res 1} {:res 1}]}]
user> (require '[clojure.zip :as z])
nil
user> (def cc (z/zipper coll? seq nil coll))
#'user/cc
user> (loop [x cc]
(if (= (z/node x) :thekey)
(z/node (z/next x))
(recur (z/next x))))
["the value I am looking for"]
更新:
這個版本是有缺陷的,因爲它並不關心:thekey是在地圖的關鍵,或者只是一個載體關鍵字,因此它會給予coll [[:thekey [1 2 3]]]
不需要的結果。下面是一個更新版本:
(defn lookup-key [k coll]
(let [coll-zip (z/zipper coll? #(if (map? %) (vals %) %) nil coll)]
(loop [x coll-zip]
(when-not (z/end? x)
(if-let [v (-> x z/node k)] v (recur (z/next x)))))))
在REPL:
user> (lookup-key :thekey coll)
["the value I am looking for"]
user> (lookup-key :absent coll)
nil
,假設我們已經在科爾的地方相同的關鍵字在一個載體:
(def coll [{:res [:thekey
{:a [{:thekey ["the value I am looking for"]}]}
{:res 1} {:res 1}]}])
#'user/coll
user> (lookup-key :thekey coll)
["the value I am looking for"]
這是我們所需要的。
必須收集矢量和地圖嗎?或者是否允許其他類型? –
您能否澄清/提供您想要的返回值樣本? – jmargolisvt
@Elogent我只有向量,原始值和映射(即沒有集合,這些值來自mongodb + congomongo數據庫)。 – nha