Back
intersection (clj)
(source)function
(intersection s1)
(intersection s1 s2)
(intersection s1 s2 & sets)
Return a time-ordered sequence of disjoint intervals where two or
more intervals of the given sequences are concurrent. Arguments must
be time-ordered sequences of disjoint intervals.
Examples
tick
(ns tick.alpha.interval-test
(:require
[clojure.spec.alpha :as s]
[tick.core :as t]
[tick.protocols :as p]
[clojure.test
:refer [deftest is testing run-tests]
:refer-macros [deftest is testing run-tests]]
[tick.alpha.interval :as ti]
#?@(:cljs [[java.time :refer [Instant LocalDateTime LocalTime]]]))
#?(:clj
(:import [java.time LocalDateTime Instant LocalTime])))
(deftest intersection-test
(let [coll1 [(ti/new-interval (t/instant "2017-01-01T06:00:00Z")
(t/instant "2017-01-01T07:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T14:00:00Z")
(t/instant "2017-01-01T18:00:00Z"))]]
(is
(= [(ti/new-interval (t/instant "2017-01-01T09:00:00Z") (t/instant "2017-01-01T10:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T11:00:00Z") (t/instant "2017-01-01T12:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T14:00:00Z") (t/instant "2017-01-01T15:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T17:00:00Z") (t/instant "2017-01-01T18:00:00Z"))]
(ti/intersection coll1 coll2))))
(is
(= [(ti/new-interval (t/instant "2017-01-01T09:00:00Z") (t/instant "2017-01-01T11:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T14:00:00Z") (t/instant "2017-01-01T16:00:00Z"))]
(ti/intersection coll1 coll2))))
(let [coll1 [(ti/new-interval (t/instant "2017-01-01T08:00:00Z")
(t/instant "2017-01-01T12:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T14:00:00Z")
(t/instant "2017-01-01T16:00:00Z"))]
coll2 [(ti/new-interval (t/instant "2017-01-01T08:00:00Z")
(t/instant "2017-01-01T12:00:00Z"))]]
(is
(=
[(ti/new-interval (t/instant "2017-01-01T08:00:00Z")
(t/instant "2017-01-01T12:00:00Z"))]
(ti/intersection coll1 coll2))))
(is (=
[(ti/new-interval (t/instant "2017-01-01T08:00:00Z")
(t/instant "2017-01-01T12:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T17:00:00Z")
(t/instant "2017-01-01T18:00:00Z"))]
(ti/intersection coll1 coll2))))
(let [coll1 [(ti/new-interval (t/instant "2017-01-01T12:00:00Z")
(t/instant "2017-01-01T14:00:00Z"))]
coll2 [(ti/new-interval (t/instant "2017-01-01T11:00:00Z")
(t/instant "2017-01-01T14:00:00Z"))]]
(is (= [(ti/new-interval (t/instant "2017-01-01T12:00:00Z")
(t/instant "2017-01-01T14:00:00Z"))]
(ti/intersection coll1 coll2))))
(let [coll1 [(ti/new-interval (t/date-time "2017-04-11T00:00")
(t/date-time "2017-04-14T00:00"))
(ti/new-interval (t/date-time "2017-04-18T00:00")
(t/date-time "2017-04-20T00:00"))
(ti/new-interval (t/date-time "2017-12-20T00:00")
(t/date-time "2017-12-23T00:00"))
(ti/new-interval (t/date-time "2017-12-27T00:00")
(t/date-time "2018-01-01T00:00"))
(ti/new-interval (t/date-time "2018-01-02T00:00")
(t/date-time "2018-01-08T00:00"))]
coll2 [(ti/bounds (t/year "2017"))]]
(is (= [(ti/new-interval (t/date-time "2017-04-11T00:00")
(t/date-time "2017-04-14T00:00"))
(ti/new-interval (t/date-time "2017-04-18T00:00")
(t/date-time "2017-04-20T00:00"))
(ti/new-interval (t/date-time "2017-12-20T00:00")
(t/date-time "2017-12-23T00:00"))
(ti/new-interval (t/date-time "2017-12-27T00:00")
(t/date-time "2018-01-01T00:00"))]
(ti/intersection coll1 coll2))))
(let [coll1 [(ti/new-interval (t/date-time "2017-04-11T00:00")
(t/date-time "2017-04-14T00:00"))
(ti/new-interval (t/date-time "2017-04-18T00:00")
(t/date-time "2017-04-20T00:00"))
(ti/new-interval (t/date-time "2017-12-20T00:00")
(t/date-time "2017-12-23T00:00"))
(ti/new-interval (t/date-time "2017-12-27T00:00")
(t/date-time "2018-01-01T00:00"))
(ti/new-interval (t/date-time "2018-01-02T00:00")
(t/date-time "2018-01-08T00:00"))]
coll2 [(ti/bounds (t/year "2017"))]]
(is (= [(ti/new-interval (t/date-time "2017-04-11T00:00")
(t/date-time "2017-04-14T00:00"))
(ti/new-interval (t/date-time "2017-04-18T00:00")
(t/date-time "2017-04-20T00:00"))
(ti/new-interval (t/date-time "2017-12-20T00:00")
(t/date-time "2017-12-23T00:00"))
(ti/new-interval (t/date-time "2017-12-27T00:00")
(t/date-time "2018-01-01T00:00"))]
(ti/intersection coll1 coll2)))
(ti/new-interval (t/instant "2017-01-01T14:00:00Z")
(t/instant "2017-01-01T18:00:00Z"))]]
(is
(empty? (ti/intersection coll1 coll2)))
(is
(empty? (ti/intersection coll2 coll1)))
(is
(empty? (ti/intersection [] [])))))))