Mercurial > lasercutter
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 macro3 ;; by Stuart Sierra, http://stuartsierra.com/4 ;; March 28, 20096 ;; Copyright (c) Stuart Sierra, 2009. All rights reserved. The use7 ;; and distribution terms for this software are covered by the Eclipse8 ;; 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 this10 ;; distribution. By using this software in any fashion, you are11 ;; agreeing to be bound by the terms of this license. You must not12 ;; remove this notice, or any other, from this software.15 (ns16 ^{:author "Stuart Sierra",17 :doc "Temporary namespace macro"}18 clojure.contrib.with-ns)20 (defmacro with-ns21 "Evaluates body in another namespace. ns is either a namespace22 object or a symbol. This makes it possible to define functions in23 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-ns29 "Evaluates body in an anonymous namespace, which is then immediately30 removed. The temporary namespace will 'refer' clojure.core."31 [& body]32 `(try33 (create-ns 'sym#)34 (let [result# (with-ns 'sym#35 (clojure.core/refer-clojure)36 ~@body)]37 result#)38 (finally (remove-ns 'sym#))))