a guest Sep 20th, 2018 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- (ns find-m-files
- (:import java.io.File))
- ; Wrap java.io.file methods
- (defn is-file? [f] (.isFile f))
- (defn is-dir? [f] (.isDirectory f))
- (defn get-name [f] (.getName f))
- (defn get-path [f] (.getPath f))
- (defn get-m-files [d]
- "Recursively get a list of File objects for files ending in .m in the given directory"
- (let [ls (if (is-dir? d) (.listFiles d) nil)
- files (filter is-file? ls)
- dirs (filter is-dir? ls)]
- (if (nil? ls)
- (flatten (concat
- (filter (fn [f] (re-matches #".*\.m" (get-name f))) files) ; .m files
- (map get-m-files dirs))))))
- (defn dupes [s]
- "Get a list of the duplicate elements in [s]. That list may itself contain duplicates."
- (empty? s) nil ; Break recursion
- (> (.indexOf (rest s) (first s)) -1) (cons (first s) (dupes (rest s)))
- true (recur (rest s))))
- (defn duplicate-filenames [d]
- "Get a list of the paths to all filenames that are duplicated in the given directory."
- (let [files (get-m-files (File. d))
- names (map get-name files)
- duped-names (set (dupes names))
- duped-files (filter (fn [f] (contains? duped-names (get-name f))) files)]
- (map get-path
- (sort #(compare (get-name %1) (get-name %2)) duped-files))))
RAW Paste Data