Created
July 22, 2019 02:46
-
-
Save EmmanuelOga/19afc4e7abd78923bfb761007329024b to your computer and use it in GitHub Desktop.
Merge intervals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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