>>108785391
(defn topo-sort
[gr]
(loop [res [] ;; resultado
nodes gr ;; estado atual do grafo
]
(let [ready (all-isolated-nodes nodes)] ;; nós isolados agora
(if (empty? ready)
(if (empty? nodes)
res ;; não tenho mais nada
;; Tenho nís isolados mas o grafo já foi percorrido
(throw (Exception. "Ciclo detectado ou dependência não encontrada")))
;; nós isolados e ainda tenho grafo a percorrer
(let [current-node (first ready)
current-node-id (:id current-node)
updated-graph (->> nodes
(remove #(= (:id %) current-node-id))
(map (fn [node] (update node :depends-on disj current-node-id)
)))]
(recur (conj res current-node)
updated-graph))))))
Is this decent clojure? I'm a schemer. Topological sorting my migration files.
;; dummy migrations.edn
[
{:id 0
:file "migrations/00_init.sql"
:description "Create migrations tracking table"}
{:id 1
:file "migrations/01_network_topology.sql"
:description "Create network_topology schema"
:depends-on #{0}}
{:id 2
:file "migrations/02_node.sql"
:description "Create and populate tables related to network nodes"
:depends-on #{0 1}}
{:id 3
:file "migrations/03_link.sql"
:description "Create and populate tables related to network links (edges)"
:depends-on #{0 1 2}}
{:id 4
:file "migrations/04_subnet.sql"
:description "Create and populate tables related to subnets and VLANs"
:depends-on #{0 2 3}}
]