I'm toying around with a file utility library as part of a vow to do all my
"scripting" in clojure so I can learn the language - this is actually my
first attempt at writing anything "real" with clojure. I considered using
memoize to cache file listing but wanted to be able to selectively clear the
cache for specific file listings.
I was trying to write a clear-cached-files function that would take nothing
to clear them or take one or more items to clear that list of files. So I
have two questions:
1) What would be the right way to call dissoc with a single item or list. I
tried numerous things but what I have below is what I got to work and I'm
not sure if is the best way.
2) (and the more important question). When would you prefer using a
multimethod (clear-cached-files) that switches based on argument count or a
single method (clear-cached-files2) with multiple argument lists?
Thanks,
Robert
I put the whole source to the file utils I've written below for reference.
----------
(ns rstehwien.clojure.fileutils
(:gen-class)
(:import (java.io File)))
(def file-seq-cache (ref {}))
(defmulti clear-cached-files (fn[& arglist] (count arglist)))
(defmethod clear-cached-files 0 [& arglist]
(dosync (alter file-seq-cache empty)))
(defmethod clear-cached-files 1 [& arglist]
(dosync (alter file-seq-cache dissoc (first arglist))))
(defmethod clear-cached-files :default [& arglist]
(doseq [arg arglist] (clear-cached-files arg)))
(defn clear-cached-files2
([]
(dosync (alter file-seq-cache empty)))
([key]
(dosync (alter file-seq-cache dissoc key)))
([key & ks]
(dosync
(clear-cached-files2 key)
(doseq [key2 ks] (clear-cached-files key2)))))
(defn get-cached-files [path]
(dosync
(when-not (contains? @file-seq-cache path)
(alter file-seq-cache conj {path (file-seq (File. path))}))
(@file-seq-cache path)))
(defn filter-files-ends-with [files suffix]
(filter #(.. % toString (endsWith suffix)) files))
(defn files-ending-with [path suffix]
(filter-files-ends-with (get-cached-files path) suffix))
----------
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---