Skip to content

Instantly share code, notes, and snippets.

@jamiltron
Created July 15, 2011 00:23
Show Gist options
  • Save jamiltron/1083793 to your computer and use it in GitHub Desktop.
Save jamiltron/1083793 to your computer and use it in GitHub Desktop.
Compress a Sequence
;; Compress a sequence: Write a function which removes consecutive duplicates from a sequence.
;; old way
(defn comp-seq-old [n]
(cond
(empty? (rest n)) n
(= (first n) (first (rest n))) (comp-seq-old (cons (first n) (rest (rest n))))
:else (cons (first n) (comp-seq-old (rest n)))))
;; new way
(defn comp-seq-new [n]
(map first (partition-by identity n)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment