Fork me on GitHub
#specter
<
2024-02-11
>
Pranav Ram08:02:19

Hello Given a tree with a root node and children as :successors, is there a better way than tree-seq to pull out all the "nodes" into something like this in specter?

(def initial-nodes
  [{:id "1" :position {:x 0 :y 0} :data {:label "1"}}
   {:id "2" :position {:x 0 :y 100} :data {:label "2"}}])
I'm using the current method:
(defn tree-nodes [tree]
  (tree-seq
   (fn [node] (seq (:successors node))) ; Branching function: checks if a node has successors
   :successors                           ; Children function: gets the successors of a node
   tree))                                ; The tree to traverse

(def hierarchy-nodes (->> (tree-nodes sa-hierarchy)
                          (map #(dissoc % :successors))
                          (map (fn [node] {:id (:id node) :position {:x 0 :y 0} :data (assoc node :label (:name node))}))))
Follow up, given the following structure:
(def initial-nodes
  [{:id "1" :position {:x 0 :y 0} :data {:label "1"}}
   {:id "2" :position {:x 0 :y 100} :data {:label "2"}}])

(def initial-edges
  [{:id "e1-2" :source "1" :target "2"}])
Can it be put back into a nested tree with the property "successors" linking them?