Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support instant values satisfying the Inst protocol #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.9.0-alpha17"]
[clj-time "0.13.0"]
[clj-time "0.14.4"]
[prismatic/plumbing "0.5.4"]
[org.clojure/math.numeric-tower "0.0.4"]
[org.clojure/data.priority-map "0.0.7"]
Expand Down
9 changes: 4 additions & 5 deletions src/huri/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
map-from-vals map-from-keys]])
[net.cgrand.xforms :as x]
[clojure.data.priority-map :refer [priority-map-by]]
[clj-time.core :as t]
[clojure.core.reducers :as r]
[clojure.spec.alpha :as s]
[clojure.spec.test.alpha :as s.test])
(:import org.joda.time.DateTime))
[clojure.spec.test.alpha :as s.test]))

(defn papply
"partial that applies its arguments."
Expand Down Expand Up @@ -557,9 +555,10 @@ the arguments passed in are not nill. Else returns nil."
Optionally takes a keyfn to extract the values."
([xs]
(let [[x & xs] xs]
(r/fold (r/monoid (if (instance? org.joda.time.DateTime x)
(r/fold (r/monoid (if (inst? x)
(fn [[acc-min acc-max] x]
[(t/earliest acc-min x) (t/latest acc-max x)])
[(min-key inst-ms acc-min x)
(max-key inst-ms acc-max x)])
(fn [[acc-min acc-max] x]
[(min acc-min x) (max acc-max x)]))
(constantly [x x]))
Expand Down
25 changes: 21 additions & 4 deletions src/huri/plot.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
[clojure.walk :as walk]
[gorilla-renderable.core :as render]
[clojure.xml :as xml]
[clj-time.core :as t])
[clj-time.core :as t]
[clj-time.coerce :as tc])
(:import org.joda.time.DateTime
java.io.File
java.util.UUID))
Expand Down Expand Up @@ -147,6 +148,13 @@
(str "g" sanitized)
sanitized))))

(defmacro ^:private when-class
[sym & body]
(try
(Class/forName (name sym))
`(do ~@body)
(catch ClassNotFoundException _)))

(defmulti ->r-type class)

(defmethod ->r-type clojure.lang.Keyword
Expand Down Expand Up @@ -189,6 +197,15 @@
[x]
(str x))

(defmethod ->r-type java.util.Date
[x]
(->r-type (tc/from-date x)))

(when-class java.time.Instant
(defmethod ->r-type java.time.Instant
[x]
[:as.Date (str x)]))

(defmethod ->r-type org.joda.time.DateTime
[x]
[:as.Date (str x)])
Expand Down Expand Up @@ -238,14 +255,14 @@
[df]
(map-vals (comp #(cond
(number? %) :number
(instance? org.joda.time.DateTime %) :date
(inst? %) :date
:else :categorical)
first)
df))

(defn- date-scale-resolution
[dts]
(let [[start end] (extent dts)]
[dts]
(let [[start end] (map (comp tc/from-long inst-ms) (extent dts))]
(if (< (t/in-days (t/interval start end)) 90)
"%d-%b"
"%b-%y")))
Expand Down