specter

Latest version: 1.1.3
Patrick Farwick 2020-10-05T05:22:09.009100Z

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

2020-10-05T16:18:03.009500Z

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}]