How do you programmatically create multiple compile-time parameters in clojure?

I want to create several defs in a file at compile time without typing everything. I would like to do something like:

(ns itervals) (loop [i 0] (if (<= i 128) (do (def (symbol (str "i" i)) i) (recur (+ i 1))))) 

Thus, we define the variables i1, ..., i128 in the current context. I cannot figure out a way to do this at compile time without explicitly defining them. I think macros can be a way, but I have no idea how to do this.

+4
source share
2 answers

This is more like compile time:

 (defmacro multidef[n] `(do ~@ (for [i (range n)] `(def ~(symbol (str "i" i)) ~i)))) (multidef 128) i0 ; 0 i127 ; 127 i128 ; unable to resolve 

But I can't come up with a test that will tell the difference, so maybe the difference is false.

+7
source

Try the following:

 (for [i (range 1 129)] (eval `(def ~(symbol (str "i" i)) ~i))) 
+4
source

Source: https://habr.com/ru/post/1335942/


All Articles