On Wed, Dec 22, 2010 at 9:40 PM, Alex Baranosky < alexander.barano...@gmail.com> wrote:
> 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 > What you want is predicate dispatch. I would search the mailing list archives as well the IRC logs to see what Rich has to say about that. It'd probably useful to write up a desirable design on Confluence as this has come up several times in the past. I know a mini-prolog rules system had been developed at one point though it's not clear to me what was inadequate about it (I imagine performance). It would be lovely to see something like this make into Clojure but no one has tried to build the machinery to make it work. Part of my interest in redoing miniKanren (Logos) was to see how performant a Prolog style logic engine would be in Clojure. There are certainly issues and it's not clear to me if that particular approach will work. It doesn't help that I'm not an LP expert :) However after considerable reading, there are some possibly fruitful avenues of research: 1. A mini-prolog DSL with tabling that compiles to very optimized Clojure 2. Datalog (on top of Reduced Ordered Binary Decision Diagrams ?) In order for a predicate dispatch system to be useful I think it needs to perform fairly close to the speed of the current predicate dispatch system. Such a system would add a great deal of expressiveness as well delivering some incredible guarantees. David -- 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