I have a case where I need several send values ββin a multimethod to map the same method. For example, to send the value 1, I want this to call method-a, and to send the values ββ2, 3 or 4 I want this to call method -b.
After some Googling, I ended up writing the following macro:
(defmacro defmethod-dispatch-seq [mult-fn dispatch-values & body] `(do (map (fn [x#] (defmethod ~mult-fn x# ~@body )) ~dispatch-values)))
Then you can use it as follows:
(defmulti f identity) (defmethod f 1 [x] (method-a x)) (defmethod-dispatch-seq f [2 3 4] [x] (method-b x))
Lets you call the following:
(f 1) => (method-a 1) (f 2) => (method-b 2) (f 3) => (method-b 3) (f 4) => (method-b 4)
Is that a good idea?
Asher source share