We all know about memoize, but let's say I want to define a global hash-map, where the keys are actual pieces of code and the value the result that would be evaluated when executed. Something like this:
| Key | Value |
|----------------------------+--------------------------------|
| "hello" | "hello" |
| (square 5) | 25 |
| (factorial 5) | 120 |
| (git-tags "~/foo") | (list "1.0.0" "0.1.0" "0.0.1") |
| (make-instance 'singleton) | #<SINGLETON {1301DE5A03}> |
Which can then be used like this in a trivial way, just passing code because code is data: (let ((path "~/foo")
(tags (cache `(git-tags ,path))))
(format t "Tags of ~a~% ~{- ~a~%~}"
path tags))
Sure, something like this is possible in other languages, but having done macros in languages such as Rust and Nim, it involves such a verbose and syntax soupy way of dealing with the AST that I don't feel like reaching for those abstractions that often. I'd rather just write boring code, and most consider this a feature.