Last active
August 14, 2023 13:31
-
-
Save johnwalker/8e7e6a8bcbeff03d4e80 to your computer and use it in GitHub Desktop.
solution accompanying https://groups.google.com/d/msg/clojure/Ksz-dGFnqVE/drHzicm7JxAJ
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
(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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! :)