This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2024-01-01
Channels
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?
(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 the tree. Using the previous snippet you shared, I'm able to get the initial-edges created. Just need to figure how to get the nodes and put back the nodes and edges into the tree again via meander.
Code for get the edges from the original tree:
(defn extract-source-destination-from-hierachy [input]
(->> (m/search input
{:id ?id
:successors (m/scan {:successors _ & ?without-children})}
{:id (str ?id "-|-" (:id ?without-children)) :source ?id :target (:id ?without-children)}
{:successors (m/scan (m/cata ?child))}
?child)
(into [])))
(def hierarchy-edges
(extract-source-destination-from-hierachy sa-hierarchy))