Created
February 3, 2013 01:33
-
-
Save danlentz/4700128 to your computer and use it in GitHub Desktop.
A nice macro from cl-nlp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro define-lazy-singleton (name init &optional docstring) | |
"Define a function NAME, that will return a singleton object, | |
initialized lazily with INIT on first call. | |
Also define a symbol macro <NAME> that will expand to (NAME)." | |
(with-gensyms (singleton) | |
`(let (,singleton) | |
(defun ,name () | |
,docstring | |
(or ,singleton | |
(setf ,singleton ,init))) | |
(define-symbol-macro ,(mksym name :format "<~A>") (,name))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment