Back
unite (clj)
(source)function
(unite intervals)
Unite concurrent intervals. Intervals must be ordered by beginning
but not necessarily disjoint (the purpose of this function is to
splice together intervals that are concurrent resulting in a
time-ordered sequence of disjoint intervals that is returned.
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 unite-test
(testing "Unite meeting intervals"
(is
(=
[(ti/new-interval (t/date "2017-06-15") (t/date "2017-06-25"))
(ti/new-interval (t/date "2017-06-26") (t/date "2017-06-30"))]
(let [intervals
[(ti/new-interval (t/date "2017-06-15") (t/date "2017-06-25"))
(ti/new-interval (t/date "2017-06-26") (t/date "2017-06-30"))]]
(ti/unite intervals)))))
(testing "Unite overlapping intervals"
(is
(=
[(ti/new-interval (t/date "2017-06-10") (t/date "2017-06-25"))
(ti/new-interval (t/date "2017-07-01") (t/date "2017-07-10"))]
(map ti/bounds
(ti/unite [(ti/new-interval (t/date "2017-06-10") (t/date "2017-06-20"))
(ti/new-interval (t/date "2017-06-15") (t/date "2017-06-25"))
(ti/new-interval (t/date "2017-07-01") (t/date "2017-07-10"))])))))
(testing "Unite containing intervals"
(is
(=
[(ti/new-interval (t/date "2017-06-15") (t/date "2017-06-25"))]
(map ti/bounds
(ti/unite [(ti/new-interval (t/date "2017-06-15") (t/date "2017-06-25"))
(ti/new-interval (t/date "2017-06-17") (t/date "2017-06-20"))])))))
(testing "Unite finished-by intervals"
(is
(=
[(ti/new-interval (t/date "2017-06-15") (t/date "2017-06-25"))]
(map ti/bounds
(ti/unite [(ti/new-interval (t/date "2017-06-15") (t/date "2017-06-25"))
(ti/new-interval (t/date "2017-06-17") (t/date "2017-06-20"))
(ti/new-interval (t/date "2017-06-18") (t/date "2017-06-25"))]))))))
(deftest flatten-test
(let [ivals [(ti/new-interval (t/date "2017-06-15") (t/date "2017-06-25"))
(ti/new-interval (t/date "2017-06-17") (t/date "2017-06-20"))
(ti/new-interval (t/date "2017-06-18") (t/date "2017-06-25"))
(ti/new-interval (t/date "2017-07-18") (t/date "2017-07-25"))
(ti/new-interval (t/date "2017-08-18") (t/date "2017-08-25"))]]
(is (= ivals (ti/flatten (ti/unite ivals))))))