On 26.11.2009, at 04:51, Rich Hickey wrote:
> Direct linking (to clojure*) is implemented and up already in the new
> branch compiler. I encourage people to play with it and share their
> experiences. I'd welcome also an attempt at a macro version so we can
> compare ease of use, configuration and performance.
Have a look at clojure.contrib.macros/with-direct-linking. Some usage
examples:
(with-direct-linking
(defn foo [x] (map count x))
(defn bar [x] (zero? x)))
This basic variant creates a let form with all the public symbols from
clojure.core whose current values are functions but not macros. Other
symbols and/or other namespaces can be given as an optional first
argument that must be a vector of symbols:
(with-direct-linking [clojure.core/map clojure.core/zero?]
(defn foo [x] (map count x))
(defn bar [x] (zero? x)))
This does direct linking for map and zero? but not for count.
(with-direct-linking [*ns*]
(defn foo [x] (map count x))
(defn bar [x] (zero? x)))
This does direct linking for all function-valued vars that already
exist in the current namespace. Watch out when using this
interactively, as the effect of redefining a var can be surprising.
(with-direct-linking [clojure.core clojure.zip clojure.contrib.seq-
utils]
(defn foo [x] (map count x))
(defn bar [x] (zero? x)))
A few observations from first experiments with this macro:
+ It acts on all usages of functions, not only invocations.
- One probably frequent usage pattern is a bit cumbersome: direct
linking for functions defined previously in the same namespace. This
requires wrapping each individual function in with-direct-linking
[*ns*] ...) to make sure that the previous definition is taken into
account.
+ It works even with Clojure 1.0.
Konrad.