From 17c7eff5b8e876ac8f9f972aaa24a239faec62ad Mon Sep 17 00:00:00 2001 From: Eric Schulte Date: Fri, 16 Apr 2010 22:47:06 -0600 Subject: [PATCH] adding ability to remember and graph structure of propagators --- propagator.org | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/propagator.org b/propagator.org index de22092..08bafc2 100644 --- a/propagator.org +++ b/propagator.org @@ -18,9 +18,19 @@ [name state] `(def ~name (agent ~state))) + (def prop-graph {}) + + (defmacro remember-prop [name in-cells out-cells] + `(def prop-graph + (assoc prop-graph + (quote ~name) + {:in-cells (map (fn [x#] (name x#)) (quote ~in-cells)) + :out-cells (map (fn [x#] (name x#)) (quote ~out-cells))}))) + (defmacro propagator "Define a new propagator." [name in-cells out-cells & body] `(do + (remember-prop ~name ~in-cells ~out-cells) (defn ~(with-meta name (assoc (meta name) :in-cells in-cells :out-cells out-cells)) @@ -50,6 +60,7 @@ #+end_src * extensions ** graphs of the propagator network +draw a graph in a JFrame #+begin_src clojure (use 'vijual) (import '(javax.swing JFrame JPanel) @@ -71,21 +82,32 @@ (doto frame (.add panel) .pack (.setSize (+ offset width) (+ offset height)).show))) #+end_src +pull a graph from a the propagators saved in the =prop-graph= variable +#+begin_src clojure + (defn graph-propagators [] + (vec (set + (apply concat + (map (fn [key] + (let [hsh (get prop-graph key)] + (concat + (map #(vec (list % (name key))) (get hsh :in-cells)) + (map #(vec (list (name key) %)) (get hsh :out-cells))))) + (keys prop-graph)))))) +#+end_src + * usage ** doubling a number -- simplest #+begin_src clojure - (cell in 0) - (cell out 0) - (propagator doubler [in] [out] (* 2 in)) + (cell in-c 0) + (cell out-c 0) + (propagator doubler [in-c] [out-c] (* 2 in)) ;; then any updates to in - (set-cell in 1) + (set-cell in-c 1) ;; will propagate to out - @out + @out-c #+end_src ** square roots -- heron -results in errors... - #+begin_src clojure (cell guess 1) (cell x 9) @@ -95,8 +117,8 @@ results in errors... (propagator enough [x guess] [done] (if (< (abs (- (* guess guess) x)) @margin) true false)) - (propagator heron [x guess] [guess] - (if (enough x guess) + (propagator heron [x done guess] [guess] + (if done guess (/ (+ guess (/ x guess)) 2.0))) #+end_src -- 2.11.4.GIT