Hi,

I'm trying to work out a way to define a multimethod dispatch function that 
will allow me to make a decision based on two values. In the example below I'm 
using symbols for the values of the major and minor values, mostly for 
convenience. What I'm trying to do is dispatch a multifunction on both values 
if the method exists, on the major value alone if it exists, and to the default 
if there's still no method.

Can anyone see a better way of doing this?

Thanks,
Bob

(declare can-we-not-do-better-dispatcher)

(defmulti can-we-not-do-better (fn [major minor] 
(can-we-not-do-better-dispatcher major minor)))
(remove-all-methods can-we-not-do-better)
(defmethod can-we-not-do-better :default [major minor]    (str "default-default 
-> " major minor))
(defmethod can-we-not-do-better :one [major minor]        (str "one-default     
-> " major minor))
(defmethod can-we-not-do-better [:one :two] [major minor] (str "one-two         
-> " major minor))

(defn can-we-not-do-better-dispatcher [major minor]
  (let [method-map (methods can-we-not-do-better)]
    (or (and (get method-map [major minor]) [major minor])
        (and (get method-map major) major)
        :default)))

(defn play []
  (println (can-we-not-do-better :zero :zero))
  (println (can-we-not-do-better :one :two))
  (println (can-we-not-do-better :one :three)))

(play)
default-default -> :zero:zero
one-two         -> :one:two
one-default     -> :one:three

----
Bob Hutchison
Recursive Design Inc.
http://www.recursive.ca/
weblog: http://xampl.com/so




-- 
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

Reply via email to