Created
October 15, 2020 19:30
-
-
Save duanebester/a3ea84e412e770decd5259a95533426f to your computer and use it in GitHub Desktop.
Get all nested File objects from a directory | Clojure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Lovingly stolen from: https://gist.github.com/adambard/2217871 | |
(require '[clojure.java.io :as io]) | |
(defn is-file? [f] (.isFile f)) | |
(defn is-dir? [f] (.isDirectory f)) | |
(defn get-name [f] (.getName f)) | |
(defn get-path [f] (.getPath f)) | |
(def dir-path "/Users/<username>/Downloads/data") | |
(def dir (io/file dir-path)) | |
(defn get-nested-files [d] | |
(let [ls (if (is-dir? d) (.listFiles d) nil) | |
files (filter is-file? ls) | |
dirs (filter is-dir? ls)] | |
(if (nil? ls) | |
nil | |
(flatten (concat | |
files | |
(map get-nested-files dirs)))))) | |
(get-nested-files dir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment