繁体   English   中英

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

[英]Clojure function not writing output to file when called from -main

所以我有以下功能,当从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)))))))))

但是,当我尝试使用Leinigen生成jar文件时,该函数不再写入文件。 因此,我尝试从REPL调用-main ,并且可以肯定的是,该函数也不会写入文件,即使它本身被调用时也是如此。 因此,我尝试定义另一个函数:

(defn call-plot-function
  "Basically calls the plot function, plot-data-files"
  [] (plot-data-files))

确实,它可以工作。 我尝试再次调用-main ,但是这次直接调用call-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时, plot-data-files神奇地不会写入文件输出。 我在这里应该做什么?

for不是循环。 您的-main返回一个惰性序列,并且您从不强制执行其任何元素。 repl强制打印以便打印,这就是为什么看到差异的原因。 如果你想要的副作用,而不是一个序列, doseq具有否则相同的语义for

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM