I would definitely prefer clear-cached-files2. I usually use multimethods when I need to change behavior on type of arguments, not the argument count.
Let's take a look at reduce. I would write the shorter form in terms of the longer one. (defn reduce ([f coll] (reduce f (first coll) (rest coll))) ([f init coll] (reduce stuff...))) I find that this makes my code easier to maintain. Just my $.02 Sean On Oct 3, 3:11 pm, Robert Stehwien <rstehw...@gmail.com> wrote: > 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 clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---