Hi folks! I’m fiddling with Specter for quite a while now and am still struggling with a seemingly basic task — doing a post-order tree traversal which only selects the leaves. I’m trying to it in a generic way, for all possible data structures, but let’s keep it simple for now and do this traversals over a linearized form that looks like this:
(def test-tree-vec [1 [2 [4 [8] 5]] [3 [6 [9 10] 7]]])
The output I expect from the selection is, obviously:
[8 4 5 2 9 10 6 7 3 1]
So far I tried out walker, recursive-path, cond-path, continue-then-stay, filterer, nthpath, srange-dynamic, preds and almost all Unparameterized Navigators — in every combination imaginable — no luck.
That said, any help with this task will be appreciated!
---
P.S. Pre-order traversal is a no-brainer, of course:
(sp/select (sp/walker int?) test-tree-vec)
=> [1 2 4 8 5 3 6 9 10 7]
P.P.S. Apart from googling, I’ve read https://github.com/redplanetlabs/specter/wiki/List-of-Navigators https://github.com/redplanetlabs/specter/wiki/Cheat-Sheet https://github.com/nathanmarz/specter-wiki/blob/master/Using-Specter-Recursively.md (in full and at least 3 times), the source code, the https://github.com/latacora/eidolon/blob/main/src/eidolon/core.clj source code and http://nathanmarz.com/blog/functional-navigational-programming-in-clojurescript-with-sp.html http://nathanmarz.com/blog/functional-navigational-programming-in-clojurescript-with-sp.html http://nathanmarz.com/blog/clojures-missing-piece.html.@smith.adriane heya! Yeah, as a Math/CS grad myself, I can firmly state that this holds true only if you consider them sets. 😃 That’s the whole point of having 3 types of DFS traversals as well as a single BFS — different orders of the result.
Still, I would like to figure out the proper way of tree traversals with Specter in general. Thank you, @nathanmarz! It seems like I missed this multi-path navigator yesterday, for sure.
BTW, love what you guys doing in RPL. Good luck in promoting RAMA! 🤞 A great piece of tech!
Maybe I'm forgetting my CS101, but if you're only visiting the leaves of a tree, then I don't think pre-order vs post-order traversal will change the order your visit the leaves.
It seems like you're trying to visit nodes in order from deepest to shallowest?
I think this does what you want:
(def data [1 [2 [4 [8] 5]] [3 [6 [9 10] 7]]])
(def TreeValues
(recursive-path [] p
(multi-path [ALL vector? p]
[ALL (complement vector?)])))
(select TreeValues data)
;; => [8 4 5 2 9 10 6 7 3 1]