Created
March 12, 2013 00:14
-
-
Save gtrak/5139166 to your computer and use it in GitHub Desktop.
Deferred loading. Remove :use and :requires from the ns declaration, as that will load the referred classes recursively in the static initializer of your namespace. Total time spent loading classes doesn't change, but you can amortize it along the perceptions of the user in chunks.
This file contains 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 deferred | |
"Loads and runs a function dynamically to defer loading the namespace. | |
Usage: \"(deferred clojure.core/+ 1 2 3)\" returns 6. There's no issue | |
calling require multiple times on an ns." | |
[fully-qualified-func & args] | |
(let [func (symbol (name fully-qualified-func)) | |
space (symbol (namespace fully-qualified-func))] | |
`(do (require '~space) | |
(let [v# (ns-resolve '~space '~func)] | |
(v# ~@args))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment