Skip to content

Instantly share code, notes, and snippets.

@johnwalker
Last active August 14, 2023 13:31

Revisions

  1. johnwalker revised this gist Nov 7, 2014. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion congeal-consecutives.clj
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,6 @@

    (defn rangify [coll]
    (mapv (fn [r]
    (assert (vector? r))
    (let [f (first r)
    top (peek r)]
    (if (= f top)
  2. johnwalker revised this gist Nov 7, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion congeal-consecutives.clj
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@

    (defn rangify [coll]
    (mapv (fn [r]
    (assert (vector? coll))
    (assert (vector? r))
    (let [f (first r)
    top (peek r)]
    (if (= f top)
  3. johnwalker created this gist Nov 7, 2014.
    23 changes: 23 additions & 0 deletions congeal-consecutives.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    (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]
    (assert (vector? coll))
    (let [f (first r)
    top (peek r)]
    (if (= f top)
    (str f)
    (str f "-" top)))) coll))

    (-> v
    congeal-consecutives
    rangify)