;;; with_ns.clj -- temporary namespace macro ;; by Stuart Sierra, http://stuartsierra.com/ ;; March 28, 2009 ;; Copyright (c) Stuart Sierra, 2009. All rights reserved. The use ;; and distribution terms for this software are covered by the Eclipse ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) ;; which can be found in the file epl-v10.html at the root of this ;; distribution. By using this software in any fashion, you are ;; agreeing to be bound by the terms of this license. You must not ;; remove this notice, or any other, from this software. (ns #^{:author "Stuart Sierra", :doc "Temporary namespace macro"} clojure.contrib.with-ns) (defmacro with-ns "Evaluates body in another namespace. ns is either a namespace object or a symbol. This makes it possible to define functions in namespaces other than the current one." [ns & body] `(binding [*ns* (the-ns ~ns)] ~@(map (fn [form] `(eval '~form)) body))) (defmacro with-temp-ns "Evaluates body in an anonymous namespace, which is then immediately removed. The temporary namespace will 'refer' clojure.core." [& body] `(do (create-ns 'sym#) (let [result# (with-ns 'sym# (clojure.core/refer-clojure) ~@body)] (remove-ns 'sym#) result#)))