Created
July 28, 2017 12:45
-
-
Save phoe/0ae2bdf01366431512326a2ee49b36c1 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
;; thanks, beach! | |
(defun split-list-if (predicate list) | |
"Destructively splits the provided list into two lists: one contains the | |
elements of the original list that satisfy the predicate, the other contains | |
elements that do not satisfy the predicate. The order of the original elements | |
is preserved." | |
(loop with result1 = '() | |
with result2 = '() | |
with rest = list | |
until (null rest) | |
do (let ((temp rest)) | |
(pop rest) | |
(if (funcall predicate (car temp)) | |
(setf (cdr temp) result1 | |
result1 temp) | |
(setf (cdr temp) result2 | |
result2 temp))) | |
finally (return (values (nreverse result1) | |
(nreverse result2))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment