Skip to content

Instantly share code, notes, and snippets.

@danlentz
Created February 3, 2013 01:33
Show Gist options
  • Save danlentz/4700128 to your computer and use it in GitHub Desktop.
Save danlentz/4700128 to your computer and use it in GitHub Desktop.
A nice macro from cl-nlp
(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