Back
disjoin (clj)
(source)function
(disjoin s1)
(disjoin s1 s2)
(disjoin s1 s2 & sets)
Split s1 across the grating defined by s2
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 disjoint-test []
(is (ti/disjoint?
(ti/new-interval (instants 0) (instants 1))
(ti/new-interval (instants 2) (instants 3))))
(is (= (ti/disjoint?
(ti/new-interval (instants 0) (instants 1))
(ti/new-interval (instants 2) (instants 3))) ti/precedes?))
(is (nil?
(ti/disjoint?
(ti/new-interval (instants 0) (instants 2))
(ti/new-interval (instants 1) (instants 3)))))
(is (nil?
(ti/disjoint?
(ti/new-interval (instants 0) (instants 3))
(ti/new-interval (instants 1) (instants 2))))))
;; concur is really the complement to disjoint, but we'll test it
;; anywhere to ensure the complement function is working as expected.
(deftest ordered-disjoint-intervals?-test
(is
(ti/ordered-disjoint-intervals? []))
(is
(ti/ordered-disjoint-intervals?
[(ti/new-interval (t/instant "2017-07-30T09:00:00Z")
(t/instant "2017-07-30T10:00:00Z"))]))
(is
(ti/ordered-disjoint-intervals?
[(ti/new-interval (t/instant "2017-07-30T09:00:00Z")
(t/instant "2017-07-30T10:00:00Z"))
(ti/new-interval (t/instant "2017-07-30T11:00:00Z")
(t/instant "2017-07-30T13:00:00Z"))]))
(is
(ti/ordered-disjoint-intervals?
[(ti/new-interval (t/instant "2017-07-30T09:00:00Z")
(t/instant "2017-07-30T11:00:00Z"))
(ti/new-interval (t/instant "2017-07-30T11:00:00Z")
(t/instant "2017-07-30T13:00:00Z"))]))
(is
(ti/ordered-disjoint-intervals?
[(ti/new-interval (t/instant "2017-07-30T09:00:00Z")
(t/instant "2017-07-30T11:00:00Z"))
(ti/new-interval (t/instant "2017-07-30T11:00:00Z")
(t/instant "2017-07-30T13:00:00Z"))
(ti/new-interval (t/instant "2017-07-30T16:00:00Z")
(t/instant "2017-07-30T18:00:00Z"))]))
(is
(false?
(ti/ordered-disjoint-intervals?
[(ti/new-interval (t/instant "2017-07-30T09:00:00Z")
(t/instant "2017-07-30T12:00:00Z"))
(ti/new-interval (t/instant "2017-07-30T11:00:00Z")
(t/instant "2017-07-30T13:00:00Z"))])))
(is
(false?
(ti/ordered-disjoint-intervals?
[(ti/new-interval (t/instant "2017-07-30T11:00:00Z")
(t/instant "2017-07-30T13:00:00Z"))
(ti/new-interval (t/instant "2017-07-30T09:00:00Z")
(t/instant "2017-07-30T10:00:00Z"))]))))
(deftest union-test
(testing "counts"
(let [coll1 [(ti/new-interval (t/instant "2017-07-30T09:00:00Z")
(t/instant "2017-07-30T12:00:00Z"))]
coll2 [(ti/new-interval (t/instant "2017-07-30T11:00:00Z")
(t/instant "2017-07-30T15:00:00Z"))]
coll3 [(ti/new-interval (t/instant "2017-07-30T17:00:00Z")
(t/instant "2017-07-30T19:00:00Z"))]]
(is (= 1 (count (ti/union coll1 coll2))))
(is (ti/ordered-disjoint-intervals? (ti/union coll1 coll2)))
(is (= 2 (count (ti/union coll1 coll2 coll3))))
(is (ti/ordered-disjoint-intervals? (ti/union coll1 coll2 coll3)))))
;; We are reclaiming 'disjoin' to mean to 'end the joining of' or 'to become disjoint'.
(deftest complement-test
(testing "complement through max of type"
(is (= [(ti/new-interval (t/time "01:00") (t/max-of-type (t/time "00:00")))]
(ti/complement [(ti/new-interval (t/time "00:00") (t/time "01:00"))]))))
(testing "complement ordered disjoint intervals"
(is (= [(ti/new-interval (t/time "00:00") (t/time "01:00"))
(ti/new-interval (t/time "02:00") (t/time "03:00"))
(ti/new-interval (t/time "04:00") (t/max-of-type (t/time "00:00")))]
(ti/complement [(ti/new-interval (t/time "01:00") (t/time "02:00"))
(ti/new-interval (t/time "03:00") (t/time "04:00"))]))))
(testing "complement meeting intervals"
(is (= [(ti/new-interval (t/time "00:00") (t/time "01:00"))
(ti/new-interval (t/time "03:00") (t/max-of-type (t/time "00:00")))]
(ti/complement [(ti/new-interval (t/time "01:00") (t/time "02:00"))
(ti/new-interval (t/time "02:00") (t/time "03:00"))]))))
(testing "complement empty interval round trip"
(is (= [] (ti/complement (ti/complement []))))))