Skip to content

Instantly share code, notes, and snippets.

@gtrak
Created March 12, 2013 00:14
Show Gist options
  • Save gtrak/5139166 to your computer and use it in GitHub Desktop.
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.
(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