6

Probably an easy one for anyone who actually knows how to write macros in any Lisp. I want to be able to define synonyms for function names. I've been copy-and-paste hacking core.clj to do this, but I don't want to be such a dunce forever! It seems obvious a macro that rewrites the call to a synoym-function into a call to the original function is the right way to do it.

2 Answers 2

13

If I understand your question, there's an easier way: def the new symbol to the old function.

user=> (def foo +)
#'user/foo 
user=> (foo 1 2) 
3

The performance of def also outperforms the macro approach:

(defmacro foo2 [& args]
  `(+ ~@args))

foo2 is then effectively an alias for + and behaves exactly the same way (being rewritten as +) except for the restrictions that are placed on using macros where a value must be returned.

If you want the behavior of the "alias" to be exactly the same as that of the original function (callable in the same contexts as well) then you need to use def to rename the function.

user=> (def foo +)

user=> (defn foo1 [& args]
         `(+ ~@args))

user=> (defmacro foo2 [& args]
         `(+ ~@args))

user=> (time (dotimes [n 1000000] (foo 1 n)))
"Elapsed time: 37.317 msecs"

user=> (time (dotimes [n 1000000] (foo1 1 n)))
"Elapsed time: 292.767 msecs"

user=> (time (dotimes [n 1000000] (foo2 1 n)))
"Elapsed time: 46.921 msecs"
6
  • I think that incurs the overhead of calling the original function, whereas a reader macro would rewrite the source code to call the original function. Or is that not the case in Clojure vs. (my understanding of) Lisp?
    – Al Chou
    Aug 22, 2009 at 6:09
  • I should clarify that last comment. I believe there are two function calls if using the def solution: the synonym function foo is called, and it calls the original function +. A macro should convert the call to foo in the source code into a single call to + in the parsed code. Right?
    – Al Chou
    Aug 22, 2009 at 6:12
  • 4
    I can't answer for Clojure, but for Scheme, the overhead for using + vs foo is the same--the symbol is evaluated. (def foo +) doesn't wrap +, it evaluates +, then binds that function to foo. There is one function call in both cases. Macros are not needed. Aug 22, 2009 at 7:05
  • @Greg Harman most of my response below is support for yours and it makes more sense to put it in your response, especially in light of this discussion. Feel free to copy whatever you like into your response and I'll eliminate mine.
    – Pinochle
    Aug 22, 2009 at 12:27
  • @Nathan Sanders Thanks, apparently it's so much easier than my understanding of On Lisp led me to believe. @Pinochle Thanks for the macro demonstrations as well as the performance profiling in support of Greg and Nathan.
    – Al Chou
    Aug 22, 2009 at 15:20
2

Macros are faster now

I have embarked on the (most assuredly foolish) task of renaming some of Clojure's core functions in one of my projects. I've been having great fun with it (fn becomes λ, loop becomes , etc.), but I found myself very curious about performance. Greg's excellent answer from five years ago is now, I think, partially wrong. I'm using Clojure 1.5.1.

For starters:

user=> (defn foo1 [& args] `(+ ~@args))
#'user/foo1
user=> (foo1 1 2 3 4)
(clojure.core/+ 1 2 3 4)

That's definitely not what you want. Moreover, it appears that now macros are definitely the fastest option. I duplicated Greg's experiments and got very different results. The times you see below are each the best of ten runs:

user=> (def foo +)
#'user/foo
user=> (defn foo1 [& args] (apply + args))
#'user/foo1
user=> (defmacro foo2 [& args] `(+ ~@args))
#'user/foo2
user=> (time (dotimes [n 1000000] (+ 1 n)))
"Elapsed time: 53.401812 msecs"
user=> (time (dotimes [n 1000000] (foo 1 n)))
"Elapsed time: 135.675486 msecs"
user=> (time (dotimes [n 1000000] (foo1 1 n)))
"Elapsed time: 494.770352 msecs"
user=> (time (dotimes [n 1000000] (foo2 1 n)))
"Elapsed time: 53.509264 msecs"

Also, I think the difference between the methods becomes insignificant as the function does more. This is the experiment I had run originally in which I found no difference between them:

user=> (defmacro α [& body] `(reduce ~@body))
#'user/α
user=> (defn β [& body] (apply reduce body))
#'user/β
user=> (def γ reduce)
#'user/γ
user=> (time (dotimes [n 10000] (reduce + (range n))))
"Elapsed time: 5466.920266 msecs"
user=> (time (dotimes [n 10000] (α + (range n))))
"Elapsed time: 5474.532622 msecs"
user=> (time (dotimes [n 10000] (β + (range n))))
"Elapsed time: 5491.337517 msecs"
user=> (time (dotimes [n 10000] (γ + (range n))))
"Elapsed time: 5456.271967 msecs"

Finally, what you're looking for might be defalias from clojure.contrib.def. I have no experience with it.

4
  • 1
    It appears that the Clojure compiler may be looking for calls to + and doing some special optimization.
    – Alex D
    Mar 23, 2014 at 16:50
  • I think so. I've been trying more experiments with more functions, and the defn method is the only one that seems consistently slower. There's nothing like the results for the first experiment.
    – galdre
    Mar 23, 2014 at 17:03
  • your line (defn foo1 [& args] (+ ~@args))` should be a defmacro not a defn right? and why do you say that this macro is 'definitely not what you want" when your tests seem to suggest that it is?
    – johnbakers
    Apr 7, 2014 at 1:36
  • It's not what you want precisely for the reason you give. I brought it up because it's the line from Greg's answer ... but it's not correct. Notice that I don't use that definition in my tests.
    – galdre
    Apr 7, 2014 at 2:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.