Skip to content

Instantly share code, notes, and snippets.

@vendethiel
Created November 30, 2023 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vendethiel/69fa948fcd344b7720ffdceb77062773 to your computer and use it in GitHub Desktop.
Save vendethiel/69fa948fcd344b7720ffdceb77062773 to your computer and use it in GitHub Desktop.
(def tree
{:nodes
[
{:function "start"
:nodes [{:call "end" :nodes [{:call "middle"}]}]
}
{:function "middle"
:nodes [{:call "start"} {:call "end"}]}
{:function "end"
:nodes [{:call "start"}]}
]})
(defn catvec [a b]
(vec (concat a b)))
(defn collect [nodes state]
(reduce (fn [{:keys [known declare] :as res} {:keys [call nodes function]}]
(let [updated
(if call
(if (or (.contains declare call) (.contains known call))
res
(update-in res [:declare] conj call))
; Function can't be redefined
(if function
(update-in res [:known] conj function)
res))
nested
(if nodes (:declare (collect nodes updated)))]
{:known (:known updated)
:declare (distinct (vec (concat (:declare updated) nested)))}))
state nodes))
(collect (:nodes tree) {:declare [] :known []})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment