Skip to content

Instantly share code, notes, and snippets.

@PetrGlad
Created January 26, 2016 08:18
Show Gist options
  • Save PetrGlad/15d7d0edd44370e051ea to your computer and use it in GitHub Desktop.
Save PetrGlad/15d7d0edd44370e051ea to your computer and use it in GitHub Desktop.
(ns lab.rle)
(->> (partition-by identity "abbasdfrressaaaa")
(mapcat (fn [r]
[(first r) (count r)]))
(apply str))
(->>
(concat "abbasdfrressaaaa" [nil]) ;; XXX To help writing last chunk
(reduce (fn [[result last-char run-count] ch]
(if (or (= last-char ch)
(nil? last-char))
[result ch (inc run-count)]
[(conj result last-char run-count) ch 1]))
[[] nil 0])
(first)
(apply str))
(loop [src "abbasdfrressaaaa"
result []
run-count 1]
(if (empty? src)
(apply str result)
(let [[ch next-ch] src
tail (rest src)]
(if (= ch next-ch)
(recur tail result (inc run-count))
(recur tail (conj result ch run-count) 1)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment