Skip to content

Instantly share code, notes, and snippets.

Created December 19, 2014 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/7765e88806e0c3fae6aa to your computer and use it in GitHub Desktop.
Save anonymous/7765e88806e0c3fae6aa to your computer and use it in GitHub Desktop.
(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