Skip to content

Instantly share code, notes, and snippets.

@rubysolo
Created May 26, 2013 22:29
Show Gist options
  • Save rubysolo/5654257 to your computer and use it in GitHub Desktop.
Save rubysolo/5654257 to your computer and use it in GitHub Desktop.
(defn pack [coll]
"pack duplicates into sublists"
(loop [acc '(())
fst (first coll)
rst (rest coll)]
(let [current (first (first acc))
added-to-current (cons (cons fst (first acc)) (rest acc))
added-new-sublist (cons (list fst) acc)]
(if (seq rst)
(if (or (nil? current) (= current fst))
(recur added-to-current (first rst) (rest rst))
(recur added-new-sublist (first rst) (rest rst)))
(reverse (if (= current fst)
added-to-current
added-new-sublist))))))
(defn pack [coll]
(partition-by identity coll))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment