Skip to content

Instantly share code, notes, and snippets.

@jcromartie
Created January 19, 2011 15:45
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 jcromartie/202a9d047123fcb37f24 to your computer and use it in GitHub Desktop.
Save jcromartie/202a9d047123fcb37f24 to your computer and use it in GitHub Desktop.
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"
[hist]
(reduce (fn
[states update]
(conj states (merge (last states) update)))
[]
(sort-by :time hist)))
(defn current
"Get current state of history stream"
[hist]
(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