Created
November 30, 2023 14:17
-
-
Save vendethiel/69fa948fcd344b7720ffdceb77062773 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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