Last active
October 13, 2015 04:57
-
-
Save abp/4142595 to your computer and use it in GitHub Desktop.
Dependency graph iteration. (Part of lib to come soon)
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
(defn iterate | |
([graph] | |
(iterate graph {})) | |
([graph state] | |
(iterate graph state | |
(->> graph | |
:topo-sort | |
(remove nil?) | |
first | |
fnk->key))) | |
([graph state start-key] | |
(let [start-fn (key->fnk graph start-key) | |
dependents (deps/transitive-dependents graph start-fn) | |
deps (conj (set (mapcat (partial deps/transitive-dependencies graph) | |
(conj dependents start-fn ))) | |
start-fn) | |
calc? (conj (union deps dependents) start-fn) | |
calc-topo (filter calc? (:topo-sort graph))] | |
(-> state | |
(iterate* calc-topo update-deps deps) | |
(iterate* calc-topo update-dependents dependents))))) |
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 g | |
{:a 1 | |
:d/b 2 | |
:b/a (fnk [a] (* a 7)) | |
:x (fnk [b/a [d/b 2] [c 3]] (+ b/a d/b c))}) | |
(def cg (compile-graph g)) | |
(iterate cg) | |
; => {:x 12, :b/a 7, :d/b 2, :a 1} | |
(iterate cg {:a 7 :x 15} :a) | |
; => {:d/b 2, :b/a 49, :x 54, :a 7} | |
(iterate cg {:b/a 11 :a 7 :x 15} :x) | |
; => {:d/b 2, :x 15, :a 7, :b/a 11} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Now with cached :topo-sort in compile-graph. (Part of lib to come soon)