Created
September 30, 2017 08:12
-
-
Save hkjels/5637e59933d4c8cebf73d4052d75b2f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn find-in | |
"Find all of the keys & indices of the [tree] that leads to the | |
value of [s]earch" | |
[tree s] | |
(some | |
(fn [[k v]] | |
(cond | |
(= v s) [k] | |
(map? v) (if-let [r (find-in v s)] | |
(into [k] r)) | |
(vector? v) (letfn [(append [n m] (when-let [r (find-in m s)] | |
(conj [k] n r)))] | |
(when-let [b (-> (keep-indexed append v) | |
(flatten))] | |
(when (last b) [b]))))) | |
tree)) | |
(find-in {:a :_ :b :_ :c {:d "foo" :e "bar"} :f [{:g "pale" :h "brown" :i {:j "ale" :k [{:l "brown"}]}}]} | |
;"bar" | |
"brown" | |
;"ale" | |
) | |
;; current result => ([:f 0 :h]) | |
;; wanted result => ([:f 0 :h] [:f 0 :i :k 0 :l]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment