(in-package :cl-user)

(defpackage ca.mhcat.advent2022
 (:use :cl))
(in-package :ca.mhcat.advent2022)

(defun load-lines (fname)
 (let ((sysroot (asdf:system-source-directory :ca.mhcat.advent2022))
       (frelpath (merge-pathnames (pathname "data/") (pathname fname))))
   (with-open-file (stream (merge-pathnames frelpath sysroot))
     (loop for line = (read-line stream nil)
           while line collect line))))

(defun compose (&rest fns)
 "Paul Graham, with some refinement from stackoverflow:

https://stackoverflow.com/questions/5928106/compose-example-in-paul-grahams-ansi-common-lisp"
 (lambda (arg)
   (reduce 'funcall fns
           :from-end t
           :initial-value arg)))

(defun juxtapose (&rest fns)
 (lambda (arg)
   (reduce (lambda (lst fn)
             (cons (funcall fn arg) lst))
           (reverse fns)
           :initial-value (list))))