Fork me on GitHub
#specter
<
2017-09-13
>
lvh01:09:39

I’m stuck writing a selector: I’d like to recursively walk down nested maps, and, if I get a choice, deepest first. So, for:

{"a" {"b" {"c" 1}}}
I’d get {"c" 1}, {"b" {"c" 1}}, {"a" {"b" {"c" 1}}}. I’ve been messing around with recursive-path but haven’t gotten far.

lvh01:09:46

writing the nested traversal that just grabs the values at the leaves is easy:

(sr/recursive-path
   [] p
   (sr/cond-path
    map? (sr/stay-then-continue [p sr/MAP-VALS])
    sr/STAY sr/STAY))

lvh01:09:30

This is almost what I want:

(def tree-vals
  (sr/recursive-path
   [] p
   (sr/if-path map?
    [sr/VAL sr/MAP-VALS p]
    sr/STAY)))
is almost what I want: it just returns the 1 too

lvh01:09:28

(sr/NONE still selects nil; and I’m not sure how to say “ok you’re done now” — I don’t think it’s STOP, because that just stops navigation entirely, right?)

lvh06:09:15

Got it:

(def tree-vals
  (sr/recursive-path
   [] p
   (sr/if-path map? (sr/continue-then-stay [sr/MAP-VALS p]))))

nathanmarz11:09:23

@lvh you can omit the [] in continue-then-stay

nathanmarz11:09:34

no performance difference, just a little nicer

lvh14:09:28

Ah, thanks :)