java - 从 -main 调用时,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-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 具有相同的语义。

关于java - 从 -main 调用时,Clojure 函数不将输出写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8633320/

相关文章:

java - HashMap : One Key, 多个值

java - Android:开发的应用程序可以在模拟器中运行,但无法安装在我的设备中

java - 解码 base64 XML 会截掉最后一部分

clojure - 上一个函数输入参数正确存储

java - JVM 是否跟踪 Java 中 foreach 循环中的索引?如果是这样,如何?

python - 如何从文本中找到正面和负面单词的总数?

android - 如何正确使用Android文件系统?

c++ - 在 C++ 中等效的 open(file, O_NONBLOCK)

interface - 在 Clojure 中实现一个带有可变数量 args 的 Java 接口(interface)方法

clojure - 如何在 ClojureScript 中获取由 fetch API 返回的 Response 对象的正文文本?