Java Clojure函数在从-main调用时未将输出写入文件
因此,我有以下函数,当从REPL调用时,该函数运行良好:Java Clojure函数在从-main调用时未将输出写入文件,java,file-io,clojure,Java,File Io,Clojure,因此,我有以下函数,当从REPL调用时,该函数运行良好: (defn plot-data-files "Produces a file with x-axis values on 1st column and y-axis values on the 2nd column" [] (let [filename (user-input "file to which you want to write plot output") x-values file-data-
(defn plot-data-files
"Produces a file with x-axis values on 1st column and y-axis values on the
2nd column"
[]
(let [filename (user-input "file to which you want to write plot output")
x-values file-data-days
y-values (get-dndtf)
file-writer (new java.io.FileWriter filename)]
(if (= (first y-values) za-not-found)
(println za-not-found)
(for [index (range (count x-values))]
(binding [*out* file-writer]
(prn (Double/parseDouble (str (nth x-values index)))
(Double/parseDouble (str (nth y-values index)))))))))
但是,当我尝试使用Leinigen生成jar文件时,该函数不再写入文件。因此,我尝试从REPL调用-main
,果然,该函数也不会写入文件,即使它自己调用时也会写入文件。因此,我尝试定义另一个函数:
(defn call-plot-function
"Basically calls the plot function, plot-data-files"
[] (plot-data-files))
果然,它起作用了。我尝试再次调用-main
,但这次调用调用plot function
,而不是直接调用plot data files
:
(defn -main [& args]
(get-all-file-data)
(while (not (= \n (first (user-input "whether you want to output more plot
files"))))
(call-plot-function)))
同样,调用
-main
时,绘图数据文件
神奇地不写入文件输出。我应该在这里做什么?for
不是一个循环。您的-main
正在返回一个惰性序列,并且您从不强制执行它的任何元素。repl强制打印,这就是您看到差异的原因。如果您想要副作用而不是序列,doseq
具有与for
相同的语义,因为for
不是循环。您的-main
正在返回一个惰性序列,并且您从不强制执行它的任何元素。repl强制打印,这就是您看到差异的原因。如果您想要副作用而不是序列,doseq
具有与for
相同的语义