Assum I hava some(more than 20) variables, I want to save them to a file. I don't want to repeat 20 times the same code.I wrote a macro but it gave me an error.
my test case:
;-----------------------------------------------(defn processor [ some-parameters ] (let [ ;after some operation ,got these data: date-str ["JN01","JN02","JN03","JN04"];length 8760 date-temperature (map #(str %2 "," %1) [3.3,4.4,5.5,6.6] date-str) ; all vector's length are 8760 date-ws (map #(str %2 "," %1) [0.2,0.1,0.3,0.4] date-str) ; ;... many variables such like date-relative-humidity,date-pressure, name starts with "date-", ; all same size ] ;(doseq [e date-temperature] ; (println e)) (spit "output-variable_a.TXT" (with-out-str (doseq [e date-temperature] (println e)))) ;same 'spit' part will repeat many times ))(processor 123); I NEED to output other variables(ws, wd, relative-humidity, ...); Output example:;JN01,3.3;JN02,4.4;JN03,5.5;JN04,6.6;-----------------------------------------------
what I want is a macro/function I can use this way:
(write-to-text temperature,ws,wd,pressure,theta-in-k,mixradio)
and this macro/function will do the work.I don't know how to write such a macro/function.
My macro post here but it doesn't work:
(defmacro write-array [& rest-variables ] `(doseq [ vname# '~rest-variables ] ;(println vname# vvalue#) (println "the vname# is" (symbol vname#)) (println "resolve:" (resolve (symbol (str vname# "-lines")))) (println "resolve2:" (resolve (symbol (str "ws-lines")))) (let [ vvalue# 5] ;(var-get (resolve (symbol vname#)))] ;----------NOTE: commented out cause '(symbol vname#)' won't work. ;1(spit (str "OUT-" vname# ".TXT" ) ;1 (with-out-str ;1 (doseq [ l (var-get (resolve (symbol (str vname# "-lines"))))] ;1 (println l)))) (println vname# vvalue#))))
I found that the problem is (symbol vname#)
part, this method only works for a GLOBAL variable, cannot bound to date-temperature in the LET form,(symbol vname#)
returns nil.