Skip to content

Instantly share code, notes, and snippets.

@amalloy
Created October 23, 2012 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amalloy/3939975 to your computer and use it in GitHub Desktop.
Save amalloy/3939975 to your computer and use it in GitHub Desktop.
(defmacro using "ns can be either a symbol designating a namespace, or
a list like (ns :only [a b c])."
[ns & body]
(let [[ns vars] (if (sequential? ns)
[(first ns) (nth ns 2)] ;; skip the :only
[ns (keys (ns-publics (find-ns ns)))])]
`(symbol-macrolet [~@(for [var vars,
clause [var `(quote ~(symbol (name ns) (name var)))]]
clause)]
~@body)))
user> (using [clojure.set :only [rename-keys]]
(rename-keys {:a 1} {:a :b}))
{:a :b}
user> (using clojure.set
(rename-keys {:a 1} {:a :b}))
{:a :b}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment