Skip to content

Instantly share code, notes, and snippets.

@SneakyPeet
Last active March 11, 2019 19:10
Show Gist options
  • Save SneakyPeet/9b0f063b52641fe4bf71eda35f68209b to your computer and use it in GitHub Desktop.
Save SneakyPeet/9b0f063b52641fe4bf71eda35f68209b to your computer and use it in GitHub Desktop.
run length encode decode clojure puzzle from purelyfunctional.tv newsletter
(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