-
-
Save jcromartie/202a9d047123fcb37f24 to your computer and use it in GitHub Desktop.
Data structure to track changes over time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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