Skip to content

Instantly share code, notes, and snippets.

@abp
Last active October 13, 2015 04:57
Show Gist options
  • Save abp/4142595 to your computer and use it in GitHub Desktop.
Save abp/4142595 to your computer and use it in GitHub Desktop.
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}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@abp
Copy link
Author

abp commented Nov 25, 2012

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