-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform.cljs
More file actions
32 lines (27 loc) · 914 Bytes
/
transform.cljs
File metadata and controls
32 lines (27 loc) · 914 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
(ns got-graph.transform)
(def ex1 {:data 'main
:children [{:data 'foo}
{:data 'bar
:children [{:data 'baz} {:data 'baz2}]}]})
(defn traverse-layers [node depth]
(let [data [depth (str (:data node))]]
(if-let [children (:children node)]
(conj (mapcat #(traverse-layers % (inc depth)) children) data)
[data])))
(defn graph->layers [graph]
(->> (traverse-layers graph 0)
(group-by first)
vals
(map #(map second %))))
(defn traverse-connections [node]
(let [data (str (:data node))
children (:children node)
result {data (map #(str (:data %)) children)}]
(if (seq children)
(conj (mapcat traverse-connections children) result)
[result])))
(defn graph->connections [graph]
(->> (traverse-connections graph)
(apply merge)
(remove #(empty? (second %)))
(into {})))