从-main调用时,Clojure函数不会将输出写入文件

wro*_*ame 1 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-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)))))))))
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试使用Leinigen生成jar文件时,该函数不再写入文件.所以我尝试-main从REPL 调用,果然,该函数也不会写入文件,即使它在自己调用时也是如此.所以,我尝试定义另一个函数:

(defn call-plot-function
  "Basically calls the plot function, plot-data-files"
  [] (plot-data-files))
Run Code Online (Sandbox Code Playgroud)

果然,它有效.我-main再次尝试打电话,但这次call-plot-functionplot-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)))
Run Code Online (Sandbox Code Playgroud)

再次,plot-data-files神奇地不会在-main调用时写入文件输出.我该怎么办?

ama*_*loy 6

for不是循环.你-main正在返回一个懒惰的序列,你永远不会强迫它的任何元素.repl强制打印,这就是你看到差异的原因.如果你想要副作用而不是序列,doseq则其他语义相同for.