-
-
Save martinklepsch/bcabc82bd2da32d66a89c1235ff914cf to your computer and use it in GitHub Desktop.
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
(ns date.partition | |
(:import (java.time Instant Duration))) | |
(defprotocol IInterval | |
(within? [this date] "xxx")) | |
(defrecord Interval [start end] | |
IInterval | |
(within? [this date] | |
(< (.toEpochMilli (:start this)) | |
(.toEpochMilli (.toInstant date)) | |
(.toEpochMilli (:end this))))) | |
(defn interval-seq | |
[start interval-size] | |
(let [i (->Interval start (.plus start interval-size))] | |
(cons i (lazy-seq (interval-seq (:end i) interval-size))))) | |
;; interval-seq usage: | |
;; (interval-seq (.toInstant some-date) interval) | |
(def dates | |
[(java.util.Date. 2016 8 1) | |
(java.util.Date. 2016 9 1) | |
(java.util.Date. 2016 10 1) | |
(java.util.Date. 2016 11 1) | |
(java.util.Date. 2016 12 1) | |
(java.util.Date. 2017 1 1) | |
(java.util.Date. 2017 2 1)]) | |
(defn partition-dates [dates interval-size]) | |
;; (partition-dates dates (Duration/ofMonths 2)) | |
;; => | |
;; [[(java.util.Date. 2016 8 1) | |
;; (java.util.Date. 2016 9 1) | |
;; (java.util.Date. 2016 10 1)] | |
;; [(java.util.Date. 2016 11 1) | |
;; (java.util.Date. 2016 12 1)] | |
;; [(java.util.Date. 2017 1 1) | |
;; (java.util.Date. 2017 2 1)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Potential solution