Skip to content

Instantly share code, notes, and snippets.

@raek
Forked from crittermike/files.clj
Created December 19, 2011 21:26
Show Gist options
  • Save raek/1498952 to your computer and use it in GitHub Desktop.
Save raek/1498952 to your computer and use it in GitHub Desktop.
(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