Macro expansion after expression (make-placeholders 9) I get the following:
(for [i__1862__auto__ (range 0 9)] (defn-from (str "_" i__1862__auto__) {:placeholder true} [& args] (nth args i__1862__auto__)))
So defn-from expects the string as the first argument, but since it is a macro, (str "_" i__1862__auto__) not evaluated, and thus passes as a list.
I played with this for a while and came up with this:
(defmacro make-placeholders [n] `(map eval '~(for [cntr (range 0 n)] `(defn ~(symbol (str "_" cntr)) {:placeholder true} [& args] (nth args ~cntr)))))
Macroexpanding (make-placeholders 3) gives
(map eval '((defn _0 {:placeholder true} [& args] (nth args 0)) (defn _1 {:placeholder true} [& args] (nth args 1)) (defn _2 {:placeholder true} [& args] (nth args 2))))
as I expected, and evaluating this, defines the functions _0 , _1 and _2 :
;=> (_0 1 2 3) 1 ;=> (_1 1 2 3) 2 ;=> (_2 1 2 3) 3
Ok, this works, but I'm still not sure if this is a good idea.
At first, eval is evil. Well, this could also be done without eval , but use do instead (replace map eval in my solution with do ). But you may be making it difficult to understand your code because you are creating functions that are not defined anywhere in your code. I remember that when I just started using Clojure, I was looking at some library for a function, and I could not find it. I started thinking about yikes, this guy must have defined a macro somewhere that defines the function I'm looking for, how will I ever understand what is happening? If this is how people use Clojure, then it will be one hell of a mess and a dwarf that all people talked about Perl ... It turned out that I was just looking for the wrong version, but you can be configured and others for some difficulties.
Having said that, there may be suitable applications for this. Or you can use something like this to generate code and put it in some file (then the source code will be available for verification). Maybe someone more experienced can call back?