Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
groupBy for CL
(defun group-by (pred list)
(loop
while list
for cur = (pop list)
collect
(nreverse
(loop with acc = (list cur)
while list
for x = (pop list)
if (funcall pred cur x)
do (push x acc)
else do (progn (push x list)
(return acc))
finally (return acc)))))
(group-by #'= '(1 1 2 3 1 1 4))
;;=> ((1 1) (2) (3) (1 1) (4))
(group-by (lambda (x y)
(equal (mod y 3) (mod x 3)))
'(1 1 2 3 1 1 4))
;;=> ((1 1) (2) (3) (1 1 4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.