Fork me on GitHub
#specter
<
2020-10-05
>
Patrick Farwick05:10:09

Is there a good way to get the value of a key that is the most deeply nested in a map? The map could have any level of nesting. So if I have {:a {:a 1 :b {:c 2 :d {:a {:e 4}}}}} I want {:e 4} but am having trouble getting there. Thanks

Jeff Evans16:10:03

you can use recursion.

(def GET-E [(s/recursive-path [] p
                    (s/if-path map?
                               (s/if-path (s/must :e) (s/continue-then-stay [s/MAP-VALS p]) [s/MAP-VALS p]) s/STOP)) (s/submap [:e])])

(s/select GET-E {:a {:a 1 :b {:c 2 :d {:a {:e 4 :l 5}}}}})
=> [{:e 4}]