view src/clojure/contrib/with_ns.clj @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
line wrap: on
line source
1 ;;; with_ns.clj -- temporary namespace macro
3 ;; by Stuart Sierra, http://stuartsierra.com/
4 ;; March 28, 2009
6 ;; Copyright (c) Stuart Sierra, 2009. All rights reserved. The use
7 ;; and distribution terms for this software are covered by the Eclipse
8 ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
9 ;; which can be found in the file epl-v10.html at the root of this
10 ;; distribution. By using this software in any fashion, you are
11 ;; agreeing to be bound by the terms of this license. You must not
12 ;; remove this notice, or any other, from this software.
15 (ns
16 ^{:author "Stuart Sierra",
17 :doc "Temporary namespace macro"}
18 clojure.contrib.with-ns)
20 (defmacro with-ns
21 "Evaluates body in another namespace. ns is either a namespace
22 object or a symbol. This makes it possible to define functions in
23 namespaces other than the current one."
24 [ns & body]
25 `(binding [*ns* (the-ns ~ns)]
26 ~@(map (fn [form] `(eval '~form)) body)))
28 (defmacro with-temp-ns
29 "Evaluates body in an anonymous namespace, which is then immediately
30 removed. The temporary namespace will 'refer' clojure.core."
31 [& body]
32 `(try
33 (create-ns 'sym#)
34 (let [result# (with-ns 'sym#
35 (clojure.core/refer-clojure)
36 ~@body)]
37 result#)
38 (finally (remove-ns 'sym#))))