2
說,我們有一個包含嵌套的數據結構集合鍵序列的功能:Clojure的:即搜索在嵌套HashMap中的VAL並返回所瓦爾包含
(def coll
{:a "aa"
:b {:d "dd"
:e {:f {:h "hh"
:i "ii"}
:g "gg"}}
:c "cc"})
我想創建了嵌套結構中的任何地方搜索VAL並返回所瓦爾包含
(search-parents "gg" coll) ; or (search-parents coll "gg")
;> [:b :e :g]
由於按鍵序列的功能!
我想clojure.walk過,但是這需要一個背景下,拉鍊也許可以工作 – jozefg
這似乎這個解決方案是有問題的。我從[find-in coll「gg」)返回[:b:e:f]。它似乎源於(vec nil)返回[]。稍微改變似乎有效:(defn find-in [coll x](some(fn [[kv](cond(= vx)[k](map?v)(if-let [r(find-in vx) ](into [k] r))))coll)) –
好人!只需在[[k v]]上加上一個方形括號即可,而且效果很好!也許DaoWen你想更新你的答案,所以我標記你的答案被接受。謝謝! – leontalbot