Files
advent-of-code/2022/lib.lisp
Aryadev Chavali 07a5f754da 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.
2024-10-30 22:33:27 +00:00

49 lines
1.3 KiB
Common Lisp

(defun string-to-clist (str)
(coerce str 'list))
(defun clist-to-string (clist)
(if (atom clist)
(string clist)
(coerce clist 'string)))
(defun split-by-first (lst delim)
"Splits LST by the first instance of DELIM"
(let ((pos (position delim lst)))
(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)
(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)
(loop for line = (read-line s nil)
while line
collect line)))
(defun remove-nth (n 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))