Skip to content

Instantly share code, notes, and snippets.

@g000001
Created September 28, 2008 06:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save g000001/13415 to your computer and use it in GitHub Desktop.
Save g000001/13415 to your computer and use it in GitHub Desktop.
(defn
#^{:doc "P11 (*) Modified run-length encoding."
:test (do (test= (encode-modified '(a a a a b c c a a d e e e e))
'((4 a) b (2 c) (2 a) d (4 e)))
(test= (encode-modified []) [])
(test= (encode-modified [1]) [1]))}
; ---------------
encode-modified
; ---------------
([coll]
(if (empty? coll)
[]
(map #(if (single? %)
(first %)
(list (count %) (first %)))
(pack coll)))))
(defn single? [coll]
(nil? (rest coll)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment