Created
January 26, 2016 08:18
-
-
Save PetrGlad/15d7d0edd44370e051ea to your computer and use it in GitHub Desktop.
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 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