I think it is a bit dull that for every combination [type1 type2] you have to define an equivalent method for [type2 type1]
Is there a way to address this problem? perhaps with sets? i need to think about this one :D On Monday, November 19, 2012 4:32:15 PM UTC+1, Brian Marick wrote: > > Here's an example of using multimethods. (Note: I overrode the Clojure > defmulti/defmethod macros with my own that play more nicely with the repl, > and for other purposes. It's a straightforward translation.) You can see > all the code here: > > https://github.com/marick/fp-oo/blob/master/sources/asteroids.clj > > The code's explained in chapter 14 of https://leanpub.com/fp-oo > > > > ;;; Objects of interest > (def make > (fn [type value-map] > (with-meta value-map {:type type}))) > > (def rim-griffon (make ::starship {:name "Rim Griffon", :speed 1000})) > (def malse (make ::asteroid {:name "Malse", :speed 0.1, > :gravitational-pull 0.5})) > > > ;;; Collide with multi-arg dispatch > > (defgeneric collide (fn [one two] [(type one) (type two)])) > > (defspecialized collide [::asteroid ::asteroid] > (fn [& asteroids] asteroids)) > > (defspecialized collide [::asteroid ::starship] > (fn [asteroid starship] > [asteroid (stopped starship)])) > > (defspecialized collide [::starship ::asteroid] > (fn [starship asteroid] > [(stopped starship) asteroid])) > > (defspecialized collide [::starship ::starship] > (fn [& starships] > (let [speed (apply max (map :speed starships))] > (map (partial with-speed speed) starships)))) > > > > > > ----- > Brian Marick, Artisanal Labrador > Contract programming in Ruby and Clojure > Occasional consulting on Agile > Writing /Functional Programming for the Object-Oriented Programmer/: > https://leanpub.com/fp-oo > > > -- 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