问题描述
我有以下代码:
(defn remove-folder [file]
(do
(println "Called with file " (.getName file))
(if (.isDirectory file)
(do
(println (.getName file) " is directory")
(def children (.listFiles file))
(println "Number of children " (count children))
(map remove-folder children)
(delete-file file)))
(do
(println (.getName file) " is file")
(delete-file file)
)))
我的问题是行(map remove-folder children)似乎不工作。在我的输出,我希望下来虽然文件夹结构,但它似乎保持在第一级。
My problem is that the line (map remove-folder children) doesn't seem to work. In my output I expect to travel down though the folderstructure but it seems to stay in the first level.
我想我已经犯了一些愚蠢的错误,但我现在花了几个小时,似乎没有更接近的解决方案。
I'll guess I have made some stupid misstake but I spent a few hours on it now and doesn't seem to get closer a solution.
推荐答案
正如@ponzao所说, map
是懒惰的。
As @ponzao said, map
is lazy.
但是无论如何,我不认为使用 map
只是为了得到副作用和丢弃的结果是个好主意。我个人会使用 doseq
,因为它给我一个暗示,我期待副作用。
But anyway I don't think it's a good idea to use map
just to get side effects and "drop" the result. Personally I would use doseq
instead, because it gives a hint that I'm expecting side effects.
(defn remove-folder [file]
(do
(println "Called with file " (.getName file))
(if (.isDirectory file)
(do
(println (.getName file) " is directory")
(def children (.listFiles file))
(println "Number of children " (count children))
(doseq [c children]
(remove-folder c))
(delete-file file)))
(do
(println (.getName file) " is file")
(delete-file file)
)))
这篇关于调用map-function似乎没有做任何事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!