From 65b65a2a3ef1c9cee21791b41fa9409586249ab6 Mon Sep 17 00:00:00 2001
From: Aryadev Chavali <aryadev@aryadevchavali.com>
Date: Wed, 30 Oct 2024 22:33:31 +0000
Subject: Introduce tests for split-by-first and split-by-completely

Allows one to split by string delimiter, or anything else.
---
 2022/lib.lisp | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

(limited to '2022')

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)))))
 
-- 
cgit v1.2.3-13-gbd6f