Skip to content

Instantly share code, notes, and snippets.

@ejackson
Created July 7, 2010 14:48
Show Gist options
  • Save ejackson/466790 to your computer and use it in GitHub Desktop.
Save ejackson/466790 to your computer and use it in GitHub Desktop.
(defn create-ts [dataset cons-method]
(reify
Object
(equals [this o]
(or (identical? this o)
(and (.isInstance name o)
(= dataset (. o dataset)))))
(hashCode [this]
(hash dataset))
(toString [this]
(str "(create-ts " (apply pr-str dataset)")"))
clojure.lang.IPersistentCollection
(cons [this x]
(create-ts (cons-method dataset x) cons-method))
(empty [this]
(create-ts (-> dataset empty) cons-method))
;; How does this differ from equals
(equiv [this o]
(.equals this o))
(seq [this]
(seq dataset))
;;--------------
clojure.lang.IPersistentStack
(peek [this]
(-> dataset rseq first))
(pop [this]
(create-ts (disj dataset (peek this)) cons-method))
;;--------------
clojure.lang.Sorted
(comparator [this]
(.comparator dataset))
(entryKey [this entry]
(.entryKey dataset entry))
(seq [this ascending]
(.seq dataset ascending))
(seqFrom [this key ascending]
(.seqFrom dataset key ascending))
;;--------------
Trim
(trim-older [this snip]
(create-ts (reduce disj dataset (subseq this < snip)) cons-method))
(trim-newer [this snip]
(create-ts (reduce disj dataset (rsubseq this > snip)) cons-method))
))
;;---------------------------------------------------------------------------
;; Extra API
;;---------------------------------------------------------------------------
;; Straight Timeseries
(defn ts [& xs]
(create-ts (apply sorted-set xs) conj))
;; No longer work, obviously.
(defmethod print-method Timeseries [o, ^Writer w]
(#'clojure.core/print-sequential "(ts " #'clojure.core/pr-on " " ")" o w))
(defmethod print-dup Timeseries [o, ^Writer w]
(print-method o w))
;;---------------------------------------------------------------------------
;; Circular Timeseries
(defn cts [& xs]
(create-ts (apply sorted-set xs) (partial circular-conj circular-series-length))
(defmethod print-method CircularTimeseries [o, ^Writer w]
(#'clojure.core/print-sequential "(cts " #'clojure.core/pr-on " " ")" o w))
(defmethod print-dup CircularTimeseries [o, ^Writer w]
(print-method o w))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment