Thanks Darren.
Along the lines of your macro I created the one
below that avoids clojure.lang.*.
I keep forgetting this is lisp and we can write a macro :P
Here is what I could come up with.
(defmulti what-num (fn [x] x))
(defmacro defmethod-in [n dval-coll args & body]
(let [get-method (fn [d] `(defmethod ~n ~d ~args ~@body))]
`(do ~@(map get-method dval-coll))))
(defmethod-in what-num [:one :two] [x] (println "1 or 2" x))
(defmethod what-num :three [x] (println "saw 3"))
(defmethod what-num :four [x] (println "saw 4"))
(defmethod what-num :default [x] :default)
In case this can be improved further, comments welcome.
Seems to be working fine so far:
user=> (what-num :four)
saw 4
nil
user=> (what-num :one)
1 or 2 :one
nil
user=> (what-num :two)
1 or 2 :two
nil
user=> (what-num :three)
saw 3
nil
user=> (what-num :five)
:default
Parth
> Which is just a wrapper around the defmethod macro that allows you to
> specify a list of values instead of a single value that will be mapped
> to the method.
>
> This would allow you to define your above example with:
>
> (defmulti what-num (fn [x] x))
>
> (defmethod-for-values what-num '(:one :two) [x] (println "saw one or
> two: " x))
> (defmethod what-num :three [x] (println "saw 3"))
> (defmethod what-num :four [x] (println "saw 4"))
>
> See this thread for more details:
>
>
http://groups.google.com/group/clojure/browse_thread/thread/eed9015c2...
>
> Hope this helps,
> --Darren