0

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.

4

3 回答 3

3

map is lazy, wrap it in a doall to force evaluation, like this: (doall (map remove-folder children)).

于 2013-03-30T22:52:30.517 回答
2

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)
          )))
于 2013-03-31T04:42:54.477 回答
0

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 -

于 2013-04-02T03:37:24.323 回答