Hi
I've been playing with multimethods, and trying to see if there was a way to
dispatch on a non-specific result of the dispatching function, such as a
range, like this:
(defn how-to-move [map1 map2]
(+ (:cats map1) (:dogs map2)))
(defmulti move how-to-move)
(defmethod move 1 [map1 map2]
(println "one"))
(defmethod move 2 [map1 map2]
(println "two"))
(defmethod move 3 [map1 map2]
(println "three"))
(defmethod move #(> % 3) [map1 map2]
(println "lots!"))
(move {:cats 1} { :dogs 0})
(move {:cats 0} { :dogs 1})
(move {:cats 1} { :dogs 1})
(move {:cats 1} { :dogs 2})
(move {:cats 2} { :dogs 0})
(move {:cats 2} { :dogs 5})
It seems I could easily do this by changing how-to-move to:
(defn how-to-move [map1 map2]
(let [cnt (+ (:cats map1) (:dogs map2))]
(if (> cnt 3)
:lots
cnt)))
and changing the last move defmethod to:
(defmethod move :lots [map1 map2]
(println "lots"))
I am wondering if there is a built in syntax for this?
Thanks,
Alex
--
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