Skip to content

Instantly share code, notes, and snippets.

@v-kolesnikov
Last active September 8, 2015 13:15
Show Gist options
  • Save v-kolesnikov/aa0caa586dffbb2d4891 to your computer and use it in GitHub Desktop.
Save v-kolesnikov/aa0caa586dffbb2d4891 to your computer and use it in GitHub Desktop.
; remove duplicates from sequence
; third
(defn compress [seq]
(->> seq
(reduce #(cond
(= %2 (first %1)) %1
:else (conj %1 %2))
nil)
(reverse)))
; vs second
(defn compress [seq]
(loop [head [] tail seq]
(cond
(nil? tail) nil
(nil? (second tail)) (concat head tail)
(= (first tail)
(first (rest tail))) (recur head (rest tail))
:else (recur (conj head
(first tail))
(rest tail)))))
; vs first
(defn compress_ [seq]
(cond
(nil? seq) nil
(nil? (second seq)) seq
(= (first seq)
(first (rest seq))) (compress_ (rest seq))
:else (cons (first seq)
(compress_ (rest seq)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment