Reputation: 5438
I have the following code:
(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)
)))
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.
Upvotes: 0
Views: 122
Reputation: 412
Why don't you inline children and get rid of def? It doesn't seem that you want to create a global binding in your current namespace. You are unintentionally introducing a side effect.
The function name remove-folder does not tell us what the function is supposed to do. The argument name file does not describe what kind of argument can be expected. But I understand that it's kind of exploratory code.
Cheers -
Upvotes: 0
Reputation: 15422
As @ponzao said, map
is lazy.
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)
)))
Upvotes: 2
Reputation: 20934
map
is lazy, wrap it in a doall
to force evaluation, like this: (doall (map remove-folder children))
.
Upvotes: 3