Skip to content

Instantly share code, notes, and snippets.

@johnwalker
Last active August 14, 2023 13:31
Show Gist options
  • Save johnwalker/8e7e6a8bcbeff03d4e80 to your computer and use it in GitHub Desktop.
Save johnwalker/8e7e6a8bcbeff03d4e80 to your computer and use it in GitHub Desktop.
(ns experimental-clojure.congeal-consecutives)
(def v [1 3 4 5 7 9 10 11 12])
(defn congeal-consecutives [coll]
(->> coll
(map-indexed (fn [i x] [(- x i) x]))
(partition-by first)
(mapv (fn [pairs]
(mapv second pairs)))))
(defn rangify [coll]
(mapv (fn [r]
(let [f (first r)
top (peek r)]
(if (= f top)
(str f)
(str f "-" top)))) coll))
(-> v
congeal-consecutives
rangify)
@firesofmay
Copy link

Beautiful Solution!

@johnwalker
Copy link
Author

Thanks! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment