2011-12-26 39 views
1

調用時文件,所以我有以下功能從REPL調用時效果很好:Clojure的功能不寫輸出 - 主

(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))) 

再次,plot-data-files奇蹟般地不寫文件輸出時-main被調用。我應該在這裏做什麼?

回答

6

for不是一個循環。你的-main正在返回一個懶惰的序列,並且你永遠不會強迫它的任何元素。複製力量爲了打印,這就是爲什麼你看到不同。如果您想要副作用而不是序列,則doseq具有與for相同的語義。