Skip to content

Instantly share code, notes, and snippets.

@EmmanuelOga
Created July 22, 2019 02:46
Show Gist options
  • Save EmmanuelOga/19afc4e7abd78923bfb761007329024b to your computer and use it in GitHub Desktop.
Save EmmanuelOga/19afc4e7abd78923bfb761007329024b to your computer and use it in GitHub Desktop.
Merge intervals
(defn ival [start finish] [start finish])
(defn mergeTwo [[a0 a1 :as a] [b0 b1 :as b]]
(if (< b0 a0)
(mergeTwo b a)
(if (>= a1 b0)
(list [a0 (max a1 b1)])
(list a b))))
(defn concatMerge [acc ival]
(if
(empty? acc) (list ival)
(concat (rest acc)
(mergeTwo (first acc) ival))))
(defn mergeIval [ivalList ival]
(if (empty? ivalList)
(list ival)
(let [sorted (sort-by first (conj ivalList ival))]
(reduce concatMerge (list) sorted))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment