Using Clojure for Scripting

Posted by Uncle Bob on 08/08/2009

Clojure makes a nice language for writing scripts that explore things on the filesystem. For example, here is a cute little program that explores the FitNesse source tree counting the lines of production java code and comparing it to the lines of unit test code.

(use 'clojure.contrib.duck-streams)  (def file-counts   (map     #(list (.toString %) (count (read-lines %)))     (remove #(.isDirectory %)       (file-seq (file-str "~/projects/FitNesseGit/src/fitnesse")))))  (defn count-lines-that-end-with [file-counts suffix]   (reduce +     (map second       (filter #(.endsWith (first %) suffix) file-counts))))  (def java-count (count-lines-that-end-with file-counts ".java")) (def test-count (count-lines-that-end-with file-counts "Test.java"))  (printf "Java lines: %d\n" java-count) (printf "Test lines: %d\n" test-count) (printf "Test pct: %.1f\n" (double (* 100 (/ test-count java-count))))

Comments

Leave a response