Skip to content

Commit

Permalink
clean up, remove some dead code, wrap docstrings, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
hiredman committed Mar 12, 2009
1 parent 35bc1fd commit 6d9a805
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions pwalker.clj
Expand Up @@ -13,38 +13,36 @@
(.execute #^ThreadPoolExecutor runner fn))

(defn walk
"takes a direcotyr as a File, a function to be applied to all directories
a fn to be applied to all files, returns a ref for bookkeeping"
"takes a direcotyr as a File, a function to be
applied to all directories a fn to be applied to all
files, returns a ref for bookkeeping"
([dir dir-fn file-fn]
(dir-fn dir)
(walk dir dir-fn file-fn (ref 0)))
([dir dir-fn file-fn r]
(let [files (.listFiles #^File dir)
[++1 +-1] [#(dosync (alter r inc)) #(dosync (alter r dec))]]
(doseq [entry files]
(++1)
(doseq [entry files] (++1)
(if (.isDirectory entry)
(execute #(do
(dir-fn entry)
(walk entry dir-fn file-fn r)
(+-1)))
(do
(file-fn entry)
(+-1)))))
(execute #(do (dir-fn entry) (walk entry dir-fn file-fn r) (+-1)))
(do (file-fn entry) (+-1)))))
r))



(defn collect-files
"takes a directory name as a string, and optionally a predicate to filter files
and walks a directory in a recursive and concurrent manner"
"takes a directory name as a string, and optionally
a predicate to filter files and walks a directory
in a recursive and concurrent manner"
[dir & opt]
(let [q (LinkedBlockingQueue.)
f (fn f [#^LinkedBlockingQueue q]
(lazy-seq
(when (.peek q)
(cons (.take q) (f q)))))
(let [queue (LinkedBlockingQueue.)
fil (if opt (first opt) identity)
x #(when (fil %) (.put q %))
x #(when (fil %) (.put queue %))
count (walk (File. #^String dir) identity x)]
(take-while #(when (or (not= 0 @count) %) %)
(repeatedly #(.poll q)))))
(repeatedly #(.poll queue)))))


(defn consume [#^Queue queue finished?]
(take-while (comp not finished?)
(repeatedly #(.poll queue))))

0 comments on commit 6d9a805

Please sign in to comment.