Skip to content

Instantly share code, notes, and snippets.

@jeroenvandijk
Created February 15, 2013 08:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeroenvandijk/4959201 to your computer and use it in GitHub Desktop.
Save jeroenvandijk/4959201 to your computer and use it in GitHub Desktop.
simple example of visualizing prismatic graphs with graphviz
(require '[plumbing.core :refer [fnk]])
(require '[plumbing.graph :as graph])
(defn calculate-deps [f]
(map (fn [parent] [parent (first f)]) (-> f second meta first second first keys)))
(defn print-dot [graph]
(println (clojure.string/join "\n"
(map (fn [r] (str "\""(first r) "\" -> \"" (second r) "\";"))
(mapcat (fn [k] (calculate-deps k)) (graph/->graph graph))))))
;; Example graph
(def stats-graph
"A graph specifying the same computation as 'stats'"
{:n (fnk [xs] (count xs))
:m (fnk [xs n] (/ (+ identity xs) n))
:m2 (fnk [xs n] (/ (+ #(* % %) xs) n))
:v (fnk [m m2] (- m2 (* m m)))})
(print-dot stats-graph)
; Outputs this:
;
; digraph "example" {
; ":xs" -> ":n";
; ":xs" -> ":m";
; ":n" -> ":m";
; ":xs" -> ":m2";
; ":n" -> ":m2";
; ":m2" -> ":v";
; ":m" -> ":v";
; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment