-
-
Save raek/1498952 to your computer and use it in GitHub Desktop.
This file contains 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
(ns my-file-utils | |
(:import java.io.File)) | |
;; Java methods cannot be used as Clojure functions, | |
;; so you have to wrap them in functions. | |
;; anonymous function, map, and filter version | |
(defn list-files [d] | |
(map (fn [f] (.getName f)) | |
(filter (fn [f] (.isDirectory f)) | |
(.listFiles d)))) | |
;; anonymous function shorthand | |
(defn list-files [d] | |
(map #(.getName %)) | |
(filter #(.isDirectory %) | |
(.listFiles d)))) | |
;; version using "threading" macros | |
(defn list-files | |
(->> (.listFiles d) | |
(filter #(.isDirectory %)) | |
(map #(.getName %)))) | |
;; list comprehension version | |
(defn list-files [d] | |
(for [f (.listFiles d) | |
:when (.isDirectory f)] | |
(.getName f))) | |
;; Usage: (prn (list-files (File. "/home/mcrittenden/Downloads"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment