for宏惰性求值记录

来源:互联网 发布:淘宝异地客服在家兼职 编辑:程序博客网 时间:2024/05/22 06:47

        如下代码

(defn create-new-user-folder [username]  (let [user-root-path (join-path-parts username)        user-root-folder (File. user-root-path)]    (if-not (.exists user-root-folder)      (do        (.mkdirs user-root-folder)        (doall          (for [tmp-dir (seq one-level-folders)]   (let [tar-file (File. (str user-root-path File/separator tmp-dir))]    (if-not (.exists tar-file)      (.mkdirs tar-file)))))))))

在REPL里测试一切正常,可以通过WEB访问就无法创建文件夹,纠结了好久,最后突然意识到可能是惰性序列的原因,在for外层加个doall,果然解决掉了

for宏的说明文档如下

formacroUsage: (for seq-exprs body-expr)List comprehension. Takes a vector of one or more binding-form/collection-expr pairs, each followed by zero or more modifiers, and yields a lazy sequence of evaluations of expr. Collections are iterated in a nested fashion, rightmost fastest, and nested coll-exprs can refer to bindings created in prior binding-forms.  Supported modifiers are: :let [binding-form expr ...], :while test, :when test.(take 100 (for [x (range 100000000) y (range 1000000) :while (< y x)] [x y]))Added in Clojure version 1.0


0 0
原创粉丝点击