Skip to content

Instantly share code, notes, and snippets.

@bpsm
Created October 6, 2010 12:38

Revisions

  1. bpsm created this gist Oct 6, 2010.
    24 changes: 24 additions & 0 deletions gistfile1.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    (ns bpsmannschott
    (:import java.io.File)
    (:import java.io.FileNotFoundException))

    (defn as-file [s]
    "Return whatever we have as a java.io.File object"
    (cond (instance? File s) s ; already a file, return unchanged
    (string? s) (File. s) ; return java.io.File for path s
    :else (throw (FileNotFoundException. (str s)))))

    (defn walk [^File dir]
    (let [children (.listFiles dir)
    subdirs (filter #(.isDirectory %) children)
    files (filter #(.isFile %) children)]
    (concat files (mapcat walk subdirs))))

    ;; EXAMPLE
    ;; count the number of files in or below /tmp
    ;;
    ;; (count (walk (as-file "/tmp")))
    ;;
    ;; alternately:
    ;;
    ;; (-> "/tmp" as-file walk count)