aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKonrad Hinsen <konrad.hinsen@fastmail.net>2009-11-26 14:42:45 +0100
committerKonrad Hinsen <konrad.hinsen@fastmail.net>2009-11-26 14:42:45 +0100
commit59b6a660707837af10b69cc67fe9033ba528d22f (patch)
treedc594bb96c421a56f2e0caf47a0face3abc7a27d /src
parentec3a9d1fc37db63245ec6a3c672725add7633bd2 (diff)
macros: new macro with-direct-linking
Diffstat (limited to 'src')
-rw-r--r--src/clojure/contrib/macros.clj48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/clojure/contrib/macros.clj b/src/clojure/contrib/macros.clj
index c9e96a65..ffb67183 100644
--- a/src/clojure/contrib/macros.clj
+++ b/src/clojure/contrib/macros.clj
@@ -34,3 +34,51 @@
(let [makefn (fn [[name args body]] (list name (list 'fn name args body)))
fns (vec (apply concat (map makefn (partition 3 fn-bindings))))]
`(let ~fns ~@exprs)))
+
+ ;; By Konrad Hinsen
+
+ (defn- unqualified-symbol
+ [s]
+ (let [s-str (str s)]
+ (symbol (subs s-str (inc (.indexOf s-str (int \/)))))))
+
+(defn- bound-var?
+ [var]
+ (try
+ (do (deref var) true)
+ (catch java.lang.IllegalStateException e false)))
+
+(defn- fns-from-ns
+ [ns ns-symbol]
+ (apply concat
+ (for [[k v] (ns-publics ns)
+ :when (and (bound-var? v)
+ (fn? @v)
+ (not (:macro (meta v))))]
+ [k (symbol (str ns-symbol) (str k))])))
+
+(defn- expand-symbol
+ [ns-or-var-sym]
+ (if (= ns-or-var-sym '*ns*)
+ (fns-from-ns *ns* (ns-name *ns*))
+ (if-let [ns (find-ns ns-or-var-sym)]
+ (fns-from-ns ns ns-or-var-sym)
+ (list (unqualified-symbol ns-or-var-sym) ns-or-var-sym))))
+
+(defmacro with-direct-linking
+ "EXPERIMENTAL!
+ Compiles the functions in body with direct links to the functions
+ named in symbols, i.e. without a var lookup for each invocation.
+ Symbols is a vector of symbols that name either vars or namespaces.
+ A namespace reference is replaced by the list of all symbols in the
+ namespace that are bound to functions. If symbols is not provided,
+ the default value ['clojure.core] is used. The symbol *ns* can be
+ used to refer to the current namespace."
+ {:arglists '([symbols? & body])}
+ [& body]
+ (let [[symbols body] (if (vector? (first body))
+ [(first body) (rest body)]
+ [['clojure.core] body])
+ bindings (vec (mapcat expand-symbol symbols))]
+ `(let ~bindings ~@body)))
+ \ No newline at end of file