-
-
Save anonymous/7765e88806e0c3fae6aa to your computer and use it in GitHub Desktop.
This file contains 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 compact-to-ranges [xs] | |
(reverse | |
(reduce (fn [rs x] | |
(if (empty? rs) | |
(list [x x]) | |
(let [[l u] (first rs)] | |
(if (= (inc u) x) | |
(cons [l x] (rest rs)) | |
(cons [x x] rs))))) | |
(list) | |
xs))) | |
(assert (= (compact-to-ranges [1 2 3 4 6 7 9 10 11 15 16])) ([1 4] [6 7] [9 11] [15 16])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment