Back
difference (clj)
(source)function
(difference s1)
(difference s1 s2)
(difference s1 s2 & sets)
Return an interval set that is the first set without elements of
the remaining sets.
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 difference-test
(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"))]
(is
(= [(ti/new-interval (t/instant "2017-01-01T08:00:00Z")
(t/instant "2017-01-01T09:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T11:00:00Z")
(t/instant "2017-01-01T12:00:00Z"))]
(ti/difference 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-01T14:00:00Z")
(t/instant "2017-01-01T16:00:00Z"))]
(ti/difference coll1 coll2))))
(is (=
[(ti/new-interval (t/instant "2017-01-01T18:00:00Z")
(t/instant "2017-01-01T19:00:00Z"))]
(ti/difference 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 (empty? (ti/difference coll1 coll2))))
(is (= [(ti/bounds (t/date "2017-07-31") (t/date "2017-08-13"))]
(ti/difference
[(ti/bounds (t/date "2017-07-31") (t/date "2017-08-13"))]
[(ti/bounds (t/date "2017-01-01"))])))
(ti/new-interval (t/instant "2017-01-01T14:00:00Z")
(t/instant "2017-01-01T18:00:00Z"))]]
(is
(= []
(ti/difference coll1 coll2)))
(is
(= coll2
(ti/difference coll2 coll1)))
(is
(= []
(ti/difference [] []))))))
(deftest difference-invariant-test
(let [coll1 [(ti/new-interval (t/instant "2017-01-01T14:00:00Z")
(t/instant "2017-01-01T16:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T08:00:00Z")
(t/instant "2017-01-01T12:00:00Z"))]
coll2 [(ti/new-interval (t/instant "2017-01-01T09:00:00Z")
(t/instant "2017-01-01T11:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T13:00:00Z")
(t/instant "2017-01-01T17:00:00Z"))]]
(is
(thrown?
#?(:clj clojure.lang.ExceptionInfo
:cljs ExceptionInfo)
(ti/difference coll1 coll2)))))