Skip to content

Instantly share code, notes, and snippets.

@jcromartie jcromartie/history.clj Secret
Created Jan 19, 2011

What would you like to do?
Data structure to track changes over time
(defn get-time
(.getTime (java.util.Date.)))
;; history-seq
(defn action
"Attach keys and values to history stream"
[hist & keyvals]
(let [m (apply hash-map keyvals)
base {:time (get-time)}]
(conj hist (merge base m))))
(defn historic
"Create a new history stream"
[& keyvals]
(apply action '() keyvals))
(defn history
"Get built-up states of history stream over time"
(reduce (fn
[states update]
(conj states (merge (last states) update)))
(sort-by :time hist)))
(defn current
"Get current state of history stream"
(last (history hist)))
(defn at
"Get state of history stream at specific point in time"
[hist time]
(last (filter #(<= (or (:time %) 0) time) (history hist))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.