Fork me on GitHub
#specter
<
2017-04-22
>
jeremyraines21:04:39

anyone have a way to get the ancestors of a node in a tree, with or without specter.zipper?

jeremyraines21:04:42

closest I can get is collecting the path with

clojure
(def NodeAndParents
  (s/recursive-path [id] p
    (s/if-path #(= (:id %) id)
      s/STAY
      (s/if-path #(empty? (:children %))
        [s/DISPENSE s/STOP]
        [(s/collect-one :id) :children s/ALL p]))))
then using the collected ids to go through the tree again, selecting the nodes whose id is in that set

nathanmarz21:04:44

@jeremyraines yea that's pretty much how you do it with specter

jeremyraines21:04:09

ah ok, cool, thanks

nathanmarz21:04:42

you can change the last part to (if-path [:children ALL] [(collect-one :id) :children ALL p])

nathanmarz21:04:57

calling DISPENSE before STOP doesn't do anything

nathanmarz21:04:32

and if-path has default "else" branch of STOP