[lib] add general purpose caching

master
Jacopo De Simoi 3 months ago
parent afe6f6d253
commit 27a447f43a
  1. 11
      lib/lib-advent.org
  2. 11
      p7/p7.org

@ -77,3 +77,14 @@
(defmacro --2map-indexed (form li)
`(-2map-indexed (lambda (it-index-1 it-index-2 it) ,form) ,li))
#+end_src
* general purpose caching
Add this function as an ~:around~ advice to any function to have its
results cached in ~cache~. The symbol ~cache~ should be already bound
#+begin_src emacs-lisp
(defun cache-result (fun &rest r)
(let ((c (assoc (cons fun r) cache)))
(if c (cdr c)
(let ((res (apply fun r)))
(push (cons (cons fun r) res) cache)
res ))))
#+end_src

@ -74,12 +74,13 @@ Of course it will blow in my face if I did not cache the results
#+begin_src emacs-lisp
(setq max-lisp-eval-depth 10000000) ; burn baby burn
(setq cache nil)
(defun cache-result (fun pos)
(let ((c (assoc pos cache)))
(defun cache-result (fun &rest r)
(let ((c (assoc (cons fun r) cache)))
(if c (cdr c)
(let ((res (funcall fun pos)))
(push (cons pos res) cache)
(let ((res (apply fun r)))
(push (cons (cons fun r) res) cache)
res ))))
(advice-add 'timelines-at :around 'cache-result)
(defun timelines-at (pos)
@ -95,3 +96,5 @@ Of course it will blow in my face if I did not cache the results
#+RESULTS:
: 1537373473728

Loading…
Cancel
Save