diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2024-10-30 22:33:31 +0000 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2024-10-30 22:33:31 +0000 |
commit | 65b65a2a3ef1c9cee21791b41fa9409586249ab6 (patch) | |
tree | 4c781e3aaa161e469f521fefaec2e70fffd8eda4 /2022/lib.lisp | |
parent | de4c5bbd5351bff29a2197233d2f0e2f3b1681e1 (diff) | |
download | advent-of-code-65b65a2a3ef1c9cee21791b41fa9409586249ab6.tar.gz advent-of-code-65b65a2a3ef1c9cee21791b41fa9409586249ab6.tar.bz2 advent-of-code-65b65a2a3ef1c9cee21791b41fa9409586249ab6.zip |
Introduce tests for split-by-first and split-by-completely
Allows one to split by string delimiter, or anything else.
Diffstat (limited to '2022/lib.lisp')
-rw-r--r-- | 2022/lib.lisp | 14 |
1 files changed, 7 insertions, 7 deletions
diff --git a/2022/lib.lisp b/2022/lib.lisp index 28efbae..d82bc9c 100644 --- a/2022/lib.lisp +++ b/2022/lib.lisp @@ -6,25 +6,25 @@ (string clist) (coerce clist 'string))) -(defun split-by-first (lst delim) +(defun split-by-first (lst delim &optional (test #'eq)) "Splits LST by the first instance of DELIM" - (let ((pos (position delim lst))) + (let ((pos (position delim lst :test test))) (if pos (list (subseq lst 0 pos) (subseq lst (+ pos 1))) (error (format nil "No instance of ~a was found in ~a" delim lst))))) -(defun split-by-completely (lst delim) +(defun split-by-completely (lst delim &optional (test #'eq)) (cond ((or (null lst) (not (cdr lst))) (list (car lst))) - ((not (member delim lst)) + ((not (member delim lst :test test)) (list lst)) (t (loop - for (start rest) = (split-by-first lst delim) - then (split-by-first rest delim) + for (start rest) = (split-by-first lst delim test) + then (split-by-first rest delim test) collect start - if (not (member delim rest)) + if (not (member delim rest :test test)) collect rest and do (loop-finish))))) |