Skip to content

Instantly share code, notes, and snippets.

@athos
Created December 5, 2010 07:31
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 athos/f3d4d7d572c9978e2b70 to your computer and use it in GitHub Desktop.
Save athos/f3d4d7d572c9978e2b70 to your computer and use it in GitHub Desktop.
(ns local-env
(:use [clojure.contrib.def :only (defvar-)])
(:use [clojure.contrib.macro-utils :only (mexpand-all symbol-macrolet)]))
(defvar- *local-envs* {})
(defmacro let-env [env-name & body]
(binding [*local-envs* (assoc *local-envs* env-name &env)]
`(let [~env-name ~(into {} (for [x (keys &env)] `['~x ~x]))]
~(mexpand-all `(do ~@body)))))
(defmacro with-env [env-name & body]
(let [env (*local-envs* env-name)]
(mexpand-all
`(symbol-macrolet ~(vec (mapcat (fn [name]
`[~name (~env-name '~name)])
(keys env)))
~@body))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment