rlm@10
|
1 ;;; apply_macro.clj: make macros behave like functions
|
rlm@10
|
2
|
rlm@10
|
3 ;; by Stuart Sierra, http://stuartsierra.com/
|
rlm@10
|
4 ;; January 28, 2009
|
rlm@10
|
5
|
rlm@10
|
6 ;; Copyright (c) Stuart Sierra, 2009. All rights reserved. The use
|
rlm@10
|
7 ;; and distribution terms for this software are covered by the Eclipse
|
rlm@10
|
8 ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
|
rlm@10
|
9 ;; which can be found in the file epl-v10.html at the root of this
|
rlm@10
|
10 ;; distribution. By using this software in any fashion, you are
|
rlm@10
|
11 ;; agreeing to be bound by the terms of this license. You must not
|
rlm@10
|
12 ;; remove this notice, or any other, from this software.
|
rlm@10
|
13
|
rlm@10
|
14
|
rlm@10
|
15 ;; Don't use this. I mean it. It's evil. How evil? You can't
|
rlm@10
|
16 ;; handle it, that's how evil it is. That's right. I did it so you
|
rlm@10
|
17 ;; don't have to, ok? Look but don't touch. Use this lib and you'll
|
rlm@10
|
18 ;; go blind.
|
rlm@10
|
19
|
rlm@10
|
20 ;; DEPRECATED in 1.2 with no replacement.
|
rlm@10
|
21
|
rlm@10
|
22 (ns ^{:deprecated "1.2"}
|
rlm@10
|
23 clojure.contrib.apply-macro)
|
rlm@10
|
24
|
rlm@10
|
25 ;; Copied from clojure.core/spread, which is private.
|
rlm@10
|
26 (defn- spread
|
rlm@10
|
27 "Flatten final argument list as in apply."
|
rlm@10
|
28 [arglist]
|
rlm@10
|
29 (cond
|
rlm@10
|
30 (nil? arglist) nil
|
rlm@10
|
31 (nil? (rest arglist)) (seq (first arglist))
|
rlm@10
|
32 :else (cons (first arglist) (spread (rest arglist)))))
|
rlm@10
|
33
|
rlm@10
|
34 (defmacro apply-macro
|
rlm@10
|
35 "This is evil. Don't ever use it. It makes a macro behave like a
|
rlm@10
|
36 function. Seriously, how messed up is that?
|
rlm@10
|
37
|
rlm@10
|
38 Evaluates all args, then uses them as arguments to the macro as with
|
rlm@10
|
39 apply.
|
rlm@10
|
40
|
rlm@10
|
41 (def things [true true false])
|
rlm@10
|
42 (apply-macro and things)
|
rlm@10
|
43 ;; Expands to: (and true true false)"
|
rlm@10
|
44 [macro & args]
|
rlm@10
|
45 (cons macro (spread (map eval args))))
|