Created
May 26, 2013 22:29
-
-
Save rubysolo/5654257 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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