diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2024-10-30 15:15:04 +0000 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2024-10-30 22:33:27 +0000 |
commit | 07a5f754da457631dc22e612a01e5d2cb26137d3 (patch) | |
tree | 70799b47b03d832131e7c8653f61e4afa565c5f2 /2022/lib.lisp | |
parent | cd94c350e69a931f4dfc84ecfe34a333a91b3552 (diff) | |
download | advent-of-code-07a5f754da457631dc22e612a01e5d2cb26137d3.tar.gz advent-of-code-07a5f754da457631dc22e612a01e5d2cb26137d3.tar.bz2 advent-of-code-07a5f754da457631dc22e612a01e5d2cb26137d3.zip |
Update lib.lisp to be more imperative and include a threading macro
Instead of (f (g (h (j (k x))))) we can write
(-> (k x)
(j it)
(h it)
(g it)
(f it))
which is works really well for particularly large and complicated
expressions.
Diffstat (limited to '2022/lib.lisp')
-rw-r--r-- | 2022/lib.lisp | 40 |
1 files changed, 25 insertions, 15 deletions
diff --git a/2022/lib.lisp b/2022/lib.lisp index 6c55508..28efbae 100644 --- a/2022/lib.lisp +++ b/2022/lib.lisp @@ -10,16 +10,23 @@ "Splits LST by the first instance of DELIM" (let ((pos (position delim lst))) (if pos - (cons (subseq lst 0 pos) (list (subseq lst (+ pos 1)))) + (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) - (if (or (null lst) (not (cdr lst))) - (cons (list (car lst)) nil) - (if (member delim lst) - (destructuring-bind (first rest) (split-by-first lst delim) - (cons first (split-by-completely rest delim))) - (list lst)))) + (cond + ((or (null lst) (not (cdr lst))) + (list (car lst))) + ((not (member delim lst)) + (list lst)) + (t + (loop + for (start rest) = (split-by-first lst delim) + then (split-by-first rest delim) + collect start + if (not (member delim rest)) + collect rest + and do (loop-finish))))) (defun get-lines (input-string) (with-input-from-string (s input-string) @@ -27,12 +34,15 @@ while line collect line))) -(defun id (x) x) - (defun remove-nth (n lst) - (if (or (null lst) (= n 0)) - (cdr lst) - (cons (car lst) - (remove-nth - (- n 1) - (cdr lst))))) + (loop for el in lst + for i from 0 + if (not (= i n)) + collect el)) + +(defmacro --> (first &rest functions) + (let ((builder first)) + (loop for function in functions + do (setq builder `(let ((it ,builder)) + ,function))) + builder)) |