Skip to content

@abp /graph.clj
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Dependency graph iteration. (Part of lib to come soon)
(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)))))
(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}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@abp
Owner

Now with cached :topo-sort in compile-graph. (Part of lib to come soon)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.