简体   繁体   English

Main如何在Clojure上工作

[英]How -main works on Clojure

I am with a problem with a clojure code, and I don't understand what is happen here. 我对clojure代码有疑问,我不明白这里发生了什么。

Code: 码:

(defn -main []
  (clean-bucket-if-limit)
  (do-something-1)
  (do-something-2)
  (do-something-3))


(defn clean-bucket-if-limit
  []
  (let [objects (get (s3/list-objects cred "deske") :objects)]
    (let [number (count objects)]
      (if (> 3 number)
        (map delete-file-s3 objects)
        (println "no limit")))))

(defn delete-file-s3
  [object]
  (let [key (get object :key)]
    (s3/delete-object cred "bucket-name" key)))

The problem is that when I execute clean-bucket-if-limit only (on REPL), everything works well, but when I use the main function not (files aren't deleted). 问题是,当我仅执行clean-bucket-if-limit(在REPL上)时,一切正常,但是当我不使用main函数时(文件未删除)。

map is lazy . map 很懒 When you run (map delete xs) , you get back a lazy sequence immediately, and no work is actually done until you force the sequence by requesting elements from it. 运行(map delete xs) ,您立即返回一个惰性序列,并且直到您通过从序列中请求元素来强制序列之前,实际上并没有完成任何工作。 At the repl, the sequence is forced when it's printed to the screen. 在替换时,将序列打印到屏幕时会强制执行。 But when you run your program for real, -main returns void, and just discards the value it receives, causing nothing to ever be realized. 但是,当您实际运行程序时, -main返回void,并且仅丢弃其收到的值,从而使任何事情都无法实现。 You should use doseq or dorun or some other side-effectful sequence function if you want to iterate over a sequence for side effects, not just map . 如果您要遍历序列的副作用而不是map ,则应该使用doseqdorun或其他一些副作用序列函数。

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

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