Skip to content

Instantly share code, notes, and snippets.

@garlic0x1
Created November 22, 2023 05:08
Show Gist options
  • Save garlic0x1/039324859e8e05c0f0d7a9d26edbc4fd to your computer and use it in GitHub Desktop.
Save garlic0x1/039324859e8e05c0f0d7a9d26edbc4fd to your computer and use it in GitHub Desktop.
defmemo/int->int
; depends on alexandria
; memoizes int->int functions in a mutable vec
(defmacro defmemo/int->int (size name (in) &body body)
(let ((memo-name (read-from-string (format nil "~a-memo" name))))
`(progn (defvar ,memo-name (make-array (+ 1 ,size) :initial-element nil))
(defun ,name (,in)
(if-let ((memo (aref ,memo-name ,in)))
memo
(setf (aref ,memo-name ,in) (progn ,@body)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment