Last active
March 11, 2019 19:10
-
-
Save SneakyPeet/9b0f063b52641fe4bf71eda35f68209b to your computer and use it in GitHub Desktop.
run length encode decode clojure puzzle from purelyfunctional.tv newsletter
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
(defn rle | |
([coll] | |
(rle [] coll)) | |
([result coll] | |
(if (empty? coll) | |
coll | |
(lazy-seq | |
(let [current (first coll) | |
n (count (take-while #(= % current) coll))] | |
(cons [n current] (rle result (drop n coll)))))))) | |
(defn rld | |
([coll] (rld [] coll)) | |
([result coll] | |
(if (empty? coll) | |
coll | |
(lazy-seq | |
(let [[n x] (first coll) | |
remainder (if (= 1 n) | |
(rest coll) | |
(cons [(dec n) x] (rest coll)))] | |
(cons x (rld result remainder))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment