Skip to content

Instantly share code, notes, and snippets.

@saulshanabrook
Created July 1, 2017 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saulshanabrook/feaed7e04079428cad5904bf791ed77e to your computer and use it in GitHub Desktop.
Save saulshanabrook/feaed7e04079428cad5904bf791ed77e to your computer and use it in GitHub Desktop.
random ideas for clojush logging
(ns clojush.log
(require [plumbing.graph :as graph]
[plumbing.core :as plumbing]
[plumbing.map :as map]
[plumbing.fnk.pfnk :as pfnk]
[lazy-map.core :as lazy-map]))
(defmacro lazy-map-self
[map]
`(do
(def self (lm/lazy-map ~map))
self))
(let [a (conj (lm/lazy-map {:a 123 :b (:three self)}) {:three 1})]
(def ^:dynamic self)
(binding [self a]
(lm/force-map a)))
(defn lazy-compile
"Copies Plumatic's core lazy-compile but uses new lazy map library so that
we can access inputs keys without computing the rest"
[g]
(graph/simple-hierarchical-compile
g
false
(fn [m] (reduce-kv assoc (lazy-map/lazy-map {}) m))
(fn [m k f] (assoc m k (delay (graph/restricted-call f m))))))
;; https://github.com/plumatic/plumbing/issues/4#issuecomment-13449331
(defn merge-inputs [f]
(pfnk/fn->fnk
(fn [m] (merge m (f m)))
[(pfnk/input-schema f)
(merge (pfnk/output-schema f)
(map/keep-leaves identity (pfnk/input-schema f)))]))
(def merge-lazy-compile (comp merge-inputs lazy-compile))
(def label-to-->data (atom {}))
(def label-to-data (atom {}))
(def label-to-handlers (atom {}))
(defn on-event!
[label handler]
(swap! label-to-handlers update label conj handler))
(defn event-graph!
[label graph]
(let [->data (merge-lazy-compile graph)]
(swap! label-to-->data assoc label ->data)))
(defn event!
[label kwargs]
(let [->data (label @label-to-->data)
data (->data kwargs)]
(swap! label-to-data assoc label data))
(doseq [handler (label @label-to-handlers)]
(handler @label-to-data)))
(defn reset-globals!
[]
(reset! label-to-->data {})
(reset! label-to-data {})
(reset! label-to-handlers {}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment