Fork me on GitHub
#specter
<
2017-10-14
>
aisamu02:10:33

Hi! Is it possible to collect a value of every "branch" separately instead of all of them? I'm trying to get all possible get-in path vectors for a given nested map (preferably excluding :root):

(magic {:root {:a {:b 1 :c 2} :d {:e 1 :f 2}}})
 => [[:a :b] [:a :c] [:d :e] [:d :f]]`
The best I could manage so far was this:
(select [MAP-VALS (collect MAP-KEYS) MAP-VALS MAP-KEYS]
        {:root {:a {:b 1 :c 2} :d {:e 1 :f 2}}})
=> [[[:a :d] :b] [[:a :d] :c] [[:a :d] :e] [[:a :d] :f]]
...which is almost there, but collect grabs all the branches' values at every step. I could massage that second structure into what I need, but I suspect there is a nicer way of approaching this 🙂

nathanmarz16:10:12

@aisamu it's like this:

(def data {:root {:a {:b 1 :c 2} :d {:e 1 :f 2}}})

(select [:root ALL (collect-one FIRST) LAST MAP-KEYS] data)

nathanmarz16:10:38

if your data is recursive, this example is instructive for how to collect all the paths (which can be arbitrary/varying lengths): https://github.com/nathanmarz/specter/wiki/Using-Specter-Recursively#find-the-index-route-of-a-value-within-a-data-structure

aisamu17:10:00

Thanks! That makes a lot of sense!